我有这个 MATLAB 代码:
在 sysms 之后,变量变成紫色,而我无法在 Latex 中的 MATLAB 脚本中做到这一点:
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{bigfoot} % to allow verbatim in footnote
\usepackage[numbered,framed]{matlab-prettifier}
\begin{filecontents*}{sample.m}
%Parcial Computacion Cientifica (Punto 1)
%Solucion del sistema de Ecuaciones para a2 y a3
%Se definen los variables simbolicas del sistema.
syms h a2 a3 f0 df0 f1 df1
eqn1 = f0 + df0*h + a2*(h^2) + a3*(h^3) == f1; %Ecuacion 7
eqn2 = df0 + 2*a2*h + 3*a3*(h^2) == df1; %Ecuacion 9
%Solucion del sistema de Ecuaciones.
solution = solve([eqn1, eqn2],[a2 a3]);
a2 = solution.a2; %Solucion de a2
a3 = solution.a3; %Solucion de a3
\end{filecontents*}
\let\ph\mlplaceholder % shorter macro
\lstMakeShortInline"
\lstset{
style = Matlab-editor,
basicstyle = \mlttfamily,
escapechar = ",
mlshowsectionrules = true,
}
\lstinputlisting[caption = {Sample code from Matlab}]{sample.m}
如果有人能告诉我如何将任何文本变成紫色的 MATLAB 风格,那将会非常有帮助,而且我想知道如何使代码的行数和字体看起来像这样:
答案1
您只需添加mlunquotedstringdelim
声明lstset
,提供两个您喜欢的分隔符(我使用了§
)。
\lstset{
style = Matlab-editor,
basicstyle = \mlttfamily,
escapechar = ",
mlshowsectionrules = true,
mlunquotedstringdelim={§}{§},
}
为了让框架内有数字,按照您的第二张截图,只需添加xleftmargin=2em,framexleftmargin=2.5em,
,并根据您的喜好增加边距;)得到的 MWE 是:
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{bigfoot} % to allow verbatim in footnote
\usepackage[numbered,framed]{matlab-prettifier}
\usepackage{listings}
\begin{filecontents}{sample.m}
%Parcial Computacion Cientifica (Punto 1)
%Solucion del sistema de Ecuaciones para a2 y a3
%Se definen los variables simbolicas del sistema.
syms §h a2 a3 f0 df0 f1 df1§
eqn1 = f0 + df0*h + a2*(h^2) + a3*(h^3) == f1; %Ecuacion 7
eqn2 = df0 + 2*a2*h + 3*a3*(h^2) == df1; %Ecuacion 9
%Solucion del sistema de Ecuaciones.
solution = solve([eqn1, eqn2],[a2 a3]);
a2 = solution.a2; %Solucion de a2
a3 = solution.a3; %Solucion de a3
\end{filecontents}
\let\ph\mlplaceholder % shorter macro
\lstMakeShortInline"
\lstset{
style = Matlab-editor,
basicstyle = \mlttfamily,
escapechar = ",
mlshowsectionrules = true,
mlunquotedstringdelim = {§}{§},
xleftmargin = 2em,
framexleftmargin = 2.5em
}
\begin{document}
\lstinputlisting[caption = {Sample code from Matlab}]{sample.m}
\end{document}