LaTex in Matlab legend

조회 수: 84 (최근 30일)
JuliaB
JuliaB 2018년 9월 8일
답변: Lazaros 2025년 4월 22일
I am trying to get my legend written in latex code, however I don't quite get most of the solutions I found when searching. My code is quite long, so I made a simplified version:
%
A= cos(tau)
l1 = sin(tau)
l2 = sin(tau + pi/8)
plot(tau, A, 'DisplayName', 'A(\tau)'), hold on
plot(tau, l1, 'HandleVisibility','off'), hold on
plot(tau, l2, 'DisplayName', 'l with $\eta_0 = \pi - \asin(\sqrt{K})$')
legend show
I have seen several solutions with legend(... 'interpreter', 'latex'), but I don't see how to incorporate that into my code. I have also seen some set(f,'defaulttextinterpreter','latex') but again I don't quite know how to use it and when I tried I only got an error.

채택된 답변

Star Strider
Star Strider 2018년 9월 8일
This will call the LaTeX interpreter correctly:
A= cos(tau)
l1 = sin(tau)
l2 = sin(tau + pi/8)
plot(tau, A, 'DisplayName', '$A(\tau)$'), hold on
plot(tau, l1, 'HandleVisibility','off'), hold on
plot(tau, l2, 'DisplayName', 'l with $\eta_0 = \pi - \asin(\sqrt{K})$')
hl = legend('show');
set(hl, 'Interpreter','latex')
You have an LaTeX syntax error in the third plot call (for ‘l2’). I will defer to you to solve that.
  댓글 수: 2
JuliaB
JuliaB 2018년 9월 8일
Thank you, that helped a lot.
Star Strider
Star Strider 2018년 9월 8일
As always, my pleasure.
See The Not So Short Introduction to LaTeX2e (link) for more on LaTeX commands and formatting.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Lazaros
Lazaros 2025년 4월 22일
Matlab understands all the basic functions that amsmath would load in a latex document. So you can use _, ^, as well as commands like \sin, \lim and \frac{}{}. Some more examples are included in the video https://www.youtube.com/watch?v=5TwChCWGllI , although the comments above cover you

카테고리

Help CenterFile Exchange에서 Legend에 대해 자세히 알아보기

태그

제품


릴리스

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by