필터 지우기
필터 지우기

sprintf in 2 lines, with 2 variables, in bold, and interpreter latex

조회 수: 23 (최근 30일)
Sim
Sim 2024년 4월 23일
댓글: Sim 2024년 4월 23일
In the following example,
plot(1:10,1:10)
alpha = 3;
string = 'Hello';
text(5,5,sprintf('$\\textbf{%s}$ {\\boldmath$\\alpha=%d$}',string,alpha),'interpreter','latex','FontSize',20)
how can I bring the text corresponding to
'{\\boldmath$\\alpha=%d$}',alpha
to a second line, i.e. below the text "Hello"?
  댓글 수: 3
Dyuman Joshi
Dyuman Joshi 2024년 4월 23일
편집: Dyuman Joshi 2024년 4월 23일
"how can I bring the text corresponding to a second line, i.e. below the text "Hello"?"
"is there a way to reduce the space between the two lines of text?"
The space in between the lines is proportional to the font-size.
If you can't change the fontsize for any particular reason, you will have to adjust the space manually by calling text() twice.

댓글을 달려면 로그인하십시오.

채택된 답변

Lokesh
Lokesh 2024년 4월 23일
편집: Lokesh 2024년 4월 23일
Hi Sim,
It is my understanding that you would like to bring the text corresponding to "'{\\boldmath$\\alpha=%d$}',alpha" to a second line, i.e. below the text "Hello".
If the direct approach does not work as expected (and it seems to be the case here), I recommend using two separate text commands to place the content on two lines manually by adjusting the y-coordinates.
Below is a sample code snippet to place "alpha" on the second line:
plot(1:10,1:10)
alpha = 3;
string = 'Hello';
% First line of text
text(5,5,sprintf('$\\textbf{%s}$', string), 'interpreter', 'latex', 'FontSize', 20)
% Second line of text, adjust the y-coordinate for placement
text(5,4.5,sprintf('{\\boldmath$\\alpha=%d$}', alpha), 'interpreter', 'latex', 'FontSize', 20)
In this code, the second text command's y-coordinate is manually adjusted to 4.5 (from 5 to 4.5) to place it below the first line. You might need to adjust the 4.5 value depending on your desired spacing between the lines.
This approach ensures that the "alpha" value is displayed on a new line below your specified text by manually controlling their positions.
Hope this helps.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Labels and Annotations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by