如何在MATLAB中将TeX/LaTeX格式用于自定义数据提示? [英] How do I use TeX/LaTeX formatting for custom data tips in MATLAB?

查看:290
本文介绍了如何在MATLAB中将TeX/LaTeX格式用于自定义数据提示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用标有'R:...,Theta:...'的数据提示注释一个极坐标图,其中theta实际上是希腊符号,而不是拼写的单词.我熟悉使用'\ theta'生成符号的字符串格式,但是在这种情况下不起作用.有没有办法将LaTeX解释器应用于数据提示?这是我到目前为止的内容:

I'm trying to annotate a polar plot with data tips labelled with 'R:...,Theta:...' where theta is actually the Greek symbol, rather than the word spelled out. I'm familiar with string formatting using '\theta' resulting in the symbol, but it doesn't work in this case. Is there a way to apply the LaTeX interpreter to data tips? Here's what I have so far:

f1=figure;
t=pi/4;
r=1;
polar(t,r,'.');
dcm_obj = datacursormode(f1);
set(dcm_obj,'UpdateFcn',@polarlabel)
info_struct = getCursorInfo(dcm_obj);
datacursormode on

其中的polarlabel定义如下:

where polarlabel is defined as follows:

function txt = polarlabel(empt,event_obj)
pos = get(event_obj,'Position');
x=pos(1);
y=pos(2);
[th,r]=cart2pol(x,y);
txt = {['R: ',num2str(r)],...
    ['\Theta: ',num2str(th*180/pi)]};

推荐答案

更新:该解决方案主要适用于R2014a和更早的版本,因为它对于较新的版本(特别是R2014b和较新的版本)似乎不起作用.使用新手柄图形系统.对于使用新手柄图形系统的新版本,可以在此处找到解决方案.

Update: This solution is primarily applicable to versions R2014a and older, since it appears to fail for newer versions, specifically R2014b and newer using the new handle graphics system. For newer versions using the new handle graphics system, a solution can be found here.

出于某种奇怪的原因,在MATLAB中数据游标工具强制将数据提示文本设置为按原样显示,而不是使用TeX/LaTeX解释显示(即使默认的MATLAB设置.似乎也没有办法通过数据光标模式对象属性直接设置文本属性.

For some odd reason, the data cursor tool in MATLAB forcibly sets the data tip text to be displayed literally instead of with TeX/LaTeX interpreting (even if the default MATLAB settings say to do so). There also appears to be no way of directly setting text properties via the data cursor mode object properties.

但是,我已经找到一种解决方法.如果将以下内容添加到polarlabel函数的末尾,则文本应正确显示:

However, I've figured out one workaround. If you add the following to the end of your polarlabel function, the text should display properly:

set(0,'ShowHiddenHandles','on');                       % Show hidden handles
hText = findobj('Type','text','Tag','DataTipMarker');  % Find the data tip text
set(0,'ShowHiddenHandles','off');                      % Hide handles again
set(hText,'Interpreter','tex');                        % Change the interpreter

说明

图中创建的每个图形对象都必须具有 'HandleVisibility'属性设置为'off',因此它们的句柄将不会显示在其父对象的子对象列表中,从而使它们更难找到.解决此问题的一种方法是设置 'ShowHiddenHandles'属性根对象的a>到'on'.然后,这将允许您使用 findobj 查找句柄具有某些属性的图形对象. (注意:,您也可以使用 findall ,而不必担心'ShowHiddenHandles'设置)

Explanation

Every graphics object created in the figure has to have a handle. Objects sometimes have their 'HandleVisibility' property set to 'off', so their handles won't show up in the list of child objects for their parent object, thus making them harder to find. One way around this is to set the 'ShowHiddenHandles' property of the root object to 'on'. This will then allow you to use findobj to find the handles of graphics objects with certain properties. (Note: You could also use findall and not worry about the 'ShowHiddenHandles' setting)

打开数据光标模式,然后单击该图会创建一个 hggroup对象,其中一个子元素是 'Interpreter'属性'tex',以便正确显示theta符号.

Turning on data cursor mode and clicking the plot creates an hggroup object, one child of which is the text object for the text that is displayed. The above code finds this text object and changes the 'Interpreter' property to 'tex' so that the theta symbol is correctly displayed.

从技术上讲,上面的代码仅需调用一次,而不必每次都调用polarlabel.但是,直到您第一次单击绘图以显示数据提示时(即第一次调用polarlabel),文本对象才存在,因此代码必须放入UpdateFcn中以获取数据光标模式对象,以便显示的第一个数据提示具有正确的文本格式.

Technically, the above code only has to be called once, not every time polarlabel is called. However, the text object doesn't exist until the first time you click on the plot to bring up the data tip (i.e. the first time polarlabel gets called), so the code has to go in the UpdateFcn for the data cursor mode object so that the first data tip displayed has the right text formatting.

这篇关于如何在MATLAB中将TeX/LaTeX格式用于自定义数据提示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆