Matlab legend text overflow using Latex interpreter

I am trying to include a legend in my Matlab graph that uses the Latex interpreter for the legend text.

When I set up a legend to use the Latex interpreter, the text inside overflows outside the legend field.

I tried to adjust the text size, but this happens regardless of FontSize.

Below is the relevant part 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 assume this is because Matlab does not account for the correct character size after the text has been interpreted using Latex. However, I do not know how to fix it.

Any insight would be greatly appreciated! Thanks!

+8
matlab plot latex matlab-figure
source share
3 answers

I have no problem with your code in R2007b if I remove [h1 h2 h3] (and a comma) from your first statement. However, the change in line width in the legend disappears after calling the latex interpreter or after installing FontSize, so I had to switch these 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 
+1
source share

Another way is to change the font size before you set the default interpreter in LaTex. I have had this job in the past, and note that the answer above me does this too.

0
source share

I never found a perfect solution for this (i.e. MATLAB automatically gets a field for LaTeX).

However, the following subtle fiction: add extra space by adding the characters ~ --- LaTeX interpreter hides them, but they are included in the calculation of the width.

Add ~ to LONGEST LINE of LEGEND to expand the width of the window

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

Play with him and see how many tildes you need to add to tweak a lot.

0
source share

All Articles