Filter löschen
Filter löschen

How do I center a double-lined diagram title, interpreted as LaTex?

14 Ansichten (letzte 30 Tage)
Ulrik William Nash
Ulrik William Nash am 27 Sep. 2017
Beantwortet: noahop am 5 Apr. 2018
Why does the following not center the first line of the double-lined diagram title?
str_title = {'Line 1';'Line 2'}; title(str_title,'interpreter','latex','HorizontalAlignment', 'center');
  1 Kommentar
Robert U
Robert U am 28 Sep. 2017
Hi Ulrik William Nash:
Please, provide a figure of your result. It is not possible to reproduce what you describe.
Varying the 'HorizontalAlignment'-property leads to different positions of the title strings as expected:
str_title = {'Line 1';'Line 2'};
fh = figure;
ah = axes;
'HorizontalAlignment', 'center');
hold on
bh = axes;
'HorizontalAlignment', 'left', 'color', 'green');
ch = axes;
'HorizontalAlignment', 'right', 'color', 'blue');
Kind regards,

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Benjamin Getraer
Benjamin Getraer am 3 Jan. 2018
Bearbeitet: Benjamin Getraer am 4 Jan. 2018
The LaTex interpreter will automatically left-align the text block, and the 'horizontalalignment' argument will align the entire block instead of each line individually. The alignment has to be set INSIDE of the LaTex environment.
I found a solution HERE that uses the LaTex tabular environment.
An example of the problem:
An example of the solution using the tabular environment:
The code looks like this:
title('\begin{tabular}{c} The first line \\ Followed by another line \end{tabular}',...
For readability it could be translated to sprintf() format like this:
title_text = 'The first line \\ Followed by another line';
title(sprintf('\\begin{tabular}{c} %s \\end{tabular}',title_text),...
Or broken down like this:
line1 = 'The first line';
line2 = 'Followed by another line';
title(sprintf('\\begin{tabular}{c} %s %s %s \\end{tabular}',line1,'\\',line2),...
I think that breaking it down like this last example is the most clear, especially if the code for the text is especially long.
NOTE: other alignments can be achieved as well:
Right aligned
Left aligned

noahop am 5 Apr. 2018
I had to add two \\ commands in a row (between the two lines) to get this to work on Matlab R2017a.
So for me it became:
"\\begin{tabular}{c} %s \\\\ %s \\end{tabular}"

Community Treasure Hunt

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

Start Hunting!

Translated by