A blog on software engineering by Trevor Brown

Syntax Highlighting in PowerPoint

screenshot of PowerPoint with code highlighting

Back in 2014 I wrote a blog post on inserting code samples into PowerPoint slides with syntax highlighting. It’s been about 8 years and things have changed. Using GitHub Gist no longer works for me.

I’ve found a better way to apply syntax highlighting to code for PowerPoint that also works for LibreOffice Impress with no differences. Both applications expect code in Rich Text Format. The only downside to this approach is you’ll need to have Python and a Python package installed for this to work.


I’m not going to cover Python installation here. If your OS comes with a package manager you can install Python with it. Or you can visit and download an installer. Once you have Python installed install Pygments using pip in the shell:

$ pip install Pygments

Syntax Highlighting

To convert your code to RTF format with syntax highlighting run the pygmentize command in the shell:

$ pygmentize -f rtf -O style=xcode code.rb -o code.rtf

In this example code.rb is the input file, and code.rtf is the output file. -f rtf specifies the output format as RTF and -O style=xcode specifies the color scheme to use when highlighting the code. Replace code.rb in my example with the name of the file containing the code you want to add to your presentation as the input file. Note that you must specify a correct file extension for your file so Pygments can detect the file type. Then open up the output file in an editor that supports RTF format and copy and paste the contents into your PowerPoint slide. If you are on OSX you can pipe the output of pygmentize directly to the clipboard and then paste it straight into PowerPoint:

$ pygmentize -f rtf -O style=xcode code.rb | pbcopy

After pasting into PowerPoint you’ll need to look for the clipboard icon below the text you just pasted and choose the "Keep Source Formatting" option. After you select that option you should see the syntax highlighting visible on the text you pasted.

Syntax Highlighting in Vim

The steps above work but they are a bit cumbersome. I use vim for everything so I thought it might be nice to have a Vim command that automatically invokes pygments on the current buffer and pipes the output to the clipboard. I looked around and found It does almost exactly what I want but it uses highlight instead of pygmentize. I created my own implementation of rtf-highlight that uses pygmentize. It’s only about 25 lines. All the code is listed below:

" set default theme, I like xcode
if !exists('g:rtf_theme')
  let g:rtf_theme = 'xcode'

" Function for RTF syntax highlighting of the current buffer. Useful when I
" need to paste code with syntax highlighting into PowerPoint or another
" application that supports rich text.
function! RTFHighlight(line1,line2)
  if !executable('pygmentize')
    echoerr "Bummer! pygmentize not found."

  let content = join(getline(a:line1,a:line2),"\n")
  let lexer=&filetype
  " Run the pygmentize command
  let command = "pygmentize -f rtf -O style=" . g:rtf_theme . " -l " . l:lexer
  let output = system(command, content)
  " Pipe the RTF text to pbcopy
  let retval = system("pbcopy", output)

" Invoking RTFHighlight on the current buffer will copy the highlighted code
" to clipboard
command! -range=% RTFHighlight :call RTFHighlight(<line1>,<line2>)

Now all I have to do is select the text I want highlighted from the current Vim buffer and then run :RTFHighlight and the RTF formatted text will be copied to my clipboard. Then I just paste it into PowerPoint or LibreOffice Impress. The great thing about this is I don’t even have to write the buffer to disk to highlight the code. Everything can be done in memory.