How to insert LaTeX equation into static text/plot in GUI?
Show older comments
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
More Answers (2)
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}$$';
annotation(gcf,'textbox',[0,0,1,1],'string',D,'interpreter','latex');
Hope that's what you were looking for.
3 Comments
Betty Wan Niu
on 23 Apr 2015
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
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 .
Betty Wan Niu
on 24 Apr 2015
Edited: Betty Wan Niu
on 24 Apr 2015
0 votes
1 Comment
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');
Categories
Find more on Labels and Annotations in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!