Matlab注释中的Latex多线支架

时间:2017-04-18 14:51:19

标签: matlab latex matlab-figure legend legend-properties

我希望注释一个用Matlab制作的图形,其中一些支架可以缠绕3条线。链接的图中给出了一个例子:

enter image description here

我成功地添加了第二个传奇。但是我想知道我怎么能干得这么干净。我尝试做这样的事情

str = '$S_n =$ $\left\{ \begin{tabular}{c} 0.5 MeV \\ 50 keV \\ 5 MeV \end{tabular}\right.$';
annotation('textbox',[0.325,0.175,0.1,0.1],'String',str,'Interpreter','latex','FitBoxToText','on','Linestyle','none')

但这会产生如下结果:

enter image description here

最大的问题是

  • 我必须调整注释的位置......但这不是什么大问题。我可以花一些时间来定位它。
  • 支架太大了......我没有解决方法。我怎么能这样做?

问题是:

  1. 支架可以缩小吗?
  2. 如果没有,可以用另一种方式完成吗?

1 个答案:

答案 0 :(得分:1)

我个人认为支架看起来不错,担心确切的尺寸会让人分心。

但是,另一个选项是为图例添加标题。 这将允许您表示每个图例所指的单位/参数,而不会使图例的每一行混乱。遗憾的是,这不是本机MATLAB功能,但我们可以强制它。确切的实现因matlab版本而异。

PRE 2014 CODE

function zz_LegendTitle(LegendHandle , TitleText, Fontsize)
% Workaround to Matlab 2014 thinking that legends don't need titles.
%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if ~exist('Fontsize','var'); Fontsize = 10; end 
if ~exist('TitleText','var'); TitleText = 'example text'; end 


% Create an invisible axes at the same position as the legend
hLegendAxes = axes('Parent',LegendHandle.Parent, 'Units',LegendHandle.Units, 'Position',LegendHandle.Position, ...
                   'XTick',[] ,'YTick',[], 'Color','none', 'YColor','none', 'XColor','none', 'HandleVisibility','off', 'HitTest','off');

% Add the axes title (will appear directly above the legend box)
hTitle = title(hLegendAxes, TitleText,...
                'interpreter','latex',...
                'FontWeight','normal',...
                'FontSize',Fontsize);  % Default is bold-11, which is too large

% Link between some property values of the legend and the new axes
hLinks = linkprop([LegendHandle,hLegendAxes], {'Units', 'Position', 'Visible'});
% persist hLinks, otherwise they will stop working when they go out of scope
setappdata(hLegendAxes, 'listeners', hLinks);

% Add destruction event listener (no need to persist here - this is done by addlistener)
addlistener(LegendHandle, 'ObjectBeingDestroyed', @(h,e)delete(hLegendAxes));

Legend Title Example

POST 2014 CODE

    hLegend = legend(LegTxt,...
        'interpreter','latex','FontSize',LegFontSize,...
        'location','eastoutside');
    %resize to fix the legend-enforced size change
    set(ax(1),'Units',units,'position',IcePosVec);

%Attach a title to legend (here be dragons. Matlab 2015+ workaround)
     hlt = text('some text',...
    'Parent', hLegend.DecorationContainer, ...
    'String', 'Title', ...
    'HorizontalAlignment', 'center', ...
    'VerticalAlignment', 'bottom', ...
    'Position', [0.5, 1.05, 0], ...
    'Units', 'normalized');

PS。在信用到期的情况下,我在大约一年前通过从优秀的Undocumented Matlab网站无耻地窃取来编写这些代码。