3

I have a code for which I am using

\begin{lstlisting} 
line1 
line2
line3
\end{lstlisting}

I want to show the line1 and line3 as slanted. How this can be done ?

2 Answers2

3

You can use delimiters:

\documentclass{article}
\usepackage{listings}

\lstdefinestyle{mystyle}{
  basicstyle = \ttfamily ,
  moredelim = [is][\itshape]{|}{|}
}

\begin{document}

\begin{lstlisting}[style=mystyle]
|line1 
line2|
line3
\end{lstlisting}

\end{document}

enter image description here

\begin{lstlisting}[style=mystyle]
|line1| 
line2
|line3|
\end{lstlisting}

enter image description here

cgnieder
  • 66,645
2

Depending on the complexities, one might be able to use the optional argument of the verbatimbox environments to achieve this:

\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}

enter image description here