Matlab情节中的希腊字母

Erd*_*ger 8 matlab matlab-figure

我在Matlab中创建了一个图,现在我想用以下命令添加一个图例:

legend({'Pos1', 'Pos2', 'Pos3', '\alpha Pos4'}, 'Location', 'northeast', 'Interpreter', 'latex', 'fontsize', 22);
legend('boxoff')
Run Code Online (Sandbox Code Playgroud)

问题是\alpha没有转化为希腊字母.如果我省略大括号{}然后它可以工作但我需要它们因为我只想标记前四行.

我怎样才能获得希腊字母alpha?

Dan*_*iel 6

你忘记了 $

legend({'Pos1', 'Pos2', 'Pos3', '$\alpha$ Pos4'}, 'Location', 'northeast', 'Interpreter', 'latex', 'fontsize', 22);
Run Code Online (Sandbox Code Playgroud)


Mat*_*att 5

我想延伸丹尼尔的答案并解释一些细节.

没有发生什么 {}

当图例条目在单元阵列中指定,只有属性LocationOrientation所用的直接呼叫中使用legend.如果存在其他属性,则将它们解释为图例条目.这意味着Interpreter,TextSize它的值将是图例条目.Adiel评论为什么它显然没有效果{}:它不是真的,它甚至会因为上述原因而间接发出警告.

旁注:根据语法,必须在特性之前提供图例条目.不过它确实以任何顺序工作,但我不建议使用这种无证件的行为.

选择地块

你提到你必须使用它{}来只选择前四行.这不是真的,因为默认legend选择前N个图.问题在于如上所述解释了属性.要选择特定图,您可以使用图表句柄省略第二个图:

legend([ph1,ph3,ph4,ph5], 'Pos1', 'Pos3', 'Pos4', 'Pos5');
Run Code Online (Sandbox Code Playgroud)

使用其他属性

为了能够在调用中直接使用其他属性legend,可以将图例条目作为单元格数组提供.这将条目与属性的名称 - 值对分离.例如,更改字体大小:

legend({'Pos1', 'Pos2', 'Pos3', 'Pos4'}, 'Fontsize', 22);
Run Code Online (Sandbox Code Playgroud)

另一种可能性是使用句柄来设置其他属性而不使用单元格数组:

l = legend('Pos1', 'Pos2', 'Pos3', 'Pos4');
set(l, 'Fontsize', 22);     % using the set-function
l.FontSize = 22;            % object oriented
Run Code Online (Sandbox Code Playgroud)

latex-interpreter

如果设置Interpreter为,latex那么图例条目的所有内容都需要通过latex进行编译.这意味着\alpha不能在数学环境之外使用.要在LaTeX中添加内联数学表达式,可以使用$-signs 将其括起来.所以$\alpha$像丹尼尔的答案中提到的那样工作.使用tex-interpreter,Matlab使用TeX标记的一个子集,并自动适用于支持的特殊字符,因此不需要$...$何时不使用latexintrpreter.


建议

  • 别忘了$-signs.
  • 在通话中点击特定地块legend.
  • 使用单元格数组并legend直接将调用中的所有属性设置为.
  • 随着...您可以在若干个分割线长.

例如这样:

legend([ph1,ph3,ph4,ph5], ...
    {'Pos $\alpha$', 'Pos $\beta$', 'Pos $\gamma$', 'Pos  $\delta$'}, ...
    'Location', 'northeast', 'Interpreter', 'latex', 'FontSize', 22);
Run Code Online (Sandbox Code Playgroud)

这是示例的完整代码:

figure; hold on;
ph1 = plot(0,-1,'*'); ph2 = plot(0,-2,'*');
ph3 = plot(0,-3,'*'); ph4 = plot(0,-4,'*');
ph5 = plot(0,-5,'*'); ph6 = plot(0,-6,'*');
legend([ph1,ph3,ph4,ph5], ...
    {'Pos $\alpha$', 'Pos $\beta$', 'Pos $\gamma$', 'Pos  $\delta$'}, ...
    'Location', 'northeast', 'Interpreter', 'latex', 'FontSize', 22);
Run Code Online (Sandbox Code Playgroud)

有了这个结果:

例