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

25 ビュー (過去 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.
Sim
Sim 2024 年 4 月 23 日
Thanks a lot @Dyuman Joshi!! :-)

サインインしてコメントする。

採用された回答

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 Center および File ExchangeLaTeX についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by