如何在其他关键词上添加其他颜色?
例如:
morekeywordsA={RequirePackage,ProvidesPackage}=\definecolor{keywordsA}{HTML}{8A4A0B}
morekeywordsB={{,},=}=\definecolor{keywordsB}{HTML}{othercolor}
\documentclass{scrreprt}
\usepackage{listings}
\usepackage[dvipsnames]{xcolor}
\definecolor{keywords}{HTML}{8A4A0B}
\definecolor{background}{HTML}{EEEEEE}
\definecolor{comments}{HTML}{868686}
\lstset{language=[LaTeX]Tex,
keywordstyle=\color{keywords},
basicstyle=\normalfont\ttfamily,
commentstyle=\color{comments}\ttfamily,
stringstyle=\rmfamily,
numbers=left,
numberstyle=\scriptsize,
stepnumber=1,
numbersep=8pt,
showstringspaces=false,
breaklines=true,
frameround=ftff,
frame=lines,
morekeywords={RequirePackage,ProvidesPackage},
backgroundcolor=\color{background}
}
\begin{document}
\begin{lstlisting}[firstnumber=1]
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{arsclassica}[2012/02/21 v4.0 Customizing ClassicThesis (LP)]
\RequirePackage{classicthesis}
\RequirePackage{caption}% Caption package
\end{lstlisting}
\end{document}
答案1
由于您想要自定义 (La)TeX,最好将其设置texcsstyle
为与关键字相同的颜色,并使用星号,以便反斜杠也具有相同的颜色;关于您的问题,括号并不是真正的关键字;我使用以下方式设置了它们的样式(以及方括号)literate
(请注意*
第一个文字行中的字符,以防止在注释和字符串中出现颜色形式):
\documentclass{scrreprt}
\usepackage{listings}
\usepackage[dvipsnames]{xcolor}
\definecolor{keywords}{HTML}{8A4A0B}
\definecolor{background}{HTML}{EEEEEE}
\definecolor{comments}{HTML}{868686}
\definecolor{myblue}{RGB}{20,105,176}
\lstset{language=[LaTeX]Tex,
keywordstyle=\color{keywords},
texcsstyle=*\color{keywords},
basicstyle=\normalfont\ttfamily,
commentstyle=\color{comments}\ttfamily,
stringstyle=\rmfamily,
numbers=left,
numberstyle=\scriptsize,
stepnumber=1,
numbersep=8pt,
showstringspaces=false,
breaklines=true,
frameround=ftff,
frame=lines,
morekeywords={RequirePackage,ProvidesPackage,NeedsTeXFormat},
backgroundcolor=\color{background},
literate=
*{\{}{{\textcolor{myblue}{\{}}}{1}
{\}}{{\textcolor{myblue}{\}}}}{1}
{[}{{\textcolor{myblue}{[}}}{1}
{]}{{\textcolor{myblue}{]}}}{1},
}
\begin{document}
\begin{lstlisting}[firstnumber=1]
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{arsclassica}[2012/02/21 v4.0 Customizing ClassicThesis (LP)]
\RequirePackage{classicthesis}
\RequirePackage{caption}% Caption package
\end{lstlisting}
\end{document}
为了回答您的具体问题,您可以[<number>]
同时使用 inkeywordstyle
和 inkeywords
来定义具有自己风格的不同关键字集;一个小例子
\documentclass{scrreprt}
\usepackage{listings}
\usepackage[dvipsnames]{xcolor}
\definecolor{keywords}{HTML}{8A4A0B}
\definecolor{background}{HTML}{EEEEEE}
\lstset{language=[LaTeX]Tex,
keywordstyle=\color{keywords},
keywordstyle=[2]\color{red},
keywordstyle=[3]\color{orange},
basicstyle=\normalfont\ttfamily,
showstringspaces=false,
frameround=ftff,
frame=lines,
morekeywords={RequirePackage,ProvidesPackage},
keywords=[2]{Some,Other,Keywords},
keywords=[3]{and,another,test},
backgroundcolor=\color{background}
}
\begin{document}
\begin{lstlisting}[firstnumber=1]
\NeedsTeXFormat{LaTeX2e}
Some Other Keywords
and another test
\end{lstlisting}
\end{document}
要将颜色应用于长度,可以使用 LaTeX(但这不适用于内联列表):
\documentclass{scrreprt}
\usepackage{listings}
\usepackage[dvipsnames]{xcolor}
\definecolor{keywords}{HTML}{8A4A0B}
\definecolor{background}{HTML}{EEEEEE}
\definecolor{comments}{HTML}{868686}
\definecolor{lengthcolor}{RGB}{200,40,150}
\definecolor{myblue}{RGB}{20,105,176}
\lstset{language=[LaTeX]Tex,
keywordstyle=\color{keywords},
texcsstyle=*\color{keywords},
basicstyle=\normalfont\ttfamily,
commentstyle=\color{comments}\ttfamily,
stringstyle=\rmfamily,
numbers=left,
numberstyle=\scriptsize,
stepnumber=1,
numbersep=8pt,
showstringspaces=false,
breaklines=true,
frameround=ftff,
frame=lines,
morekeywords={RequirePackage,ProvidesPackage,NeedsTeXFormat,setlength},
backgroundcolor=\color{background},
literate=
*{\{}{{{\color{myblue}{\{}}}}{1}
{\}}{{{\color{myblue}{\}}}}}{1}
{[}{{{\color{myblue}{[}}}}{1}
{]}{{{\color{myblue}{]}}}}{1},
escapeinside=!!
}
\newcommand\lencolor[1]{\textcolor{lengthcolor}{\texttt{#1}}}
\begin{document}
\begin{lstlisting}[firstnumber=1]
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{arsclassica}[2012/02/21 v4.0 Customizing ClassicThesis (LP)]
\RequirePackage{classicthesis}
\RequirePackage{caption}% Caption package
\setlength\parskip{!\lencolor{18pt}!}
\hspace*{!\lencolor{15cm}!}
\end{lstlisting}
\end{document}
答案2
要将颜色应用于长度,可以添加分隔符:
moredelim=[is][\color{lengthcolor}\texttt]{@@}{@@}
然后在您的列表中只需在 @@ 之前和之后添加:
\setlength\parskip{@@18pt@@}