Fontname switches to Times New Roman in ylabel('$\hat x_2$: Height' , 'Interpreter' , 'latex')

clear
clc
x=0:0.1:10;
y=sin(x);
figure(1)
plot(x,y)
xlabel('Time')
ylabel('$\hat x$: Height' , 'FontName', 'Helvetica', 'Interpreter' , 'latex')
I am trying to get ylabel in default Helvetica font.
Any help will be very much appreciated.
Thanks
-Saroj

Answers (1)

The builtin LaTeX interpreter is very limited in its abilities and since can only do things inline, it's not possible to add packages or other fonts. You'll note that the documentation explicitly notes that all the font-related properties are totally ignored when choose the 'Interpreter','latex' option This is true for labels, legends, titles, text ... LaTeX is the red-haired stepchild that gets no love nor respect...
The \hat syntax surrounds the character with {} like a cell
x=0:0.1:10;
y=sin(x);
subplot(2,1,1)
plot(x,y)
xlabel('Time')
ylabel('$\hat{x_2}$' , 'Interpreter' , 'latex')
One should be able to use a sans serif font similarly...but apparently no such luck.
subplot(2,1,2)
plot(x,y)
ylabel('$\textsf{Height}$' , 'Interpreter' , 'latex')
Warning: Error in state of SceneNode.
Font cmss10 is not supported.
return
AFAIK, the only likely candidates are those from <Table 6.1 6.2 in the LaTeX doc> -- the link takes you to the opening page, the relevant section is in Chap 6.
This has been a big hassle "since forever" and afaik nobody has ever found any working syntax.
In short, if you use LaTeX interpreter, you're pretty much at the mercy of trial and error to see what, if anything, beyond straight text you can get to work.

5 Comments

I am trying to get both math and text in one figure rather than two.
You can string stuff together as you wish, but there's no way to use another font and stick with LaTeX interpreter.
I just used the two figures to illustrate with the two pieces the problem with the sans serif font independently of the hat over character illustration so there wouldn't be an error on it.
I could get the following to work with the \textup font, but could find no way to nest the two or concatenate multiple strings in one call...
ylabel('$\hat{x_2}: Height$', 'Interpreter' , 'latex')
str=strcat('$\hat{x_2}$', ' $\textup{: Height}$'); % two substring, not one
str = '$\hat{x_2}$ $\textup{: Height}$'
xlabel(str, 'Interpreter' , 'latex') % show both on same plot for brevity
ylabel('$\textup{\hat{x}}$', 'Interpreter' , 'latex')
nor any other perturbations that I could think of would not work although the above was suggested by the AI 'bot. It probably would work in a standalone LaTeX document, but crashes and burns in MATLAB.
You can control the font a slight bit by placing items outside of math mode. However, it looks like even if you do that, that the FontName is not paid attention to
text(0.1, 0.1, '$\hat{x_2}:$ Height ijxyz', 'Interpreter' , 'latex', 'FontName', 'Helvetica')
text(0.1, 0.2, '$\hat{x_2}:$ Height ijxyz', 'Interpreter' , 'latex', 'FontName', 'Times New Roman')
text(0.1, 0.3, '$\hat{x_2}: Height\,ijxyz$', 'Interpreter' , 'latex', 'FontName', 'Helvetica')
Thanks for the lead to split it into two. I am able to resolve the issue with two commands:
text(-1, 0.1, '$\hat{x_2}:$', 'Interpreter' , 'latex','Rotation',90)
text(-1, 0.25, 'Height', 'FontName', 'Helvetica', 'Rotation',90)
Basically, these are two separate strings, one with a LaTeX command and the other is standard. Thanks everyone for looking into it. Hope others will find it useful.
"... even if you do that, that the FontName is not paid attention to"
Yeah, this is explicitly documented in the details under 'Interpreter'
"The displayed text uses the default LaTeX font style. The FontName, FontWeight, and FontAngle properties do not have an effect. To change the font style, use LaTeX markup."
As I noted earlier, the above is in all places -- x|ylabel, title, legend, text, any else I'm forgetting.
Unfortunately, as noted above also, even many of the LaTeX markups don't work or at least not as expected.

Sign in to comment.

Categories

Find more on Scripts in Help Center and File Exchange

Products

Release

R2024b

Asked:

on 16 Feb 2026

Commented:

dpb
on 16 Feb 2026

Community Treasure Hunt

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

Start Hunting!