The only way I figure is writing a <.m> file that builds all the comments. Something like: generator.m --> pub.m --> html (origin) (file to Publish) (Published web page)
My code example,<myOwnSolution.m> is:
% My own solution
clc, clear, close all
syms s
fprintf('I want to Publish a LaTeX Variable, randomly built.\n');
fprintf('Let''s say:\n');
a = randi([2,7]);
b = randi([2,7]);
P = [1,a,b]; % Coeffs are randomly selected
symPol = poly2sym(P,s);
disp('A pretty view of P(s), in the Workspace:')
pretty(symPol);
fprintf('\n\n')
fileID = fopen('testPublishLaTeX.m','w');
fprintf(fileID,'%%%% Publish a LaTeX variable\n');
fprintf(fileID,'%% This is a Publish file in Matlab\n');
fprintf(fileID,'%%%% First Section \n');
fprintf(fileID,'%% Publish a LaTeX variable in a text line: $P(s)=%s$\n',latex(symPol));
fprintf(fileID,'%%%% Second Section\n');
fprintf(fileID,'%% Publish a LaTeX variable and expression in a ordered list:\n');
fprintf(fileID,'%%\n');
fprintf(fileID,'%% # A LaTeX variable: $P(s)=%s$\n',latex(symPol));
fprintf(fileID,'%% # A LaTeX expression: $F(s)=\\alpha^2$\n');
fprintf(fileID,'%%%% Third Section\n');
fprintf(fileID,'%% Publish a LaTeX variable and expression in a unordered list:\n');
fprintf(fileID,'%%\n');
fprintf(fileID,'%% * A LaTeX variable: $P(s)=%s$\n',latex(symPol));
fprintf(fileID,'%% * A LaTeX expression: $F(s)=\\alpha^2$\n');
fprintf(fileID,'%%%% Fourth Section\n');
fprintf(fileID,'%% Publish a LaTeX variable and expression as lone expressions:\n');
fprintf(fileID,'%%\n');
fprintf(fileID,'%% $$P(s)=%s$$\n',latex(symPol));
fprintf(fileID,'%%\n');
fprintf(fileID,'%% $$F(s)=\\alpha^2$$\n');
fprintf(fileID,'%%%% Fifth Section\n');
fprintf(fileID,'%% Publish a LaTeX variable with (not in) HTML format:\n');
fprintf(fileID,'%%\n');
fprintf(fileID,'%% <html>\n');
fprintf(fileID,'%% <div style="font-family:Georgia, serif; font-size:large;">\n');
fprintf(fileID,'%% <p>This is my polynomial over s:</p>\n');
fprintf(fileID,'%% </div>\n');
fprintf(fileID,'%% </html>\n');
fprintf(fileID,'%%\n');
fprintf(fileID,'%% $$P(s)=%s$$\n',latex(symPol));
fprintf(fileID,'%%\n');
fclose(fileID);
myDoc = publish("testPublishLaTeX.m","html");
Now, I can view in the browser the file <testPublishLaTeX.html>, just created whith the 'publish' command.