Greek letter in the plot of Matlab

I created a plot in Matlab, and now I would like to add a legend with the following command:

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

The problem is that \alpha not converted to the Greek letter. If I omit the curly braces {}, then it works, but I need them because I only want to mark the first four lines.

How can I get the letter greek?

+7
matlab matlab-figure
source share
2 answers

I want to talk about Daniel and explain some details.

What happens without {}

When legend records are not specified in an array of cells, only the Location and Orientation properties can be used in a direct call to legend . If other properties are present, they are interpreted as legend records. This means Interpreter and TextSize , and the values ​​will be legend entries. Adiel comments on why he apparently works without {} : he doesn’t actually do this, he even warns, indirectly because of the above reasons.

Sidenote: According to the syntax, legend entries must be submitted before creation. However, it works in any order, but I do not recommend using this undocumented behavior.

Choice Charts

You mentioned that you need to use {} to select only the first four lines. This is not so because legend selects the first N graphs by default. The problem was that the properties got the interpretation, as explained above. To select specific plots, you can use plot descriptors to leave a second plot:

 legend([ph1,ph3,ph4,ph5], 'Pos1', 'Pos3', 'Pos4', 'Pos5'); 

Using other properties

To be able to directly use other properties in the legend call, you can provide legend entries as an array of cells. This separates records with pairs of property-name values. For example, change the font size:

 legend({'Pos1', 'Pos2', 'Pos3', 'Pos4'}, 'Fontsize', 22); 

Another possibility is to use a descriptor to set other properties without using an array of cells:

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

latex -interpreter

If you set Interpreter to latex , then the entire contents of the legend entries must be compiled in a latex way. This means that \alpha cannot be used outside the mathematical environment. To add an inline-math expression to LaTeX, you can enclose it with $ -signs. So, $\alpha$ works as mentioned in Daniel's answer. With tex -interpreter, Matlab uses a subset of TeX markup and automatically works for supported special characters, so there is no need for $...$ if you are not using intrpreter latex .


suggestions

  • Do not forget about $ -signs.
  • In the case of legend select special graphics in the list.
  • Use an array of cells and set all the properties in the legend call directly.
  • With ... you can split a long string into several.

For example, for example:

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

This is the complete code for an example:

 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); 

Using this result:

example

+5
source share

You forgot $

 legend({'Pos1', 'Pos2', 'Pos3', '$\alpha$ Pos4'}, 'Location', 'northeast', 'Interpreter', 'latex', 'fontsize', 22); 
+6
source share

All Articles