我目前正在使用 listing 包在 Latex 中显示我的 matlab 代码。
但是,当尝试向包中添加新关键字时,什么也没有发生。以下是 MWE:
\documentclass[a4paper,12pt,parskip,bibtotoc,liststotoc]{article}
\usepackage[usenames,dvipsnames]{color}
\usepackage{listings}
\definecolor{javaBlue}{RGB}{42,0.0,255}
\definecolor{javaGreen}{RGB}{63,127,95}
\definecolor{javaLila}{RGB}{127,0,85}
\lstloadlanguages{Matlab} % load java, needed because of option 'savemem'
\lstset{
morekeywords={classdef}
}
\lstset{
language=Matlab,
keywordstyle=\color{javaLila},
commentstyle=\color{javaGreen},
stringstyle=\color{javaGreen},
numbers=left,
stepnumber=1,
numbersep=5pt,
numberstyle=\tiny,
breaklines=true,
breakautoindent=true,
breakatwhitespace=false,
postbreak=\space,
tabsize=2,
basicstyle=\ttfamily\scriptsize,
showspaces=false,
showstringspaces=false,
extendedchars=true,
backgroundcolor=\color{white}}
\begin{document}
\lstinputlisting[language=Matlab]{"E:/Program Files/MATLAB/projects/master/classes /Customer.m"}
\end{document}
我不知道为什么它不起作用,因为这个论坛上的所有相关帖子都告诉我使用“morekeywords”。似乎没有人遇到我的问题。
答案1
当将语言作为列表环境的可选参数加载时,将使用语言默认值。定义您自己的样式。现在,您使用 lstset i 设置的所有内容完全地忽略。您甚至可以定义一个新环境,从而节省一些输入。
\documentclass[a4paper,12pt,parskip,bibtotoc,liststotoc]{article}
%Only works with KOMA, and by the way some are obsolet by now
\usepackage[usenames,dvipsnames]{color}
\usepackage{listings}
\definecolor{javaBlue}{RGB}{42,0.0,255}
\definecolor{javaGreen}{RGB}{63,127,95}
\definecolor{javaLila}{RGB}{127,0,85}
\lstloadlanguages{Matlab} % load java, needed because of option 'savemem'
\lstdefinestyle{myMatlab}{
language=Matlab,
keywordstyle=\color{javaLila},
commentstyle=\color{javaGreen},
stringstyle=\color{javaGreen},
numbers=left,
stepnumber=1,
numbersep=5pt,
numberstyle=\tiny,
breaklines=true,
breakautoindent=true,
breakatwhitespace=false,
postbreak=\space,
tabsize=2,
basicstyle=\ttfamily\scriptsize,
showspaces=false,
showstringspaces=false,
extendedchars=true,
backgroundcolor=\color{white},
morekeywords={classdef}%copy and paste
}
\begin{document}
\begin{lstlisting}[style=myMatlab]
classdef warning
\end{lstlisting}
\end{document}