如何从变量手动输出发布内容

Dmi*_*ruk 7 matlab latex publishing

当使用MATLAB的Publish功能时,它通常只发布%符号或函数输出之后的内容.但是,是否有任何命令可以获取变量并将其值拼接成文本,甚至可能从包含字符串的MATLAB变量创建LaTeX公式?

Amr*_*mro 8

下面是一个渲染LaTeX公式的示例(一个在注释中硬编码,另一个在变量中存储为字符串).

%% 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);
Run Code Online (Sandbox Code Playgroud)

以下是将文件作为HTML发布时的样子:

published_html

您可以将最后一个代码包装在辅助函数中render_latex_string(str),并从不同的位置调用它.


RTL*_*RTL 3

使用工作区中的变量

当使用 html 编码字符串发布到 html 时disp(),会将行添加到输出(不使用代码输出的格式)。

例如

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

笔记:

  • 它非常敏感,您可能需要在代码输出和以此方式生成的行之间添加不可见的分节符。
  • 添加段落标签对于改进格式很有用
  • 遗憾的是(据我所知)这些行上没有使用发布 Markdown 解释器,它们被“注入”到输出中,因此乳胶方程将不起作用

代码

%% 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>'])
Run Code Online (Sandbox Code Playgroud)

输出

上面的代码生成以下内容:

在此输入图像描述