![matlab-prettifier 代码无法正确显示](https://linux22.com/image/312965/matlab-prettifier%20%E4%BB%A3%E7%A0%81%E6%97%A0%E6%B3%95%E6%AD%A3%E7%A1%AE%E6%98%BE%E7%A4%BA.png)
在下面的 MWE 中,从 \lstinputlisting 读取的 Matlab 代码中的文本(字体和颜色)不会像从 lstlisting 代码生成的 Matlab 那样显示:
\documentclass{book}
\usepackage[svgnames]{xcolor}
\definecolor{ocre}{RGB}{243,102,25}
\usepackage{avant}
\usepackage{mathptmx}
\usepackage{microtype}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{fontspec}
\usepackage{filecontents}
\begin{filecontents*}{sample.m}
% create a file for output
!touch testFile.txt
fid = fopen('testFile.text', 'w')
for i=1:10
fprintf(fid,'%6.2f \n', i);
end
\end{filecontents*}
\usepackage[numbered,framed]{matlab-prettifier}
\usepackage[font={color=ocre,bf},figurename=Fig.,labelfont={it}]{caption}
\newfontfamily{\lstconsolas}{Consolas}
\newcommand\ph\mlplaceholder
\begin{document}
\lstlistoflistings
\lstinputlisting[caption=Sample code from Matlab]{sample.m}
\begin{lstlisting}[
style=Matlab-editor,
basicstyle=\lstconsolas,
escapechar=`,
caption={For educational purposes},
]
% example of while loop using placeholders
while x2 = 1 + 100`\ph{condition}`
if `\ph{something-bad-happens}`
break
else
% do something useful
end
% do more things
end
\end{lstlisting}
\end{document}
我怎样才能让 sample.m 文件代码(从 Matlab 文件读取的代码)显示颜色、框和行号?
另外,如何改变 Matlab 代码字体的大小?
答案1
与您的其他问题相同,您需要style=Matlab-editor
告诉listings
应该将什么样式应用于列表。
字体大小的变化可以纳入到按键的设置中basicstyle
,例如basicstyle=\lstconsolas\small
。
如果您想一次为多个列表设置这些键,您可能需要研究\lstset
哪些可用于为整个文档或其中的任何组设置这些键,除非另行覆盖。
\documentclass{book}
\usepackage[svgnames]{xcolor}
\definecolor{ocre}{RGB}{243,102,25}
\usepackage{avant}
\usepackage{mathptmx}
\usepackage{microtype}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{fontspec}
\usepackage{filecontents}
\begin{filecontents*}{sample.m}
% create a file for output
!touch testFile.txt
fid = fopen('testFile.text', 'w')
for i=1:10
fprintf(fid,'%6.2f \n', i);
end
\end{filecontents*}
\usepackage[numbered,framed]{matlab-prettifier}
\usepackage[font={color=ocre,bf},figurename=Fig.,labelfont={it}]{caption}
\newfontfamily{\lstconsolas}{Consolas}
\newcommand\ph\mlplaceholder
\begin{document}
\lstlistoflistings
\lstinputlisting[style=Matlab-editor,caption=Sample code from Matlab]{sample.m}
\begin{lstlisting}[
style=Matlab-editor,
basicstyle=\lstconsolas\tiny,
escapechar=`,
caption={For educational purposes},
]
% example of while loop using placeholders
while x2 = 1 + 100`\ph{condition}`
if `\ph{something-bad-happens}`
break
else
% do something useful
end
% do more things
end
\end{lstlisting}
\end{document}