Tuesday, August 28, 2012

Syntax Highlighting in LaTeX with Minted

Here is a quick tip for those of you who use MacPorts and want pretty syntax highlighting in your LaTeX documents. Recently I discovered a great looking package called minted, which depends on an external python syntax highlighter called Pygments. I couldn't find any decent tutorials online for a quick and easy setup, so I decided to write one. I hope it will be helpful for someone.

To my readers who didn't come upon this article through google, the minted package is a better alternative to the listings package. Minted allows you to present code in techno-color! This means the programs you have worked hard on, like:
int main() { printf("hello, world"); return 0; } will no longer be restricted to marginalizing black and white:


With minted your code will finally be noticed in the appendix of that report...


Disclaimer: Minted can do many things, but it will not make bad programs work. Though it will make them look like they could work.

The Setup

1. Make sure you have MacPorts and LaTeX installed.

2. Install Python. If you don't have it installed already, here is an example of how you would install Python 2.7 with MacPorts in the terminal:
sudo port install python27 
3. Install the proper Pygments package. For continuity, I'm going to show how to do this with the version of Python installed in step 2. In the terminal type the following:
sudo port install py27-pygments 
4. Finally, create a soft link from the Pygments package to your /usr/local/bin folder and the setup is complete.
sudo ln -s /opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin/pygmentize /usr/local/bin/pygmentize 

The Usage

Let's run some examples! One of the basic examples provided in the minted documentation is called minimal.tex. If you are feeling lazy, you can download it from here. The TeX file contains the first program everyone writes in C:
\documentclass{article} \usepackage{minted} \begin{documents} \begin{minted}[linenos]{c} int main() { printf("hello, world"); return 0; } \end{minted} \end{document}
The minted documentation assumes that you are compiling minimal.tex in the terminal. If this is true, to compile minimal.tex you need only to send this command:
pdflatex -shell-escape minimal I usually like to use TeXShop for my LaTeX needs. For us TeXShoppers, we must do some tinkering to mimic the "-shell-escape" command that is needed to compile the minted package. Go to the TeXShop Preferences, choose the Engine tab, and type "-shell-escape" under the pdfTeX/Latex section.


Now typeset it! Both methods should result in a PDF containing:


4 comments:

  1. FYI, If you use Kile the way to set up the pdflatex flag is to go to Tools > Configure Kile > Tools > Build > PDFLaTeX and add '-shell-escape' to the 'Options:' box.

    ReplyDelete
  2. Thank You So Much for this! Very clear and really helped me out!

    ReplyDelete
  3. There's a typo in your sample latex document:

    `\begin{documents}` should be `\begin{document}`

    Other than that, great job. This helped me get my system up and running.

    ReplyDelete