Is there a way to fix a legend to an exact size and position?
조회 수: 9 (최근 30일)
이전 댓글 표시
Here is my setting (Code snippets simplified for illustration purposes):
I have a figure with a legend that uses the LaTeX interpreter.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1086590/image.png)
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1086595/image.png)
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).
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1086600/image.png)
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
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)
답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Legend에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!