MATLAB 中的 LaTeX multi-line 标题

LaTeX in MATLAB multi-line titles

我需要在 MATLAB 绘图中创建一个 two-line 标题,在每一行中使用 LaTeX。

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

有效,但不适用于 LaTeX。在单行 MATLAB 标题中,LaTeX 理解为示例中的:

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

我已经尝试了很多东西,但我还没有设法让 MATLAB 在这些行中用 LaTeX 生成 multi-line 标题。

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

您可以将标题包装在 LaTeX 表格环境中:

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

这会让您选择文本对齐方式。将 {c} 替换为 {r}{l},分别用于右对齐和左对齐文本。

您可以使用 sprintf to create the string for the title,明确地使用换行符,'\n'

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

如果你运行

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

您将获得 two-line 正确 LaTeX-ification 的标题。