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

2 次查看(过去 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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Title 的更多信息

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by