Effacer les filtres
Effacer les filtres

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

23 vues (au cours des 30 derniers jours)
Sim
Sim le 23 Avr 2024
Commenté : Sim le 23 Avr 2024
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 commentaires
Dyuman Joshi
Dyuman Joshi le 23 Avr 2024
Modifié(e) : Dyuman Joshi le 23 Avr 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.
Sim
Sim le 23 Avr 2024
Thanks a lot @Dyuman Joshi!! :-)

Connectez-vous pour commenter.

Réponse acceptée

Lokesh
Lokesh le 23 Avr 2024
Modifié(e) : Lokesh le 23 Avr 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.

Plus de réponses (0)

Catégories

En savoir plus sur Labels and Annotations dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by