Minted package for LaTeX: syntax highlighting for source code typesetting

In a previous entry, I briefly discussed the benefits of syntax highlighting for text editors. These benefits also apply to source code that is embedded in a web page or a document for distribution. If you use LaTeX to typeset school assignments and/or scientific publications (if not, you should be), then the minted package for LaTeX is excellent for typesetting source code with syntax highlighting in your documents.

Here is an example of a document containing a block of source code that uses minted for syntax highlighting.

\documentclass[letterpaper, 10pt]{article}
\usepackage[margin=1in]{geometry}
\usepackage{minted}
\usepackage{hyperref}
\hypersetup
{
  colorlinks=false,
  pdfborder={0,0,0},
}

\begin{document}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras vitae egestas nunc. Nullam non elit lacinia ante auctor gravida. Quisque id vestibulum dolor. Phasellus fermentum ante id nisi euismod nec ornare lectus fringilla. Praesent metus orci, porttitor vel lobortis eget, scelerisque non magna. In gravida felis vitae urna interdum dignissim. Maecenas tempus dui ut ipsum interdum molestie. Ut et dapibus risus. Etiam tempus, sapien quis feugiat ornare, nibh massa aliquet magna, quis interdum leo lacus eget urna. Proin aliquam lacinia metus, vitae posuere urna luctus nec. Vestibulum vitae lacus non orci placerat adipiscing quis id eros. Ut in enim orci, id pellentesque diam. Nulla dapibus sodales commodo.

\begin{minted}[mathescape,
               linenos,
               numbersep=5pt,
               gobble=2,
               frame=lines,
               framesep=2mm]{c}
  #include <zlib.h>
  #include <stdio.h>
  #include "kseq.h"
  // STEP 1: declare the type of file handler and the read() function
  KSEQ_INIT(gzFile, gzread)
   
  int main(int argc, char *argv[])
  {
      gzFile fp;
      kseq_t *seq;
      int l;
      if (argc == 1) {
          fprintf(stderr, "Usage: %s <in.seq>\n", argv[0]);
          return 1;
      }
      fp = gzopen(argv[1], "r"); // STEP 2: open the file handler
      seq = kseq_init(fp); // STEP 3: initialize seq
      while ((l = kseq_read(seq)) >= 0) { // STEP 4: read sequence
          printf("name: %s\n", seq->name.s);
          if (seq->comment.l) printf("comment: %s\n", seq->comment.s);
          printf("seq: %s\n", seq->seq.s);
          if (seq->qual.l) printf("qual: %s\n", seq->qual.s);
      }
      printf("return value: %d\n", l);
      kseq_destroy(seq); // STEP 5: destroy seq
      gzclose(fp); // STEP 6: close the file handler
      return 0;
  }
\end{minted}

\end{document}

Here is the final typeset product.

For more information about the minted package, see the minted project homepage. Note that minted relies on the Python Pygments syntax highlighter, which can be installed with the following command.

sudo easy_install Pygments
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s