Matlab图中注释文本框中的Overbar(使用Latex作为解释器)

Mil*_*ila 5 matlab latex

我试着写一个简单的平面方程加上系数\ bar {a} _1,a_2和a_3的值,但我无法使\ bar工作.有什么建议吗?我尝试$\bar {a} _ {1} $有或没有美元,其他一切对我来说都很好,我使用了乳胶或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])  
Run Code Online (Sandbox Code Playgroud)

谢谢

pm8*_*m89 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);
Run Code Online (Sandbox Code Playgroud)

原始代码存在问题

  1. 你必须用符号包装整个latex 命令$:

    此代码未提供所需的输出:

    annotation('textbox', [.2 .4 .1 .1], 'interpreter','latex', 'String', 'a_2x');
    
    Run Code Online (Sandbox Code Playgroud)

    但是这个做了:

    annotation('textbox', [.2 .4 .1 .1], 'interpreter','latex', 'String', '$a_2x$');
    
    Run Code Online (Sandbox Code Playgroud)
  2. 如果你使用sprintf它,你将丢失字符串的一些部分,因为它有另一个解释器(有解决方法,但我建议连接字符串,如上所述)

    sprintf('$\bar{a}_{1}$ = %5.2f', a1)
    
    Run Code Online (Sandbox Code Playgroud)

    将返回:

    ar{a}_{1}$ = 1.00
    
    Run Code Online (Sandbox Code Playgroud)

    这是不被承认的latex.(\b被解释为退格sprintf,并删除的重要$标志.)