How to obtain legend with tex equation and the value of an output variable?

3 ビュー (過去 30 日間)
Hi, I am running R2018b and trying to obtain a nice subplot with tex interpreted legend, which also have to display an output variable XAbsFinal
Here is my piece of code:
subplot(3,2,2)
hold on, grid on
pl1=line(1:n, XAbs, 'Color', 'b');
pl2=line(l(1,end), X(1,end),'Color', 'b', 'Marker', 's', 'MarkerSize', 8, 'LineWidth', 2);
set(gca, 'XAxisLocation','top','YAxisLocation','left','XLim',[0 n]);
leg1=legend([pl1,pl2], {'$\Delta|X_{E}|$', sprintf('$\Delta|X_{E}|_{final}$ at %0.4f [m]$ ',XAbsFinal), 'Interpreter', 'tex'})
set(leg1,'Location', 'NorthWest', 'Interpreter','latex','FontSize', 10);
ay = gca;
ay_pos=ay.Position; %position of first axes
ay.YRuler.Exponent = 0;
ay.XRuler.Exponent=0;
ylabel (ay, 'Extremum [m]');
text(-0.1,1.15,'d).','Units', 'Normalized', 'VerticalAlignment', 'Top');
ay2=axes('Position',ay_pos, 'XAxisLocation','bottom','YAxisLocation','left','Color', 'none', 'XLim',[0 n_days]);
ay2.YRuler.Color='none';
And here is the result:
I'd like to have \Delta|X_{E}|_{final} = XAbsFinal as the second piece of my legend, but I feel like I am missing something concerned the usage of sprintf and tex syntax, while first part of the line
leg1=legend([pl1,pl2], {'$\Delta|X_{E}|$', sprintf('$\Delta|X_{E}|_{final}$ at %0.4f [m]$ ',XAbsFinal), 'Interpreter', 'tex'})
works fine and provide me with an expected notation for the first piece of my legend.
Thank you in advance for your help!
Cheers,
Hlib

採用された回答

Walter Roberson
Walter Roberson 2019 年 5 月 13 日
leg1=legend([pl1,pl2], {'$\Delta|X_{E}|$', sprintf('$\\Delta|X_{E}|_{final}$ at %0.4f [m]$ ',XAbsFinal), 'Interpreter', 'tex'})
You missed that \ is a special character for sprintf purposes. See "Text Before or After Formatting Operators in the sprintf documentation. \a, \b, \f, \n, \r, \t, \v, \x####, \### all having defined meanings.
  1 件のコメント
Hlib Cheporniuk
Hlib Cheporniuk 2019 年 5 月 13 日
Hi Walter,
Thanks a lot for such a promt reply! Devil is in the details. Now it works perfectly fine.
Cheers,
Hlib

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeTitle についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by