Stratus3D

A blog on software engineering by Trevor Brown

Inserting Code Samples Into LibreOffice Impress

Last year I wrote a blog post about inserting code samples with syntax highlighting into Microsoft PowerPoint. On OSX it’s easy to use GitHub gists and TextEdit to generate syntax highlighting and convert highlighted code to RTF format, which can be pasted into PowerPoint slides. I have since moved to linux and now use LibreOffice Impress instead of PowerPoint. My old method of highlighting code in slideshows doesn’t work in Impress, but I have found a better way. On linux it is trivial to highlight code with Pygments and convert it to a format Impress can handle.

The most difficult part of this process is getting Pygments installed. I don’t use Python much I had to install pip before I could install Pygments:

$ sudo apt-get install python-pip

Or:

$ sudo yum install python-pip

On OSX pip should already be installed. Once you have the pip executable in your $PATH (you can check by running pip in the shell) you can install Pygments. To install Pygments run:

$ pip install Pygments

Note: You may need to be root to install Pygments if pip installs packages to place normal users cannot write too (e.g /usr/local/lib/).

Once you have Pygments installed there should be a pygmentize command available in the shell. Now comes the fun part. Save the code you want highlighted to a file somewhere on your file system and run the following command:

$ pygmentize -f rtf <source_file> > <destination_file> The source file should be a plain text file containing the code you want to highlight and the destination file should be the file you want the RTF document to be written to (e.g. `$ pygmentize -f rtf code/i/want/to/highlight.sh > hightlighted_code.rtf`). Now all you need to do is open up the RTF file in LibreOffice Writer and copy the text in it into your Impress presentation. When you open up the RTF document in Writer you should see the text highlighted and when you copy the text into Impress the format should not change. If the format does change undo the pasting of the text and then go to the Edit menu and click "Paste Special...", then choose "Formatted text [RTF]" in the dialog window that appears. You will likely need to change the font and font size of the text after you paste it into Impress. You might also want to [use another Pygments color scheme or create your own](http://pygments.org/docs/styles/) so the colors in the syntax highlighting match the other colors in your presentation.

###Resources