TeX (LaTeX math mode) symbols in legends and labels in MATLAB figures
조회 수: 4,121 (최근 30일)
이전 댓글 표시
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. Shah
댓글 수: 0
채택된 답변
Matthew Simoneau
2024년 11월 13일 0:00
편집: MathWorks Support Team
2024년 11월 13일 4:33
이 답변에 Walter Roberson
님이 플래그를 지정함
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: title('$\hat{\psi}$','Interpreter','latex') 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. plot(1:10) 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
댓글 수: 4
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.
Walter Roberson
2024년 11월 13일 5:20
You cannot use two different interpreters in a single call.
The key to getting Roman font with LaTex is to use \textrm or \mathrm
text(0.5, 0.8, '\textsf{sans serif}','interpreter','latex')
text(0.5, 0.7, '\textrm{roman}','interpreter','latex')
text(0.5, 0.6, '$$\mathsf{math\,\,mode\,\,sans\,\,serif}$$','interpreter','latex')
text(0.5, 0.5, '$$\mathrm{math\,\,mode\,\,roman}$$','interpreter','latex')
추가 답변 (6개)
Mikhail Smirnov
2017년 2월 10일
You can do like this
plot(1:10);
leg1 = legend('$\bar{x}$','$\tilde{x}$','$\hat{x}$');
set(leg1,'Interpreter','latex');
set(leg1,'FontSize',17);
works well
댓글 수: 4
Mario
2023년 4월 17일
Unfortunately this doesn't work for me.
I am tried using annotations and legends to write v_{mean on} as v_on to save figure space.
I tried several things:
lgd = legend{'on', 'off', '$$\bar{\itv}$$_{on}', 'v_{mean off}'}
lgd = legend{'on', 'off', '$\bar{\itv}$_{on}', 'v_{mean off}'}
lgd = legend{'on', 'off', '$\bar{v}$_{on}', 'v_{mean off}'}
lgd = legend{'on', 'off', ['$\bar{v}$','_{on}'], 'v_{mean off}'}
or
lgd = legend{'on', 'off', '$$\overline{\itv}$$_{on}', 'v_{mean off}'}
lgd = legend{'on', 'off', '$\overline{\itv}$_{on}', 'v_{mean off}'}
lgd = legend{'on', 'off', '$\overline{v}$_{on}', 'v_{mean off}'}
lgd = legend{'on', 'off', ['$\overline{\itv}$','_{on}'], 'v_{mean off}'}
won't work.
It would just add the $$\overline... to the legend text.
I tried both latex and tex interpreter.
I am using MATLAB v 2018b
Mario
2023년 4월 17일
편집: Mario
2023년 4월 17일
I figured it out:
lgd = legend('on','off', '$\overline{v}_{on}$', '$\overline{v}_{off}$')
But now my font is not Calibri anymore. it's rather looking like Times New Roman or sth.
set(lgd, 'interpreter', 'latex', 'FontName', 'Calibri')
won't help :(
is there a problem with Calibri + Latex?
Frederik van der Walt
2012년 4월 4일
편집: KSSV
2022년 5월 26일
l= legend(['$\hat{\psi}$']);
set(l,'Interpreter','Latex');
댓글 수: 0
Steven
2011년 11월 22일
Try with Latex interpreter, something like
title(['$ latex $'],'interpreter','latex')
댓글 수: 2
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.
댓글 수: 1
Rostislav Lebedev
2022년 3월 16일
Try this
title('Verkehrstr\"ager', 'Interpreter', 'latex');
Richard Ulbricht
2022년 12월 13일
Put this in front of your plotting commands, if you want it for all plots:
set(groot,'defaultLegendInterpreter','latex');
참고 항목
카테고리
Help Center 및 File Exchange에서 Annotations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!