Effacer les filtres
Effacer les filtres

Error using latex in colorbar title

7 vues (au cours des 30 derniers jours)
Nathaniel H Werner
Nathaniel H Werner le 19 Déc 2019
Hello,
I'm trying to make my color bar have a title, and I want to give it a math equation. Here is my code.
cbh = findall(fh, 'Type', 'ColorBar');
cTH = get(cbh,'Title');
set(cTH,'String',['$',var,'(r)/','\underset{\text{LEV}}{\text{mean}}(',var,')$'])
The variable var is just a string in my code, you can make it any letter.
After getting to the third line in my code I get this error, and $var(r)/\underset{\text{LEV}}{\text{mean}}(var)$ showed up as the title for my colorbar.
Warning: Error updating ColorBar.
String scalar or character vector must have valid interpreter syntax:
However, if I open a live script in Matlab and type the content of the LaTeX script and run it successfully gives the equation.
I thought maybe the interpreter was something other than LaTex, so I tried changing it with the methods below but nothing happened. But I did go and check and they were already set as latex.
cbh.Title.Interpreter = 'latex';
cTH.Interpreter = 'latex';
So why am I getting this error? I checked in other LaTeX packages, and the syntax is correct. I also looked into the MatLab documentation and it seems like I am doing everything correct.
  1 commentaire
Walter Roberson
Walter Roberson le 19 Déc 2019
I recommend using sprintf to construct the title character vector into a variable and then setting the property from the variable. This makes for more robust debugging as you can check for possibilities such as odd characters such as could occur if var was numeric.
If you do use sprintf remember that \ in the format needs to be doubled to make it into the output.

Connectez-vous pour commenter.

Réponse acceptée

Subhadeep Koley
Subhadeep Koley le 7 Jan 2020
Hi, I can reproduce the same issue in my end also. I have written a workaround. Try if that works. Meanwhile we are looking into this.
fh=figure; surf(peaks); shading flat; axis tight;
colorbar;
cbh = findall(fh, 'Type', 'ColorBar');
cTH = get(cbh,'Title');
set(cTH,'String',['$','u_t','(r)/','\mathop{}_{\scriptstyle{LEV}}^{\rm{mean}}(','u_t',')$'],'Interpreter','latex','fontsize',17);
LaTeX.png
Hope this helps!
  2 commentaires
Nathaniel H Werner
Nathaniel H Werner le 7 Jan 2020
Should I accept this as a workaround until a solution is arrived at?
Subhadeep Koley
Subhadeep Koley le 7 Jan 2020
Yes, you can accept this workaround.

Connectez-vous pour commenter.

Plus de réponses (0)

Produits


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by