[Tex/LaTex] bold just one line inside of lstlisting

boldformattinglistings

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..

Best Answer

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}

enter image description here

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.

Related Question