1

This question in somehow related to https://stackoverflow.com/questions/1304315/highlighting-a-chunk-of-code-within-a-lstlisting but not exactly the same.

I want to make some slides with animation where one can see the execution of a program. The code is printed with lstlisting.

In order to make the code more readable, I want to highlight the line that is currently executed. The URL I show above has a solution where one modifies the code. Since I want to make animations, the code itself remains the same. Therefore it would be useful that I can just specify the line number and LaTeX will do the rest.

Is there some way to do this?

3

As discussed in the comment you want to use pygmentize. A solution with listings is provided here: highlight lines in listings

The package minted and verbments are using the parser pygmentize (see: minted vs. texments vs. verbments). I am using verbments here. Both packages require the package fancyvrb to print the contents. So you can manipulate some commands of fancyvrb without influencing the output of pygmentize.

In the example below I provide two new keys:

  • bgrlinenbr - accepts a comma list representing the line numbers. These lines will be highlighted by the given color of bgrlineclr
  • bgrlineclr - defines the background color of the highlighted line.

Here the result and code:

enter image description here

\documentclass[]{article}

\usepackage[T1]{fontenc}
\usepackage{beramono}
\usepackage{xparse}

\usepackage{verbments}
\makeatletter

\def\highlightedlines{}
\def\highlightedcolor{white}
\define@cmdkey{pyglist}[pl@]{bgrlinenbr}{\def\highlightedlines{#1}}%number
\define@cmdkey{pyglist}[pl@]{bgrlineclr}{\def\highlightedcolor{#1}}%color
\makeatother
\ExplSyntaxOn
\DeclareDocumentCommand \FancyVerbFormatLine { m }
{
 \clist_if_in:NVTF  \highlightedlines  { \the\value{FancyVerbLine} }
   { 
    \colorbox{\highlightedcolor}{#1}
  }
  {
     #1
  }
}
\ExplSyntaxOff
\begin{document}
Test
\begin{pyglist}[language=c,numbers=right,bgrlinenbr={1,2,4},bgrlineclr=red!20]
/**
* Prints Hello World.
**/
#include <stdio.h>

int main(void) {
   printf("Hello World!");
   return 0;
}
\end{pyglist}
\end{document}
  • 3
    Don't count minted out just yet. I've been communicating with Konrad Rudolph since learning that minted is no longer maintained, and am planning to maintain minted in the future. – G. Poore Mar 31 '13 at 13:19
  • @G.Poore: I had the same idea as I read his answer ;-) – Marco Daniel Mar 31 '13 at 13:51
  • Maybe we can get several people working on minted. – G. Poore Mar 31 '13 at 18:10
  • @G.Poore: Will you provide a github repository? – Marco Daniel Apr 2 '13 at 16:03
  • 1
    It will be at github.com/gpoore/minted. The issue tracking is still at code.google.com/p/minted for the moment, but I'll probably transition that to GitHub as well. – G. Poore Apr 8 '13 at 17:50

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.