这是我的书 documentclass 的序言:
\documentclass[11pt,fleqn]{book} % Default font size and left-justified equations
\usepackage[top=3cm,bottom=3cm,left=3.2cm,right=3.2cm,headsep=10pt,a4paper]{geometry} % Page margins
\usepackage[titletoc]{appendix}
\usepackage[svgnames]{xcolor} % Required for specifying colors by name
\definecolor{ocre}{RGB}{243,102,25} % Define the orange color used for highlighting throughout the book
\definecolor{mygray}{RGB}{243,243,244}
% Font Settings
\usepackage{avant} % Use the Avantgarde font for headings
\usepackage{mathptmx}
\usepackage{microtype}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{makeidx}
\usepackage{calc}
%\usepackage{bigfoot}
\usepackage[numbered,framed]{matlab-prettifier}
\usepackage{filecontents}
\usepackage{empheq}
% Index
\usepackage{calc} % For simpler calculation - used for spacing the index letter headings correctly
\usepackage[font={color=ocre,bf},figurename=Fig.,labelfont={it}]{caption}
\usepackage[framemethod=default]{mdframed}
\usepackage{mathtools}
\usepackage[most]{tcolorbox}
\tcbset{myformula/.style={
arc=0pt,
outer arc=0pt,
%colback=ocre!10,
colback=mygray,
colframe=ocre,
boxrule=0.8pt,
left=2pt,
right=2pt,
highlight math style={
arc=0pt,
outer arc=0pt,
colback=mygray,
colframe=red.
}
}
}
%\usepackage{hyperref}
\newenvironment{spread}[1]{%
\advance\jot#1% indeed
}{%
\ignorespacesafterend
}
当我尝试输入一些 matlab 代码时:
\begin{lstlisting}[caption = {For educational purposes}]
% example of while loop using placeholders
while "\ph{condition}"
if "\ph{something-bad-happens}"
break
else
% do something useful
end
% do more things
end
\end{lstlisting}
它只显示为黑色和白色:
我想要得到的是:
你能告诉我如何修复我的 tex 文件以便 Matlab 代码可以显示彩色吗?
答案1
首先,我们需要用 来告诉listings
应用哪种风格style=Matlab-editor
。
其次,如果您想使用\ph
占位符的简写,则必须\newcommand\ph\mlplaceholder
按照手册中讨论的那样定义简写。
第三,即使使用简写,您仍然必须选择并告知listings
合适的转义字符,以便它知道何时退出逐字处理。在这里,我escapechar=`
在手册中使用了 as。
这是一个最小示例,显示了此代码清单的所有要求:
\documentclass{book}
\usepackage[T1]{fontenc}
\usepackage[numbered,framed]{matlab-prettifier}
\newcommand\ph\mlplaceholder
\begin{document}
\begin{lstlisting}[
style=Matlab-editor,
basicstyle=\mlttfamily,
escapechar=`,
caption={For educational purposes},
]
% example of while loop using placeholders
while `\ph{condition}`
if `\ph{something-bad-happens}`
break
else
% do something useful
end
% do more things
end
\end{lstlisting}
\end{document}
输出结果如下:
您选择的特定字体不提供\textlangle
和\textrangle
符号,它们由内部用来matlab-prettifier
分隔占位符。由于不提供文本模式尖括号,我们可以通过将命令更新为使用数学模式尖括号来解决此问题。我使用以下代码执行此操作:
\makeatletter
\renewcommand\phOpDelim@mlpr{$\langle$}
\renewcommand\phClDelim@mlpr{$\rangle$}
\makeatother
另一个选择是自己定义。在这种情况下,我上面添加的代码块不是必需的\textlangle
。\textrangle
以下是使用数学模式符号的完整代码:
\documentclass{book}
\usepackage[svgnames]{xcolor}
\definecolor{ocre}{RGB}{243,102,25}
\definecolor{mygray}{RGB}{243,243,244}
\usepackage{avant}
\usepackage{mathptmx}
\usepackage{microtype}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[numbered,framed]{matlab-prettifier}
\usepackage[font={color=ocre,bf},figurename=Fig.,labelfont={it}]{caption}
\newcommand\ph\mlplaceholder
\makeatletter
\renewcommand\phOpDelim@mlpr{$\langle$}
\renewcommand\phClDelim@mlpr{$\rangle$}
\makeatother
\begin{document}
\begin{lstlisting}[
style=Matlab-editor,
basicstyle=\mlttfamily,
escapechar=`,
caption={For educational purposes},
]
% example of while loop using placeholders
while `\ph{condition}`
if `\ph{something-bad-happens}`
break
else
% do something useful
end
% do more things
end
\end{lstlisting}
\end{document}
输出结果如下: