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 ?

  • 4
    Possible duplicate of Using bold/italic text inside listings? – Maarten Dhondt Jan 31 '17 at 14:04
  • @MaartenDhondt : Thanks. But probably that will not solve my problem . I have a JVML code of about 10 lines, and I do not want to make the keywords different looking. For explanation purpose I need some of the lines of my code to be slanted ( or of different color or may be boldfaced. I just need those lines to look different from others, that's it). – fellow_dev Jan 31 '17 at 14:17
  • Welcome to TeX.SE! Please take a moment to take our Tour! You may also want to look at what makes a good Minimal Working Example (MWE) – Tavrock Jan 31 '17 at 16:55
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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.