Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LaTeX in MATLAB multi-line titles

I need to create a two-line title in a MATLAB plot, using LaTeX in each of the lines.

title({'first line','second line'})

works, but not with LaTeX. In a single line MATLAB title, LaTeX is understood as in the example:

title(['$y=x^2$'],'interpreter','latex')

I have tried many things, but I haven't managed to have MATLAB produced a multi-line title with LaTeX in those lines.

like image 582
Yiorgos S. Smyrlis Avatar asked Nov 06 '15 10:11

Yiorgos S. Smyrlis


People also ask

How do you write multiple lines in Matlab?

To enter multiple lines before running any of them, use Shift+Enter or Shift+Return after typing a line. This is useful, for example, when entering a set of statements containing keywords, such as if ... end. The cursor moves down to the next line, which does not show a prompt, where you can type the next line.

How do I add a new line to a title in Matlab?

This is done by separating each string line of text with a comma and enclosing all comma-separated strings in curly braces as follows. The 10 outside the single quotes is the ascii value for a newline.


2 Answers

If you run

title({'$y=x^2$','$y=x^2$'},'interpreter','latex')

you will get a two-line title with correct LaTeX-ification.

like image 114
gariepy Avatar answered Nov 10 '22 11:11

gariepy


Up to version R2017a, using a cell array, as suggested by other answers, forced left alignment. This seems to have been fixed in R2017b.

You can wrap the title in a LaTeX tabular environment:

figure;
plot((1:5).^2);
title('\begin{tabular}{c} first_line \\ second_line \end{tabular}', ...
      'interpreter', 'latex')

This will let you choose text alignment. Replace {c} with either {r} or {l}, for right and left aligned text, respectively.

like image 42
mikkola Avatar answered Nov 10 '22 10:11

mikkola