# bold just one line inside of lstlisting

How can i bold just one line in this code?

\begin{lstlisting}
//Algoritmo 2
int f2 ( n ):
local i , j , r =0;
para i = 1 a n -1
para j = i +1 a n
r = r + 2;
cont++;
retorna r ;
\end{lstlisting}


I just want to bold cont++ I tried to use \textbf but no success..

The best way to do this would be to use the escapeinside command, like this:

\documentclass{article}
\usepackage{listings}
\lstset{
escapeinside={(*}{*)}
}
\begin{document}
\begin{lstlisting}
//Algoritmo 2
int f2 ( n ):
local i , j , r =0;
para i = 1 a n -1
para j = i +1 a n
r = r + 2;
(*\bfseries cont++;*)
retorna r ;
\end{lstlisting}
\end{document}


However, depending on what else you've put in your \lstset, this approach may or may not work. In particular, it's common to put code listings in monospaced font, and if so, you'll have to use one of the solutions to this question to get a monospaced bold font.

• Actually, it worked perfectly. Thank you :) Mar 16, 2016 at 23:57
• If you wish to maintain the same spacing used by lstlisting, you should use (*\aftergroup\bfseries*) ... (*\aftergroup\mdseries*)...
– Werner
Mar 17, 2016 at 0:01