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

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

 採用された回答

Matthew Simoneau
Matthew Simoneau 2024 年 11 月 13 日
編集済み: MathWorks Support Team 2024 年 11 月 13 日

24 投票

この 回答 は Walter Roberson さんによってフラグが設定されました
  • Walter Roberson さんによって 2024 年 11 月 13 日 にフラグが設定されました。

    needs reformatting

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 件のコメント

Myrthe Breedijk
Myrthe Breedijk 2021 年 2 月 3 日
編集済み: Myrthe Breedijk 2021 年 2 月 3 日
I am using this information to plot my legend. However, I want to have multiple labels in the legend and now it only displays the last input of the legend. I am working with Matlab R2014b. Can you help me how to fix this?
figure
plot(t_two2,edim_two2,t_two3,edim_two3,t_two4,edim_two4,t_two5,edim_two5,t_two6,edim_two6,'LineWidth',4);
xlabel('t')
ylabel('$\displaystyle e^{*}=\frac{N^{2}}{b_{0}^{2}w\mathcal{L}}e$','interpreter','latex')
legend({'$\Pi_{5}=60$'},{'$\Pi_{5}=30$'},{'$\Pi_{5}=20$'},{'$\Pi_{5}=50$'},{'$\Pi_{5}=70$'},'Interpreter','latex','Location','southeast');
title('Kinetic energy','interpreter','latex')
FontSize = 36;
ax = gca;
ax.FontSize = 24;
SOURAV KUMAR
SOURAV KUMAR 2021 年 3 月 29 日
You should use the following syntax for legend() :
legend({'$\Pi_{5}=60$','$\Pi_{5}=30$','$\Pi_{5}=20$','$\Pi_{5}=50$','$\Pi_{5}=70$'},'Interpreter','latex','Location','southeast');
It works on my system (Matlab R2013a),
check on yours
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.
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')
Warning: Error in state of SceneNode.
Font cmss10 is not supported.

サインインしてコメントする。

その他の回答 (6 件)

Mikhail Smirnov
Mikhail Smirnov 2017 年 2 月 10 日

12 投票

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 件のコメント

Walter Roberson
Walter Roberson 2017 年 9 月 1 日
Ahmad Alsharif comments to Mikhail Smirnov:
This helped me in creating multi line legend with latex as interpreter
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
Mario
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
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
Frederik van der Walt 2012 年 4 月 4 日
編集済み: KSSV 2022 年 5 月 26 日

3 投票

l= legend(['$\hat{\psi}$']);
set(l,'Interpreter','Latex');
Steven
Steven 2011 年 11 月 22 日

0 投票

Try with Latex interpreter, something like
title(['$ latex $'],'interpreter','latex')

2 件のコメント

Shah
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.
Leonardo
Leonardo 2024 年 1 月 28 日
Is there a way to have text and latex together?

サインインしてコメントする。

Shah
Shah 2011 年 11 月 23 日

0 投票

......... 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 日

0 投票

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
Rostislav Lebedev 2022 年 3 月 16 日
Try this
title('Verkehrstr\"ager', 'Interpreter', 'latex');

サインインしてコメントする。

Richard Ulbricht
Richard Ulbricht 2022 年 12 月 13 日

0 投票

Put this in front of your plotting commands, if you want it for all plots:
set(groot,'defaultLegendInterpreter','latex');

1 件のコメント

Yan Gao
Yan Gao 2023 年 7 月 4 日
This is the best solution! Thanks, Richard!

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeGraphics Object Properties についてさらに検索

タグ

タグが未入力です。

Community Treasure Hunt

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

Start Hunting!

Translated by