2015-03-24 83 views
2

我创建像这样一个传说,Matlab的 - 乳胶格式间距

theta = [90 120 80 120]; 
phi = [120 120 180 180]; 


for i=1:length(theta) 
    plot_legend{i} = sprintf('\\theta=%3d\\circ\\phi=%3d\\circ',theta(i),phi(i)) 
end 

这让我我想要的输出

plot_legend = 
'\theta= 90\circ\phi=120\circ' 
'\theta=120\circ\phi=120\circ' 
'\theta= 80\circ\phi=180\circ' 
'\theta=120\circ\phi=180\circ' 

但是当这是由TeX的解释器解释,领先的空间被忽视了,我觉得有点烦人。有没有简单的TeX方法来确保间距保持?

但是当通过图例调用时,间距不被保持。

legend(plot_legend,'interpreter','latex') 
+1

'\'(斜杠后面有一个空格)是建立在乳胶空白的一种方式,你也可以尝试'\,',对于一个完整列表,请参阅[上TeX.se这个答案](http://tex.stackexchange.com/a/74354),尽管有些可能不适用于Matlab的LaTeX解释器。 – David 2015-03-25 00:02:37

+0

但我只需要2digt的情况下,而不是3digit的空白? – Ash 2015-03-25 00:31:19

+0

是否要在显示的图例中添加空格?或者在评估'plot_legend'时在命令窗口中? – David 2015-03-25 00:48:32

回答

1

这里是乳胶的解释空间,以取代第一填零一个通用的解决方案:

% --- Definition 
theta = [5 120 80 120]; 
phi = [120 120 180 180]; 

% --- The function handle that does the job 
f = @(x, n) regexprep(num2str(x, ['%0' num2str(n) 'i']), '^(0+)', '${repmat(''\\hspace{0.5em}'', [1 numel($0)])}'); 

% --- Create a plot and the legend 

hold on 
for i = 1:length(theta) 

    % Fake curve 
    ezplot(['x^' num2str(i)]); 

    plot_legend{i} = ['$\theta=' f(theta(i),3) '^\circ,\ \phi=' f(phi(i),3) '^\circ$']; 
end 

legend(plot_legend, 'interpreter', 'latex') 

而结果:

enter image description here

您可以指定的长度带有函数句柄的第二个参数的数字。请注意,这只适用于整数值。

最佳,

+0

据我所知,你使用'regexprep'来用'/ hspace'替换每个缺失的数字? – Ash 2015-03-25 13:17:30

+0

是的,regexprep用空格替换所有paddding零。 – Ratbert 2015-03-25 13:41:16

+0

我只是遇到了一个独特的案例,theta = 0,是否有正则表达式不用'/ hspace'替换单元位置并保留相同的输入字符? – Ash 2015-03-30 14:50:23