How to change the fontname of axes labels when using the latex interpreter

450 views (last 30 days)
I am using the latex interpreter for my axes labels as opposed to tex because the subscripts look a whole lot better with the latex interpreter. However, I would like the font to look like Times New Roman, as it does when specifying 'fontname' to 'Times' with the tex interpreter. I'm using Matlab 2012a.
I've done a lot of searching - I haven't been able to find a solution to either the ugly subscripts with tex that actually looks good or a way to (easily...) changing fonts with the latex interpreter. Solutions must exist somewhere, I would appreciate any insights for either of these issues!

Accepted Answer

dpb on 19 Jan 2017
Edited: dpb on 22 Jan 2017
Well, when you change to the LaTeX interpreter from TeX then (as you've learned) fontname and all the similar properties are no longer in effect; the only way is to use LaTeX commands embedded in the string sent text or xlabel or whoever.
And, while TMW provided a modicum of basic information and syntax for TeX, the only thing they provide on the LaTeX side is a link to the LaTeX project home page and that leads into a morass of links into which I've never been able to find an answer to anything in short that is helpful for the casual user that just wants, as you, a quick fix answer to a basic question. AFAICT, there is no such thing as that extant; everything seems tied to everything else and makes, it appears, assumptions that one is already pretty fluent.
Obviously, there must be "LaTeX for Dummies" somewhere, but the need has never been to the level of forcing me to figure it out so I've just always taken the coward's way out and just accepted what I got with a very few exceptions of being able to copy and modify some examples for an equation or two. Searching at has occasionally been helpful on those forays in the past.
Wonder if Yair has anything on using LaTeX inside Matlab at his "undocumented Matlab" site. Unfortunately the exact URL has escaped me at the moment...
OK, I still think it's terribly difficult to wade through the forest for the trees, but thanks to Star's link, try something like
hT=text(0.5,0.5,'\textrm{This is Roman?}','interpreter','latex');
Seems to work here. I knew were some shorthands defined for common fonts but didn't know what the syntax is for them.
I'm sure it's possible to fix defaults up by delving into the TMW installation but that's far beyond my pay grade... :)
Thanks again to Star for the link to Yair's site, a search there uncovers a page I'd forgotten having visited years ago on similar quest for "how to" do something w/ LaTeX inside Matlab. Anyways, there's a long discussion with some possible workarounds but nothing appears fully satisfactory and this page is now somewhat dated (ca 2013). Undocumented LaTeX interface
All in all, my take is unless this is absolutely mandatory it's not likely going to be worth the effort although if you were to find the magic solution could become a rock star! :)
The one thing in that page that is relatively new is a workaround of saving the figure as eps and then editing it to make the font changes internally there which apparently will let you use any TTF on your system. This, of course, is another step that would be tough to automate (albeit perhaps not impossible).
Just couldn't quite get it out of my mind...reading further in the doc, I came to learn that unlike my thinking that LaTeX was/is a newer, enhanced TeX, I see based on some comments of the authors that really its intent is to be a higher-level abstraction than TeX for which authors are supposed to not have to consider details such as this; the templates are supposed to be able to create the document basically on their own.
Hence, rather than being a lower level with more flexibility than TeX, one can conclude there's less and indeed the restriction on fonts to a smaller class seems to bear this out. It seem that making only a general set of three or so fonts available is a deeply-rooted design decision and that the effort to expand those is pretty sizable. So, appears that unless you find a package already built that has these additional fonts prepared to be imported (probably around somewhere but I've no klew as to where that would be) you'd have to build them yourself. Then, even if get that far, you'll be left with the apparent difficulty of actually figuring out the TMW interface to make them accessible to the Matlab LaTeX engine which stumped the folks at the link given above, or use the alternative of installing and using a standalone installation. That has the issues it appears of being able to then relate to the actual locations on Matlab figures which I can imagine isn't the simplest thing to accomplish, hence the TMW modifications to "how it works" described therein.

More Answers (3)

Cosmin Constantin Popescu
Cosmin Constantin Popescu on 10 Sep 2018
Edited: Cosmin Constantin Popescu on 10 Sep 2018
So here is an example based on the information provided by Star in his link, from page 109 about fonts and sizes. This example works in MATLAB. It should give you stuff in Roman font, Sans Serif, and Italic. Otherwise, follow normal LaTeX and MATLAB format.
legend({'$\mathrm{text here}}$','$\mathsf{text here}$','$\mathit{text here}$'});

Star Strider
Star Strider on 20 Jan 2017
The LaTeX documentation has a section on Fonts and Sizes. I have no idea how this works in MATLAB, since I’ve always used the default font, but it promises a limited selection. It doesn’t seem possible to use any font you have installed. You may only be able to use those available in LaTeX.
Yair Altman’s Undocumented MATLAB link.
  1 Comment
dpb on 20 Jan 2017
That's a better link than I found, Star, guess I started searching for font too far up...

Sign in to comment.

Shannon on 7 Mar 2017
Thanks for the comments. I wouldn't say that I have a "solid" solution to this issue. I've managed to make some labels that I can tolerate using the regular tex editor and by reducing the fontsize of what would be the subscript, e.g:
ylabel('H{\fontsize{9} 2}O', 'fontname', 'Times', 'fontsize', 14);
  1 Comment
dpb on 7 Mar 2017
It's a nightmare inside Matlab, granted. :( If those who actually know something about TeX/LaTeX can't do it trivially, there's little hope for the casual user to make use of much more than the bare basics.

Sign in to comment.


Find more on Migrate GUIDE Apps in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!