Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overbar in annotation textbox in Matlab plot (using Latex as interpreter)

Tags:

latex

matlab

I am trying to do write a simple equation of a plane plus the value of the coefficient \bar{a}_1, a_2 and a_3 but I cannot get the \bar to work. Any suggestion? I tried $\bar{a}_{1}$ with or without dollars, everything else looks fine to me, I used latex or tex interpreter but it cannot recognize it. this is the code:

a1=1
a2=2
a3=3
str = {'LLSQ fit:','z=$\bar{a}_{1}$+a_2x+a_3y',sprintf('$\bar{a}_{1}$=%5.2f',a1),sprintf('a_2=%5.2f',a2),sprintf('a_3=%5.2f',a3)};
annH = annotation('textbox',[0.63 0.8 0.08 0.08],'string',str,'interpreter','latex')
set(annH,'FitBoxToText','on','fontsize', 18,'BackgroundColor',[1 1 1])  

thanks

like image 548
Millemila Avatar asked Jun 15 '13 00:06

Millemila


People also ask

How do you write a subscript on a Matlab plot?

To add subscripts to symbolic variables in live scripts, append the corresponding index to the variable using one underscore ( _ ). For example, create two symbolic variables with subscripts using syms . Use these variables in an expression.

How do you write a Varphi in Matlab?

set(groot, 'defaultAxesTickLabelInterpreter','tex'); set(groot, 'defaultLegendInterpreter','tex'); set(0, 'defaultTextInterpreter', 'tex'); legend('\varphi', 'k');

What is the LaTeX font in Matlab?

In ubuntu (and possibly other distro's) the latex font is called Latin Modern, or lm for short. You can find them all via: # fc-list | grep lmroman /usr/share/texmf/fonts/opentype/public/lm/lmroman10-bold.

Can you write LaTeX in Matlab?

Insert LaTeX Equation To insert a LaTeX equation: Go to the Insert tab, click Equation , and select LaTeX Equation. Enter a LaTeX expression in the dialog box that appears.


1 Answers

I have listed the problems with your code below, here is a working code:

str = {'LLSQ fit: ' ...
    'z = $\bar{a}_{1}$ + $a_2x$ + $a_3y$' ...
    ['$\bar{a}_{1}$ = ' sprintf('%5.2f', a1)] ...
    ['$a_2$ = ' sprintf('%5.2f', a2)] ...
    ['$a_3$ = ' sprintf('%5.2f', a3)]};

annotation('textbox', [0.5 0.8 0.3 0.08], 'interpreter','latex', 'String', str);

Problems with the original code

  1. You have to wrap the whole latex commands with $ sign:

    This code does not give the desired output:

    annotation('textbox', [.2 .4 .1 .1], 'interpreter','latex', 'String', 'a_2x');
    

    But this one does:

    annotation('textbox', [.2 .4 .1 .1], 'interpreter','latex', 'String', '$a_2x$');
    
  2. You will loose some part of the string if you use sprintf since it has another interpreter (there are workarounds for this, but I suggest concatenating strings as I did above)

    sprintf('$\bar{a}_{1}$ = %5.2f', a1)
    

    will return:

    ar{a}_{1}$ = 1.00
    

    which is not recognized by latex. (\b is interpreted as backspace in sprintf and removes the vital $ sign.)

like image 65
p8me Avatar answered Oct 13 '22 06:10

p8me