Clear Filters
Clear Filters

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

9 views (last 30 days)
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 Comments
Dyuman Joshi
Dyuman Joshi on 3 Aug 2022
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 on 3 Aug 2022
Thanks, that's a good idea and helped so there is at least a workaround.

Sign in to comment.

Answers (0)

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!