Yiorgos S. Smyrlis
Yiorgos S. Smyrlis

Reputation: 163

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.

Upvotes: 6

Views: 4230

Answers (3)

mikkola
mikkola

Reputation: 3476

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.

Upvotes: 4

gariepy
gariepy

Reputation: 3674

If you run

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

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

Upvotes: 7

IKavanagh
IKavanagh

Reputation: 6187

You can use sprintf to create the string for the title, explicitly with a newline character, '\n'.

title(sprintf('$y=x^3$\n$sin(x)$'), 'interpreter', 'latex');

Upvotes: 1

Related Questions