Matlab legend text overflows using Latex interpreter
Asked Answered
P

3

8

I am trying to include a legend within my matlab plot which uses the Latex interpreter for the legend text.

When I set the legend to use the Latex interpreter though, the text within overflows outside the legend box.

I have tried adjusting the size of the text, but this occurs regardless of the FontSize.

The following is the relevant portion of my script:

I = legend([h1 h2 h3],'RainFall Flux', ... 
           'Temperature term ($$\rho \alpha$$dT)', ...
           'Salinity term ($$\rho \beta$$dS)');
c=get(I,'children');
set(c(5),'LineWidth',3); %adjust lineWidth in legend
set(c(2),'LineWidth',3); %adjust lineWidth in legend
set(I,'interpreter','latex'); %set Latex interpreter
set(I,'FontSize',15);

I am assuming this is because matlab is not taking into account the proper character size after the text has been interpreted using Latex. However, I don't know how to fix this.

Any insight would be much appreciated! Thanks!

Psychometrics answered 20/9, 2013 at 18:40 Comment(0)
A
1

I don't have any problem with your code, in R2007b, if I remove [h1 h2 h3] (and the comma) from your first statement. However, the change in linewidths in the legend disappears after calling the latex interpreter, or after setting FontSize, so I had to switch those orders. In other words, this code works:

x=[1:100]; y=sin(pi*x/50); plot(x,y,x,y.^2,x,sqrt(abs(y)));
I = legend('RainFall Flux', ... 
           'Temperature term ($$\rho \alpha$$dT)', ...
           'Salinity term ($$\rho \beta$$dS)');
c=get(I,'children');
set(I,'interpreter','latex'); %set Latex interpreter
set(I,'FontSize',15);
set(c(5),'LineWidth',3); %adjust lineWidth in legend
set(c(2),'LineWidth',3); %adjust lineWidth in legend
Aboral answered 20/9, 2013 at 19:34 Comment(0)
M
0

Another way around it is to change the font size before you have set the default interpreter to LaTex. I have had that work in the past and notice that the answer above me is actually doing that too.

Moisture answered 17/12, 2014 at 20:36 Comment(0)
O
0

I've never found a perfect fix for this (ie. MATLAB automatically getting the box correct for LaTeX).

However the following is a fine fudge: force extra space by adding ~ characters --- the LaTeX interpreter hides them but they are included in the width calculation.

Add ~s to the LONGEST LINE OF THE LEGEND to extend the width of the box

I = legend([h1 h2 h3],'RainFall Flux', ... 
       'Temperature term ($$\rho \alpha$$dT)~~~', ...
       'Salinity term ($$\rho \beta$$dS)');

Play about with it and see how many tildes you need to add to force a nice fit.

Okay answered 21/2, 2017 at 16:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.