Matlab - 乳胶格式间距

时间:2015-03-24 23:57:40

标签: matlab latex legend spacing tex

我正在创建一个这样的传奇,

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 个答案:

答案 0 :(得分:1)

以下是使用LaTeX解释空格替换第一个填充零的一般解决方案:

% --- 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

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

最佳,

相关问题