TeX (LaTeX math mode) symbols in legends and labels in MATLAB figures

조회 수: 6,665(최근 30일)
Shah 2011년 11월 22일
댓글: Bokang Zhou 2021년 8월 14일
I am plotting some data which includes estimates of some parameters. I want to describe the estimates by using \hat on the parameter symbols. This I have to do in legends and axis labels fields. I could not fix this matter despite some experiments using information from MATLAB help. Any idea, please to fix this problem.
I really appreciate help on this matter which will improve the look of my presentation.

채택된 답변

Matthew Simoneau
Matthew Simoneau 2014년 5월 30일
편집: MathWorks Support Team 2021년 4월 28일
By default, MATLAB supports a subset of TeX markup. For a list of supported symbols, see the documentation .
For more symbols, you can use LaTeX markup by setting the Interpreter property to 'latex'. Use dollar symbols around the text. For example:
If you are using the legend function in R2018a or earlier, you must specify the labels as a cell array to distinguish the labels from the name-value pairs. In R2018b and later, the cell array is not needed.
legend({'$\hat{\psi}$'},'Interpreter','latex') % R2018a and earlier
legend('$\hat{\psi}$','Interpreter','latex') % R2018b and later
When you use LaTeX markup, the displayed text uses the default LaTeX font style. The FontName, FontWeight, and FontAngle properties no longer have an effect. To change the font style, use LaTeX markup instead.
For more information on using LaTeX in plot titles, labels, and legends, refer to the following example:https://www.mathworks.com/help/matlab/creating_plots/greek-letters-and-special-characters-in-graph-text.html#mw_421aadf2-3104-41f5-ae7e-57bf5f7cdde3
  댓글 수: 3
Bokang Zhou
Bokang Zhou 2021년 8월 14일
Thank you for your answer, but how do I use two different interpreters in the same label line?
For example:
If I use 'latex', I get the desired symbol \varphi but can't get the font of words in Time New Roman.
xlabel('Power factor angle, $\varphi$','Interpreter','latex')
If I use 'tex', I get the desired font of words in Time New Roman, but can't get the symbol \varphi since 'tex' does not support \varphi.
xlabel('Power factor angle, \phi','Interpreter','tex')
So I wonder if there is a way to have normal font of words and 'latex' font of symbol in one label line.

댓글을 달려면 로그인하십시오.

추가 답변(5개)

Mikhail Smirnov
Mikhail Smirnov 2017년 2월 10일
You can do like this
leg1 = legend('$\bar{x}$','$\tilde{x}$','$\hat{x}$');
works well
  댓글 수: 2
Thomas Gillet
Thomas Gillet 2017년 12월 17일
Hello Everyone,
When I use Latex interpreter, I am not able to change the font of the legend as you mentionned with set(leg1,'FontSize',17); Have you any idea ? Thank's, Thomas

댓글을 달려면 로그인하십시오.

Frederik van der Walt
Frederik van der Walt 2012년 4월 4일
l= legend(['$\hat{\psi}$']);

Steven 2011년 11월 22일
Try with Latex interpreter, something like
title(['$ latex $'],'interpreter','latex')
  댓글 수: 1
Shah 2011년 11월 22일
Thanks Steven but its not working here. I am using MATLAB 7.9.0.
I wrote legend(['$\hat{\psi}$'],'interpreter','latex') but got a warning: Unable to interpret TeX string '$\hat{\psi}$'interpreterlatex'. Same appear in the legend field.

댓글을 달려면 로그인하십시오.

Shah 2011년 11월 23일
......... Its done! Actually, the default interpreter in MATLAB for legend is 'tex', I guess. It is not changed from the code line. What I had to do was to right click on the legend in the figure window and then changed the 'interpreter' from 'tex' to 'latex'. This action changed the latex statement in the legend field to Math mode.
Cheers Shah

Jordan Brel Ngako Kadji
Jordan Brel Ngako Kadji 2020년 5월 7일
Once I had this problem, I was writing in German and because the german letter 'ä' was not recognised all the latex commands were ignored.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by