当使用MATLAB的发布功能时,它通常只发布%符号或函数输出之后的内容。但是,是否有任何命令来获取变量并将将其值拼接成文本,甚至可能从包含字符串的MATLAB变量创建LaTeX公式?
答案 0 :(得分: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);
以下是将文件发布为HTML时的样子:
您可以将最后一个代码包装在辅助函数render_latex_string(str)
中,并从不同的地方调用它。
答案 1 :(得分:3)
当使用带有html编码字符串的disp()
发布到html时,会将这些行添加到输出中(不使用代码输出的格式)。
例如
str = sprintf('some value: %f from the workspace',variable)
disp(['<html>',str,'</html>'])
注意:
%% 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>'])
上面的代码生成以下内容: