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

15 views (last 30 days)
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 Comments
Dyuman Joshi
Dyuman Joshi on 23 Apr 2024
Edited: Dyuman Joshi on 23 Apr 2024
"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.

Sign in to comment.

Accepted Answer

Lokesh
Lokesh on 23 Apr 2024
Edited: Lokesh on 23 Apr 2024
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.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!