How do I center a double-lined diagram title, interpreted as LaTex?
조회 수: 18 (최근 30일)
이전 댓글 표시
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
Robert U
2017년 9월 28일
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;
title(ah,str_title,'interpreter','latex',...
'HorizontalAlignment', 'center');
hold on
bh = axes;
title(bh,str_title,'interpreter','latex',...
'HorizontalAlignment', 'left', 'color', 'green');
ch = axes;
title(ch,str_title,'interpreter','latex',...
'HorizontalAlignment', 'right', 'color', 'blue');
Kind regards,
Robert
답변 (2개)
Benjamin Getraer
2018년 1월 3일
편집: Benjamin Getraer
2018년 1월 4일
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.
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}',...
'interpreter','latex')
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),...
'interpreter','latex')
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),...
'interpreter','latex')
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:
Centered
\begin{tabular}{c}
Right aligned
\begin{tabular}{r}
Left aligned
\begin{tabular}{l}
댓글 수: 0
noahop
2018년 4월 5일
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}"
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Graphics Object Properties에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!