How can I use \textit
in a listing?
\documentclass[a4paper]{article}
\begin{document}
\begin{lstlisting}[label=test]
Some code.
\textit{This should be italic.}
Some code.
\end{lstlisting}
\end{document}
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It only takes a minute to sign up.
Sign up to join this communityYou can emphasize words using the emph
and emphstyle
keys.
See page 19 in the listings manual.
\documentclass[a4paper]{article}
\usepackage{listings}
\begin{document}
\lstset{emph={world}, emphstyle=\itshape} % the word "world" shall be italic
\begin{lstlisting}[label=test]
Hello world
\end{lstlisting}
\end{document}
I agree, it is not a good solution for highlighting single lines of code. I think here is what you are looking for:
\documentclass[a4paper]{article}
\usepackage{listings}
\lstset{escapeinside={(*@}{@*)}}
\begin{document}
\begin{lstlisting}[label=test]
Some code.
(*@\textit{This should be italic.}@*)
Some code.
\end{lstlisting}
\end{document}
\documentclass{...}
and ending with\end{document}
.mathescape
option, then inside yourlstlisting
you can use$\textit{Hello World.}$
.\lstinline
(since it also applies tolstlisting
); Using commands inside inline code