Greetings...

You might also be interested in my research website as well...

Friday, May 4, 2012

LaTeX labels in Matlab and Octave

Very happy to've finally discovered the LaTeX interpreter option in Matlab labels this evening.  Previously I'd been so frustrated when wanting to use something mathy like a partial derivative expression to label a plot axis, because while I can specify a few LaTeX-like things in there like partials and sub/superscripts:


xlabel('\partial{P_k}(t_i)/\partial{S}_{p}(z_j)');


it comes out looking kinda lame like this:
I mean, it's not totally unusable (been using it that way for years in fact!), but it's all scrunched up against the axis numbers and the subscript spacing is screwy and the relative sizes of the partials and variables is lousy; just not very professional looking.  Octave (awesome free GNU clone of Matlab) which I often use does better with that same unmodified xlabel line:
but it's still not ideal.

Turns out in Matlab you can modify that label statement and call its built-in LaTeX interpreter like so:


xlabel( '$\partial{P_k}(t_i)/\partial{S}_\mathrm{p}(z_j)$', 'Interpreter', 'latex' );


The flanking $'s denote math-mode to the LaTeX interpreter, and note added in the \mathrm{} to improve that p subscript, which you couldn't use above since it wasn't really LaTeX handling the above.  Anyway, with LaTeX now it looks like this:

Much better!  I did find a little remaining funniness - that result there was after having first specified a 14pt font size on the axes text via:

set(0,'DefaultAxesFontSize',14); 

But using the LaTeX interpreter for xlabel with the original default fontsize had some subscript spacing issues (k subscript touching paren and wide space before the p subscript):


As far as an equivalent in Octave, according to the latest in the user manual under Section 15.2.8, "Use of the interpreter Property", that 'latex' interpreter option isn't implemented yet (although the hook is there).  The 'tex' interpreter option that's mentioned in that section is just the bit that was shown at the top of this post, which Octave does do a little better than Matlab but it's pretty limited.  There are a few webpages I've seen (e.g. here and here) about how to output an EPS plot file from Octave and then pass it through latex with a specially written .tex file to use the full interpreter that way.  But that too much effort for me.  Meanwhile I'm just happy I've figured out the LaTeX use in Matlab at least.

No comments:

Post a Comment