我有一个正在使用的代码
\begin{lstlisting}
line1
line2
line3
\end{lstlisting}
我想将线 1 和线 3 显示为倾斜的。该怎么做?
答案1
您可以使用分隔符:
\documentclass{article}
\usepackage{listings}
\lstdefinestyle{mystyle}{
basicstyle = \ttfamily ,
moredelim = [is][\itshape]{|}{|}
}
\begin{document}
\begin{lstlisting}[style=mystyle]
|line1
line2|
line3
\end{lstlisting}
\end{document}
\begin{lstlisting}[style=mystyle]
|line1|
line2
|line3|
\end{lstlisting}
答案2
根据复杂程度,可能能够使用verbatimbox
环境的可选参数来实现这一点:
\documentclass{article}
\usepackage{verbatimbox,xcolor}
\begin{document}
\newcommand\formatting{%
\ifnum\theVerbboxLineNo=1\relax\slshape\fi%
\ifnum\theVerbboxLineNo=3\relax\slshape\fi%
}
\begin{verbbox}[\formatting]
line1
line2
line3
\end{verbbox}
This is my code: \fbox{\theverbbox}
\renewcommand\formatting{%
\ifnum\theVerbboxLineNo=1\relax\slshape\fi%
\ifnum\theVerbboxLineNo=3\itshape\leavevmode%
\smash{\rlap{\color{red!20}\rule[-\dp\strutbox]{27pt}{\baselineskip}}}\fi%
}
\begin{verbbox}[\formatting]
line1
line2
line3
line4
\end{verbbox}
This is my revised code: \fbox{\theverbbox}
\end{document}