2

I'm trying to animate the C code. So I've used a colorbox to highlight the current row (How btw I can highlight the whole row?).

My problem is, I'm not that familiar with fonts, so I do not know how to arrange that code in lstlisting environment and the escaped part (the highlighted row) looks the same. And not strikingly different.

\documentclass{standalone}


\usepackage{xcolor}
\usepackage{listings}
\usepackage[scaled, mono]{zi4}

\definecolor{bluekeywords}{rgb}{0,0,1}
\definecolor{types}{rgb}{0.17,0.57,0.68}

\lstset{%language=C,
captionpos=b,
breakatwhitespace=true,
escapeinside={(*@}{@*)},
keywordstyle=\color{bluekeywords},
}
\lstdefinestyle{c}{
    morekeywords={bool,false,true},
}

\begin{document}
    \begin{lstlisting}[language=C, style=c]
   board[i][j] = path_len+1;        

   (*@\colorbox[RGB]{255,255,51}{\ttfamily{\color{blue}if} (board[i][j] == N * N) {\color{blue}return true};} @*)
    \end{lstlisting}
\end{document}

Here is the image of compiled document

enter image description here

and the one after(before) the other line highlighted.

enter image description here

I suppose, image sizes are different due, to artificial \t I've put in the example code to fully produce the second highlighted line.

Is there some automatic way to use the same fonts?

  • You've typeset it all using \ttfamily, but most of it is not typeset using that font in the regular part of the listing. – cfr Jun 27 '18 at 0:19
  • keywordstyle=\ttfamily\color{bluekeywords}, basicstyle=\ttfamily, commentstyle=\ttfamily, – Yiannis Lazarides Jun 29 '18 at 6:22
1

The solution is to add the second highlighted style with appropriate background

\documentclass[a5paper]{article}

\usepackage{xcolor}
\usepackage{listings}
\usepackage[scaled, mono]{zi4}

\definecolor{bluekeywords}{rgb}{0,0,1}
\definecolor{types}{rgb}{0.17,0.57,0.68}
\definecolor{highlight}{RGB}{255,255,51}

\lstset{%language=C,
captionpos=b,
breakatwhitespace=true,
escapeinside={(*@}{@*)},
keywordstyle=\color{bluekeywords},
}
\lstdefinestyle{c}{
    morekeywords={bool,false,true},
}

\lstdefinestyle{c-highlighted}{
    morekeywords={bool,false,true},
    backgroundcolor=\color{highlight},
}

\begin{document}
    \begin{lstlisting}[language=C, style=c]
   board[i][j] = path_len+1;
    \end{lstlisting}
    \begin{lstlisting}[language=C, style=c-highlighted]   
   if (board[i][j] == N * N) return true;
    \end{lstlisting}
\end{document}

the result The only problem with this solution are wide margins of the article class

0

\ttfamily is a switch. It changes the font family until further notice or the end of the current group. It doesn't take an argument.

So

\ttfamily{\color{blue}if} blah-blah-blah

will typeset if in blue typewriter and blah-blah-blah in typewriter.

Use

\texttt{color{blue}if} blah-blah-blah

to limit the typewriter font to if or, if you must,

{\color{blue}\ttfamily if}} blah-blah-blah

or

\textcolor{blue}{\ttfamily if} blah-blah-blah

adjusted output with constrained font effects

\documentclass{standalone}


\usepackage{xcolor}
\usepackage{listings}
\usepackage[scaled, mono]{zi4}

\definecolor{bluekeywords}{rgb}{0,0,1}
\definecolor{types}{rgb}{0.17,0.57,0.68}

\lstset{%language=C,
captionpos=b,
breakatwhitespace=true,
escapeinside={(*@}{@*)},
keywordstyle=\color{bluekeywords},
}
\lstdefinestyle{c}{
    morekeywords={bool,false,true},
}

\begin{document}
    \begin{lstlisting}[language=C, style=c]
   board[i][j] = path_len+1;        

   (*@\colorbox[RGB]{255,255,51}{\texttt{\textcolor{blue}{f}} (board[i][j] == N * N) \textcolor{blue}{return true};} @*)
    \end{lstlisting}
\end{document}
  • Do you see the difference between both board[i][j]? When it comes to animation you pretty see the rows jump and fonts change. I've used tt also to get a similarity for array indexing – dEmigOd Jun 27 '18 at 4:30
  • @dEmigOd I just corrected the font. If you mean the spacing, you need to add what you need manually since you are just doing this as regular text so no special formatting gets applied automatically. Isn't that the whole reason you're doing this in the first place with jumping outside the listing? So that you don't get the automatic formatting?) – cfr Jun 28 '18 at 0:24
  • The only reason is to get highlighting for that specific row, aka the current row at execution. Maybe i need to split into two listings and put one of the listings as a whole inside some colorbox environment? – dEmigOd Jun 28 '18 at 4:26
  • I think highlighting could be done with background color of the listing itself – dEmigOd Jun 28 '18 at 4:30
  • @dEmigOd Have you considered using tikzmark? That has specialised support for annotating code listings and you could easily overlay a filled node with an appropriate blend mode. – cfr Jun 28 '18 at 21:17

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.