Clear Filters
Clear Filters

How to insert LaTeX equation into static text/plot in GUI?

23 views (last 30 days)
I want to portrait different math equation (latex) in static text (Tag:Eqn), according to the choice in pop up menu(Tag:popupmenu1). Somehow, the textbox showed a number that increase for one unit every time I click on the choice in the pop up menu. I used the command
D ='$$z = c^{2} - \frac{c^{2}}{a^{2}}x^{2} - \frac{c^2}{b^2}y^{2}$$'; E = text(0,0,D,'interpreter','latex'); set(handles.Eqn,'String',E);
Then, I tried to change the static box to plot, to check if I was able to key in math equation. I typed the following command under the plot's Callback function.
D ='$$z = c^{2} - \frac{c^{2}}{a^{2}}x^{2} - \frac{c^2}{b^2}y^{2}$$'; E = text(hObject,0,0,D,'interpreter','latex'); annotation(hObject,'textbox',0,0,'string',D,'interpreter','latex');
Still, I am unable to get the text with or without the third line.
Please help. If possible, I wish to portrait the equation in static textbox. I am using MATLAB R2015a.
Thank you.

Accepted Answer

Mike Garrity
Mike Garrity on 23 Apr 2015
Edited: Mike Garrity on 23 Apr 2015
These three commands:
create three different types of text objects which are used for different jobs.
  • The first one creates a graphics text object which goes in an axes. It will pay attention to things like xlim and ylim, and it does support the latex interpreter.
  • The second one creates a different type of graphics text object which draws over all of the axes. It also supports the latex interpreter, but it's not going to pay attention to things like xlim and ylim.
  • The third one creates a uicontrol with a text string in it. It does not support the latex interpreter, so you can't use it for this job.
In your code snippet:
  • You have a variable named hObject. I'm not sure what that's supposed to be. I don't think you want that.
  • In the call to annotation, you're passing in an X & Y argument. The annotation command doesn't want those. That's the syntax for the text command.
The annotation command creates an object with a Position property. That's a rectangle with four values [x y w h]. Those are in normalized units (i.e. 0 to 1), where the X & Y for the text command are in the coordinate system of the axes. I can use it like this:
h = annotation('textbox','String',D);
h.Position=[.5 .5 .2581 .0929];
If the width of your box is too small to hold the string, then it will try to wrap it into multiple lines. Unfortunately, that doesn't work well with LaTeX because it doesn't know enough about where it can safely break the equation. The best way to deal with that is to use the property named 'FitBoxToText':
h.FitBoxToText = 'on'
That will recompute the width so that your equation will fit on one line.

More Answers (2)

Michael Haderlein
Michael Haderlein on 23 Apr 2015
Edited: Michael Haderlein on 23 Apr 2015
I guess you mean a uicontrol of style "text" with "static text", right?
The first part of your question: You set the String property of your uicontrol to E. However, E is the handle, not the message (I avoid using "text" here to prevent from further confusion). So, you always create a new text object first and then make its handle be shown in handles.Eqn. That should hopefully explain the number behaviour.
In your second attempt, you give the handle of hObject as parameter to the text function. But it's not understood as handle but as numeric value and will create a text control at x=hObject, y=0, z=0. You can check this by
set(gca,'xlim',[hObject-1 hObject+1])
Please note that it actually does not make sense to do arithmetics with handle values, it's just to show what happened.
Finally, you create the annotation. That's coming close to what you intend, but to create a textbox, you need to give [x y w h] instead of x,y (I suppose the two zeros are meant to be the position). Also, hObject must be a handle referencing to a figure. I don't know if that's the case. So, what will work:
D ='$$z = c^{2} - \frac{c^{2}}{a^{2}}x^{2} - \frac{c^2}{b^2}y^{2}$$';
Hope that's what you were looking for.
Michael Haderlein
Michael Haderlein on 23 Apr 2015
I must admit - I'm puzzled. When I create the annotation, sometimes it tells me that it cannot interpret the string. When I use the mouse to resize it (or, even only reposition it), it will suddenly appear correctly. Seems like 0.3 is the minimum width you actually need for this equation (on a standard sized figure). But I cannot say anything to the weird behavior. If you cannot fix this and nobody else here has an idea, maybe this file exchange submission could be helpful. It actually creates a pushbutton, but you should be able to reset the style to "text" afterwards.
Walter Roberson
Walter Roberson on 11 Feb 2019
if it wrapped the latex in mid construct then you could get interpreter errors . It wraps as if each character was being output as text rather than rendering and splitting the bitmap . $ string $ is likely to get split which would cause interpretation failures .

Sign in to comment.

Betty Wan Niu
Betty Wan Niu on 24 Apr 2015
Edited: Betty Wan Niu on 24 Apr 2015
I took the ideas from Michael and Mike and put as below:
D ='$\frac{x^2}{a^2}$ + $\frac{y^2}{b^2}$ + $\frac{z^2}{c^2}$ = 1';
annotation(gcf,'textbox', [0.025,0.78,0.1,0.1],'string',D,'interpreter','latex', 'FontSize',22,'FitBoxToText','on');
It works!
I didn't use the format suggested below because the position is totally different compared to put it inside the command of 'annotation'. h = annotation('textbox','String',D); h.Position=[.5 .5 .2581 .0929];
Anyway, thanks a lot!
  1 Comment
sandeep singh
sandeep singh on 11 Feb 2019
Hello ,
can i use same using uicontrol as shown below, if not please guide me how to use
% h41 = uicontrol(...
% 'Parent',DigitalCompensatorImagePanel4,...
% 'FontUnits',get(0,'defaultuicontrolFontUnits'),...
% 'Units','normalized',...
% 'HorizontalAlignment','left',...
% 'String','TEST_{Equation}',...
% 'Style','text',...
% 'interpreter','latex',...
% 'Position',[CasOL2TX CasOL2TY-CasOL2Toff CasOL2TW CasOL2TH],...
% 'Children',[],...
% 'Tag','text2');

Sign in to comment.


Find more on Migrate GUIDE Apps in Help Center and File Exchange


Community Treasure Hunt

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

Start Hunting!