subscribe

Syntax highlighting in presentations

I often go back and forward between presentations software, trying to find the best one, never being happy. I recently used cleaver, which is nice for simple stuff, but this time I’m back to LibreOffice Impress because I don’t want to spend a long time fighting with layout.

One task that’s never super obvious is figuring out how to do syntax highlighting of code in my presentations. I wanted to share my solution. I’m fairly sure this will also work for MS Office and maybe even Keynote.

First, download Pygments. Pygments is a syntax highlighting tool for the command line.

On ubuntu it can be installed with:

apt-get install python-pygments

If you’re on a Mac, I don’t believe there’s a homebrew package. This should work though:

sudo easy_install Pygments

After that, we’re using the tool to convert your source file into a syntax highlighted RTF file! Yes, the old school format you’d never thought you’d need again.

Run it like this:

pygmentize -O style=xcode -o output.rtf input.js

I picked the xcode style, because it worked fairly well with the light backgrounds in my presentation, but other styles are supported. To see a list run:

pygmentize -L

Instead of pygmentize I also tried highlight, but it was impossible for me to build and seems a bit over engineered.

Using the file

In LibreOffice Impress, just click “Insert” and then “File” to find your rtf file.

Batch syntax highlighting

I don’t want to have to type this every time, so I made this Makefile to automatically do this. The file assumes that you have:

  • A src/ directory containing .js files (you can change the extension).
  • An empty rtf/ directory.

STYLE=xcode
SRC = $(wildcard src/*.js)

RTF = $(patsubst src/%.js, rtf/%.js.rtf, $(SRC))

all: $(RTF)

rtf/%.js.rtf: src/%.js
	pygmentize -O style=$(STYLE) -o $@ $<

clean:
	rm rtf/*.rtf

Web mentions

Comments

  • Philipp Kewisch

    Interesting, I just had the same need a few days later. Last time I needed this I actually used services like https://tohtml.com/ which generate colored HTML that can easily be pasted into Impress or the likes. This time around I was not quite happy with the color themes and wanted a local solution.

    What I ended up doing, since I write my code with vim, is using the neat :TOhtml feature, which takes the current buffer and generates a HTML file that looks exactly like it. Yes, this includes line numbers if you have them enabled.

    The HTML it generates uses CSS classes to set the colors, so changing the colors afterwards is fairly easy. Now this does not get you colored pasteable content yet, because the colors are not set inline. Since only a fraction of the HTML gets copied, the CSS rules are not included and OpenOffice will not see them.

    The next step I took is to install one of those CSS inliner tools often used when creating HTML Email templates. The one I used is juice from https://github.com/Automatt... which works fairly well. In vim I can then do :%!juice /dev/stdin /dev/stdout (I have an alias for this so I just have to use %!juice and it will inline the CSS.

    Finally, I can open this in the browser and get a picture perfect copy of what I saw in vim, which I can copy and paste into OpenOffice with all the colors.

    Given the extra CSS inlining step I realize this may not be the quickest solution, but for vim fans I think it works quite well, especially since you can easily change the colors.

    • Evert

      Evert

      Nice alternative!

  • Jeroen Noten

    If you use any JetBrains IDE (e.g. PhpStorm), when you copy source code from there, markup will be copied as well, and you can just paste it into your presentation! :D