在使用Latex作为解释器的Matlab图中的注释文本框中有上横线

5

我正在尝试编写一个简单的平面方程,加上系数\bar{a}_1、a_2和a_3的值,但是我无法使\bar正常工作。有什么建议吗?我尝试过使用带有或不带有美元符号的$\bar{a}_{1}$,其他一切看起来都很好,我使用了latex或tex解释器,但它无法识别它。这是代码:

a1=1
a2=2
a3=3
str = {'LLSQ fit:','z=$\bar{a}_{1}$+a_2x+a_3y',sprintf('$\bar{a}_{1}$=%5.2f',a1),sprintf('a_2=%5.2f',a2),sprintf('a_3=%5.2f',a3)};
annH = annotation('textbox',[0.63 0.8 0.08 0.08],'string',str,'interpreter','latex')
set(annH,'FitBoxToText','on','fontsize', 18,'BackgroundColor',[1 1 1])  

thanks

1个回答

5
我已经列出了您代码中的问题,以下是可运行的代码:
str = {'LLSQ fit: ' ...
    'z = $\bar{a}_{1}$ + $a_2x$ + $a_3y$' ...
    ['$\bar{a}_{1}$ = ' sprintf('%5.2f', a1)] ...
    ['$a_2$ = ' sprintf('%5.2f', a2)] ...
    ['$a_3$ = ' sprintf('%5.2f', a3)]};

annotation('textbox', [0.5 0.8 0.3 0.08], 'interpreter','latex', 'String', str);

原始代码存在的问题

  1. You have to wrap the whole latex commands with $ sign:

    This code does not give the desired output:

    annotation('textbox', [.2 .4 .1 .1], 'interpreter','latex', 'String', 'a_2x');
    

    But this one does:

    annotation('textbox', [.2 .4 .1 .1], 'interpreter','latex', 'String', '$a_2x$');
    
  2. You will loose some part of the string if you use sprintf since it has another interpreter (there are workarounds for this, but I suggest concatenating strings as I did above)

    sprintf('$\bar{a}_{1}$ = %5.2f', a1)
    

    will return:

    ar{a}_{1}$ = 1.00
    

    which is not recognized by latex. (\b is interpreted as backspace in sprintf and removes the vital $ sign.)


1
通常情况下,sprintf不支持带反斜杠的LaTeX命令,因为它会尝试解释像\b\t等这样的字符。因此,如果您想让LaTeX命令通过sprintf,您需要转义反斜杠本身:sprintf('$\\bar{a}_{1}$ = %5.2f', a1)(请注意两个反斜杠)。 - anandr

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接