How to paste syntax highlighted code into Keynote ’13

iworkkeynote

I give a lot of code heavy presentations with Keynote. Syntax highlighting makes these readable.

In the past I've used web based syntax highlighting tools, and then pasted the easier-to-read code into my presentation.

When I upgraded to iWork '13, which includes Keynote 6.1, pasting content strips the font style and color.

For example, if I take this colorful code from GitHub and paste it into Keynote it removes all the formatting as shown here:

enter image description here

Is there a way to get syntax highlighted code into these presentations?

Best Answer

The 'short' answer

... is that different apps copy formatted text to the clipboard in different ways, and different apps support pasting from different formats. Typically you'll get some kind of 'plain text' format, as well as maybe HTML or RTF formatted text in the clipboard, and it's up to the pasting app to choose which it likes best.

Keynote 6.1 definitely supports some kind(s) of formatted text pasting, as I can get this to work with Safari. But it doesn't seem to work with Firefox or Chrome (at the time of writing). From a bit of testing, it seems the Apple apps all play nicely together (no surprise there), and a few others will copy/paste to/from the Apple apps too (including Microsoft Office products). Unfortunately when you copy from Chrome, you only get plain text & HTML-formatted text copied to the clipboard, so it would appear Apple's iWork apps don't support pasting HTML formatted text.

So if copy/paste doesn't include formatting, you can either:

  • Try a different browser to copy from, such as Safari; or
  • Find an intermediate app to paste into (which 'speaks' both HTML formatting and can re-copy into a format Keynote/etc understand), such as Apple's own TextEdit.app

Surprisingly, Apple's own TextEdit.app will happily paste formatted text from Chrome, and it can be re-copied to the clipboard and pasted with formatting into Keynote/Pages/etc.

Depending on your point of view, this may seem a limitation (or fault) in Chrome/Firefox or in Pages/Keynote/etc, but in the end they simply don't speak the same formatting languages (at present).

I'd encourage anyone experiencing this to send feedback to the developers on both 'sides' in the hope of getting this fixed—either with Chrome/Firefox supporting RTF/RTFD copying (or whatever is most standard/appropriate beyond HTML), or by Apple's iWork apps supporting the pasting of HTML content.


More than you want to know about copying to the clipboard

When you copy some text to the clipboard, depending on the application, it may get copied in a few formats. You can see these using Apple's Clipboard Viewer application (requires building in Xcode).

For example, copying from Google Chrome, the clipboard gets populated with four separate copies of the information I copied—two plain text (public.utf8-plain-text, NSStringPboardType), and two HTML formatted (public.html, Apple HTML pasteboard type).

Copying from other apps gives various combinations of format types, with some other common ones being 'TEXT' (CorePastboardFlavorType), com.apple.traditional-mac-plain-text, Apple PDF pasteboard type, Apple PICT pasteboard type, public.rtf, NeXT Rich Text Format v1.0 pasteboard type, NeXT RTFD pasteboard type, com.apple.flat-rtfd, com.apple.iWork.TSPNativeData, com.adobe.pdf, com.adobe.indesign-import-ustl, and so on...

As you can see there are a variety of plain text formats (with various encodings like UTF8/UTF16), RTF types, PDF and Adobe types, image types. There's also a bunch of metadata types that describe the application that copied the data (e.g. com.apple.iWork.TSPNativeMetadata), or the features of the copied text (e.g. com.apple.iWork.TSPDescription), and what appear to be separated text and style data (ustl and TEXT as CorePasteboardFlavorTypes, and their com.adobe.indesign-import- equivalents).

I don't know any way of seeing which format types are supported by which applications for pasting, but using the Clipboard Viewer you can quickly see which format types are generated on copying.