Highlight code in LibreOffice Impress on OS X

programming LibreOffice libre-office Apple os-x syntax-highlighting

It’s a pain to put code with proper syntax highlighting in slides, but it turns out there’s a solution for LibreOffice that does not involve taking screenshots, copying-and-pasting rendered HTML, or manually coloring text.

This article describes how to install libreoffice-code-highlighter, a syntax highlighting Python macro for LibreOffice, on OS X (the original instructions are for Linux). It uses Pygments to perform the syntax highlighting.

First, download the Highlight.py macro script and save it to your LibreOffice’s python scripts directory. The following commands will accomplish this:

mkdir -p ~/Library/Application\ Support/LibreOffice/4/user/scripts/python
curl https://raw.githubusercontent.com/slgobinath/libreoffice-code-highlighter/master/Highlight.py > ~/Library/Application\ Support/LibreOffice/4/user/scripts/python/Highlight.py

(The /4 part probably depends on your version of LibreOffice.)

Download the Pygments source code wherever you want, for example to ~/Downloads/pygments-main.

Go to that directory in a terminal, and install Pygments with LibreOffice’s bundled Python version:

cd ~/Downloads/pygments-main
/Applications/LibreOffice.app/Contents/MacOS/python \
  -E setup.py install

Now you can configure a keyboard shortcut to trigger the macro in LibreOffice:

  • Go to Tools > Customize > Keyboard
  • Select the LibreOffice option button (available in the top left corner)
  • Select any desired shortcut
  • Select LibreOffice Macros/user/Highlight/highlight_source_code in the Category and Function boxes
  • Click the Modify button to set the shortcut

You’re all set! Follow the usage instructions of libreoffice-code-highlighter to highlight some code.

Meta

Version f21631 of libreoffice-code-highlighter was used.