我想编写一些 LaTeX 代码并将其插入到代码片段中,因此我\lstset
在主代码中使用了它,如下所示:
\documentclass[11pt]{article}
\usepackage{listings}
\usepackage{color}
\lstset{
language=TeX,
backgroundcolor=\color{white},
basicstyle=\ttfamily,
breakatwhitespace=false,
breaklines=true,
captionpos=b,
commentstyle=\usefont{T1}{pcr}{m}{sl}\color{red},
deletekeywords={},
escapeinside={\%},
firstnumber=1,
frame=leftline,
framerule=2.0pt,
keywordstyle=[1]\color{red}\ttfamily\bfseries,
morekeywords=[1]{\documentclass},
keywordstyle=[2]\color{blue}\ttfamily,
morekeywords=[2]{\begin, \end},
keywordstyle=[3]\color{green}\ttfamily,
morekeywords=[3]{\item},
numbers=left,
numbersep=10pt,
numberstyle=\tiny\color{red},
rulecolor=\color{black},
showstringspaces=false,
showtabs=false,
stepnumber=2,
stringstyle=\color{green},
tabsize=2,
}
\begin{document}
This is the snippet code:
\lstinputlisting{demo_code.tex}
According to this code, for example, "documentclass" should look like
{\color{red}$\backslash$documentclass} instead of $\backslash${\color{red}{documentclass}}
Likewhise, "begin" should look like {\color{blue}$\backslash$begin} instead of $\backslash$\color{blue}{begin}
\end{document}
这是在主代码中插入的演示代码:
\documentclass[12pt]{article}
\begin{document}
\begin{itemize}
\item this is the first item
\item this is the second item
\item this is the third item
\end{itemize}
\end{document}
所以,问题是我不知道如何在keywordstyle
, 命令中插入反斜杠符号。
答案1
- 软件包
listings
中含有针对 TeX 命令名称的特殊选项名称 (texcs
)。 - 值前面的星号
texcsstyle
在格式中也包含反斜杠。 \item
并且\end
需要从关键字/texcs 列表中删除,以便使用不同的类重新定义它们。- 该示例还添加了
columns=fullflexible
。它对我来说看起来更好。 - 颜色
green
相当明亮,与其他颜色相比,白色与背景色的对比度较低。因此,示例将其调暗一些。
\documentclass[11pt]{article}
\usepackage{listings}
\usepackage{xcolor}
\colorlet{lstgreen}{green!80!black}
\lstset{
language=TeX,
backgroundcolor=\color{white},
basicstyle=\ttfamily,
breakatwhitespace=false,
breaklines=true,
captionpos=b,
commentstyle=\usefont{T1}{pcr}{m}{sl}\color{red},
deletetexcs={end,item},
escapeinside={\%},
firstnumber=1,
frame=leftline,
framerule=2.0pt,
texcsstyle=*[1]\color{red}\ttfamily\bfseries,
moretexcs=[1]{documentclass},
texcsstyle=*[2]\color{blue}\ttfamily,
moretexcs=[2]{begin, end},
texcsstyle=*[3]\color{lstgreen}\ttfamily,
moretexcs=[3]{item},
numbers=left,
numbersep=10pt,
numberstyle=\tiny\color{red},
rulecolor=\color{black},
showstringspaces=false,
showtabs=false,
stepnumber=2,
stringstyle=\color{green},
tabsize=2,
columns=fullflexible,
}
\begin{document}
\lstinputlisting{demo_code.tex}
\end{document}