How to manually deduce publication of content from a variable

When using the publish function in MATLAB, it usually publishes only what appears after the% signs or the exit of the function. However, is there any command to take a variable and combine its value into text, perhaps even creating LaTeX formulas from the MATLAB variable, which contains a character string?

+7
matlab publishing latex
source share
2 answers

To use variables from the workspace

when publishing to html using disp() using an encoded html string will add the lines to the output (without using formatting to output the code).

eg

 str = sprintf('some value: %f from the workspace',variable) disp(['<html>',str,'</html>']) 

Notes:

  • This is pretty sensitive, and you may need to add invisible section breaks between the code output and lines created in this way.
  • Adding paragraph tags is useful for improving formatting.
  • Sadly (as far as I know) the markdown publication interpreter is not used in these lines, they are β€œentered” into the output, so latex equations will not work .

the code

 %% HTML option % This option is anly available with HTML output... a=1; str = ['The current value of a is ', num2str(a)]; %%% % % When publishing to HTML using the |disp| function with HTML tags % surrounding the string can allow workspace variables to appear within % text. % % For example the following line is created by evaluating code, it is not a % comment in the m-file % disp(['<html><p>',str,'</p></html>']); %% Changing the value % Now if we change a to 2... a=2,str = ['The new value of a is ', num2str(a)]; %% % Re-runing a similar code should show the updated value disp(['<html><p>',str,'</p></html>']) 

Exit

The above code generates the following:

enter image description here

+3
source share

Here is an example of rendering LaTeX formulas (one hard-coded in the comments, the other as a string in a variable).

 %% LaTeX Examples % Below are some equations rendered in LaTeX. % (try to publish this file). % %% The definition of e % Here we use equation embedded in the file. % % $$ e = \sum_{k=0}^\infty {1 \over {k!} } $$ % %% The Laplace transform % Here we render an equation stored in a variable. % % offscreen figure fig = figure('Menubar','none', 'Color','white', ... 'Units','inches', 'Position',[100 100 6 1.5]); axis off str = 'L\{f(t)\} \equiv F(s) = \int_0^\infty\!\!{e^{-st}f(t)dt}'; text(0.5, 0.5, ['$$' str '$$'], 'Interpreter','latex', 'FontSize',28, ... 'HorizontalAlignment','center', 'VerticalAlignment','middle') snapnow close(fig); 

Here's what it looks like when a file is published as HTML:

published_html

You can transfer this last code to the render_latex_string(str) helper function and call it from different places.

+8
source share

All Articles