Is there a way to fix a legend to an exact size and position?

13 次查看(过去 30 天)
Here is my setting (Code snippets simplified for illustration purposes):
I have a figure with a legend that uses the LaTeX interpreter.
figure
plot(my_data,'LineWidth',2)
legend('SMO $\sigma = 10^{-3}$','SMO $\sigma = 10^{-2}$','SMO $\sigma = 10^{-1}$', ...
'HGO $\sigma = 10^{-3}$','HGO $\sigma = 10^{-2}$','HGO $\sigma = 10^{-1}$', ...
'Interpreter','latex','NumColumns',3,'Location','north')
When exporting the figure as an SVG I change the interpreter to 'none'. This seems necessary to me because I want to use the Inkscape functionality to export SVG's to .pdf_tex (see: here), but when using the LaTeX interpreter this didn't work in my experience.
fig = gcf;
ax = gca;
pos = ax.Legend.Position; % Remember the legend position before the interpreter is changed
ax.Legend.Interpreter = 'none'; % Change the interpreter
ax.Legend.Position = pos; % Set the remembered position -> Unfortunately, this didn't help
print(fig,'my_figure_name','-dsvg') % Export as SVG
When including the exported figure in my tex project it is rendered using an LaTeX interpreter. Unfortunately, the legend now occupies to much space (as this was needed for all the LaTeX commands during the export).
So here is my question:
Is there a way to fix my legend to an exact size and position so it does not move when changing the interpreter?
Thanks for any help in advance :)
  4 个评论
Dyuman Joshi
Dyuman Joshi 2022-8-3
Maybe decreasing the font size might help.
x=reshape(linspace(-pi,pi,48),6,[]);
figure
plot(sin(x),'LineWidth',2)
legend('SMO $\sigma = 10^{-3}$','SMO $\sigma = 10^{-2}$','SMO $\sigma = 10^{-1}$', ...
'HGO $\sigma = 10^{-3}$','HGO $\sigma = 10^{-2}$','HGO $\sigma = 10^{-1}$', ...
'NumColumns',3,'Location','north', 'FontSize', 7.5)
Daniel Pusicha
Daniel Pusicha 2022-8-3
Thanks, that's a good idea and helped so there is at least a workaround.

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Legend 的更多信息

产品


版本

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by