Within a legend, how do I modify where the text is relative to the symbol?

10 views (last 30 days)
If you put a subscript within your text string in a legend, it lowers the symbol or line to the left of the text. For example, in the following code:
figure(1)
x = -10:0.1:10;
y = x.^2;
j=plot(x,y,'k-');
axis([-10 10 -2 110]);
legend('T_{experiment}')
Here, the black line to the left of "T_experiment" is at the bottom of the "T", and I would like it centered on the "T". I have tried adding an empty superscript to raise the symbol, but superscripts do not alter the symbol position. Is there a way to solve this problem?
Thanks!!

Answers (3)

Azzi Abdelmalek
Azzi Abdelmalek on 25 Sep 2013
Edited: Azzi Abdelmalek on 26 Sep 2013
That's what _ do, use two _ _
legend('T__{experiment}')

Jan
Jan on 26 Sep 2013
Do I understand correctly: The problem is not the sub-scripting in the TeX string, but the relative position to the the symbol of the line style?
Then you could try to modify the text properties manually:
[legend_h, object_h] = legend(...)
text_h = findobj(object_h, 'flat', 'type', 'text'); % Does this work?
% Alternatively:
% typeList = get(object_h, 'Type');
% text_h = object_h(strcmpi(typeList, 'text'));
set(text_h, 'VerticalAlignment', 'baseline'); % Or 'middle'

Radhika
Radhika on 27 Mar 2023
Edited: Radhika on 28 Mar 2023
Seeing this question is still active, here is what worked for me (Matlab R2022a).
Instead of modifying the legend properties, I put the legend entries in latex form and set the interpreter to Latex as in the first line of code.
The resulting "math font" is italics. To have the legend in normal face, I used 'mathrm' as in the second line of code
legend('$T_{experiment}$', 'Interpreter', 'Latex')
legend('$\mathrm{T_{experiment}}$', 'Interpreter', 'Latex')

Products

Community Treasure Hunt

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

Start Hunting!