Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Embed formatted, syntax-highlighted source code in Keynote Apps
If you have ever given a presentation on any aspect of programming (or webdesign), you may have felt the need to embed some source code (formatted and syntax highlighted to be easily readable) in Keynote. Your options have traditionally been limited:
  • You can take a screenshot of the code in a programmer's editor and embed this in Keynote, but you lose any ability to edit the code or change its appearance once inserted.
  • You can use a web-based syntax highlighter such as GeSHi, and embed the generated page in Keynote as a Web View, but this is similarly inflexible. Also, Web Views have been removed from Keynote '09.
A much better approach would be to have the source code text (suitably formatted and highlighted) inserted natively in your presentation. Since you can drag Rich Text Format (RTF) text to Keynote, all you need is a way to convert your code to RTF.

Here are three ways of accomplishing this while keeping the formatting intact:
  1. If you have a recent version of the Xcode Developer Tools installed, you already have a Quick Look generator that supports syntax highlighting for a couple of common programming languages. In a Terminal session, type qlmanage -p source_file -o ., where source_file is the file you'd like to have highlighted.

    This will create a subdirectory called source_file.qlpreview, wherein you'll find a file called Preview.rtf. Just drag this file into Keynote; the formatted and highlighted code will appear in a new text box for further manipulation.
  2. While the Quick Look solution is useful in a pinch, it doesnt provide a way for you to customize the formatting of your code, nor does it support more esoteric programming languages. If you need more flexibility, consider installing a highly customisable command-line utility called Highlight (available from Fink and MacPorts) that allows you to convert a plaintext source code file to many formats, including RTF.
  3. Finally, if you want to bypass the command line entirely, here is a third solution that uses MacVim, the OS X port of the UNIX programmer's editor Vim. It assumes no knowledge of Vim (or vi), but if you are familiar with it, you should be able to streamline the procedure and adapt it to your needs (e.g. by changing the highlight colors).

    Install MacVim (I'm using the latest Snapshot 50), and use it to open your source code file. MacVim detects virtually all programming languages, but syntax highlighting is not on by default. To enable it, select Automatic on the Syntax menu.

    Now select Convert to HTML in the Syntax menu. MacVim will open a new buffer populated with HTML code. With this new buffer selected, select Files » Save; this will create a new HTML file in the same directory as the source code file.

    Open this HTML file in Safari. It should appear as a perfect duplicate of the original editing buffer in MacVim, with all formatting (font, indentation, syntax highlighting) options intact. Simply select the code as displayed in Safari and drag it across to a Keynote slide to create a new Text Box.
  • Currently 2.63 / 5
  You rated: 4 / 5 (16 votes cast)

Embed formatted, syntax-highlighted source code in Keynote | 5 comments | Create New Account
Click here to return to the 'Embed formatted, syntax-highlighted source code in Keynote' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Embed formatted, syntax-highlighted source code in Keynote
Authored by: altotus on Nov 13, '09 08:10:04AM

For what it's worth, you can open up the code in Xcode and cut and paste from there. Xcode puts the syntax-highlighted rich text on the clipboard just find and you simply paste / drag into Keynote from there.

I find that much simpler - but it only applies to things that Xcode highlights, of course...

[ Reply to This | # ]
Embed formatted, syntax-highlighted source code in Keynote
Authored by: le_fnord on Nov 13, '09 08:10:25AM

I think, most devels are using TextMate for coding ... a simpler way to do is installing copy-as-rtf textmate bundle, after that you can copy your code as rtf and paste it everywhere ...

[ Reply to This | # ]
Embed formatted, syntax-highlighted source code in Keynote
Authored by: Toadling on Nov 13, '09 09:25:37AM
Most developers I work with are using BBEdit these days (some recently switched from the seemingly stagnant TextMate). BBEdit provides both a "Save as Styled Text" and a "Save as Styled HTML" options. Although I personally think TextMate's syntax styling capabilities are a little better, BBEdit seems pretty good at this.

[ Reply to This | # ]
Embed formatted, syntax-highlighted source code in Keynote
Authored by: bobsc on Nov 13, '09 08:17:16AM

Xcode already makes a nice color-highlighted rich text view of source code. You can select some code, copy it, and paste into TextEdit. It will still be colored and formatted just like it was in Xcode. If you want you can save the TextEdit file, and it will save as rtf.


[ Reply to This | # ]
SubEthaEdit, too
Authored by: PCheese on Nov 13, '09 11:19:25AM
You can also use SubEthaEdit. Just choose Copy with Styles from the Edit menu.

[ Reply to This | # ]