9

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

16

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.

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

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.