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

1 vue (au cours des 30 derniers jours)
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

Réponse acceptée

Walter Roberson
Walter Roberson le 13 Mai 2019
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 commentaire
Hlib Cheporniuk
Hlib Cheporniuk le 13 Mai 2019
Hi Walter,
Thanks a lot for such a promt reply! Devil is in the details. Now it works perfectly fine.
Cheers,
Hlib

Connectez-vous pour commenter.

Plus de réponses (0)

Produits


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by