14

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

1 Answer 1

27

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.

2
  • Actually, it worked perfectly. Thank you :)
    – GGirotto
    Mar 16, 2016 at 23:57
  • 5
    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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .