MATLAB: Publishing Latex equations in MATLAB comments

equationslatexMATLABpublishing

%
% $2+2 = 4$
When publishing an M file to HTML, how do I include Latex declarations in my MATLAB code so that equations are formatted nicely in HTML?

Best Answer

When publishing MATLAB code to HTML, one can nicely format equations by declaring them in Latex written into the MATLAB code's comments. There are certain formatting restrictions that need to be observed in order for MATLAB to interpret the Latex correctly:
1. The Latex definitions must be in a comment directly below a code section delimiter (i.e. a "%%").
CORRECT:
%%




% $2 + 2 = 4$
INCORRECT:
%

% $2+2 = 4$
2. The Latex code, if declared inline with a sentence, must be placed between single dollar signs (corresponding with Latex math mode declarations).
%%
% Latex makes this equation pretty: $a^2 + b^2 = c^2$
3. Latex code can be declared in blocks, which are delimited by double dollar signs (i.e. "$$"). Latex that is output on its own line must be alone on a separate comment line and have one empty comment line above it.
%%
% $$a^2 +
% b^2 = c^2$$
OR
%%
% This is a block of latex that will be output on its own line:
%
% $$a^2 + b^2 = c^2$$
4. All Latex code that is placed on its own line must have only one space between the MATLAB comment sign (i.e. "%") and the dollar sign that signals the beginning of the Latex definition.
CORRECT:
%%
% $a^2 = b^2 + c^2$
INCORRECT:
%%
% $a^2 = b^2 + c^2$
For further information on Latex equations in MATLAB publishing, refer to the following documentation: