Keynote tips: syntax highlighting
Vladimir Dementyev

Vladimir Dementyev @palkan_tula

About: A mathematician found his happiness in programming Ruby and Erlang, contributing to open source and being an Evil Martian.

Joined:
Dec 5, 2017

Keynote tips: syntax highlighting

Publish Date: Aug 4
1 0

It's been 9 years since I started my public speaking journey. Different cities, countries, and continents, presenting in front of a handful of people eating pizza or giving an opening keynote for hundreds of engineers, shaking like Elvis, and almost oversleeping my talk. The only thing that stayed constant—I used Apple's Keynote to craft all of my ~40 slides.

Keynote is a popular presentation software (among Mac users), no doubt. Still, many software engineers prefer other, usually Markdown-driven, tools for building their slides. Why so? Partially due to the everything-should-be-code lifestyle, but I think most are attracted by the simplicity with which you can add beautiful code snippets to your presentation. In Markdown, you just write code and add the language specifier!

How can one add highlighted code snippets to a Keynote presentation? Let's do a quick overview of options, finishing with the one I've ended up with.

Option #1. Screenshots

The first option is trivial—just take a screenshot of the code editor and paste it into your slide (that's how I started my Keynote journey). Simple? Yes. Maintainable? Not really.

Still, it's an option (a last resort one). If you have to do that, consider using some specialized code-to-image tool like carbon and not just crop an image of your editor.

Option #2. HTML

Did you know that Keynote can consume HTML content? Copy some HTML and paste it onto a slide, and you'll see it being transformed into a text box with styles preserved. Compared to screenshots, you can edit the text, change its characteristics (e.g., size), and so on.

The question is: where to get an HTML version of the code?

If you are a VS Code user, you can just copy the code from the editor. That's it. VS Code automatically saves an HTML version into the clipboard buffer.

If your editor copies only the plain text (I'm looking at you, Zed), there is Code to Markup at your service!

Although it is as simple as screen capturing, this approach has a couple of downsides. First, you have to switch between Keynote and an editor/website to highlight code snippets (not-so-ideal UX).

Another thing that bothers me is the fact that aforementioned (maybe, all similar?) HTML generators preserve background color for code lines but not for gaps between them. That results in the following representation:

You should either adjust your slide background or manually update it.

Option #3: RTF & Automator

HTML is not the only format that can preserve code styling. There is also RTF (Rich Text Format). It is not as expressive as HTML, but it has everything we need to highlight code snippets. And from my experience, pasting code-as-RTF turned out to work the best with Keynote. And this experience is based on the setup described below.

First, to turn a plain text code into its highlighted RTF representation, I use the tool called (surprise!)... highlight! It's a very old one, but it still works perfectly (people used to know how to write software that lasts).

You can install it with Homebrew: brew install highlight.

It's a command-line tool, our target code snippet lives on a Keynote slide—how to connect them? Using Automator.

Automator is a macOS tool that allows you to customize your OS features. For example, you can create a quick action—an action that can be triggered either from the context menu or via a hotkey combination. Do you see where I'm going? We will create a quick action to get selected text, turn it into RTF, and paste it back where it belongs. This way, we can highlight code in Keynote by pressing a key combination!

Here is a step-by-step how-to:

  • Open Automator, go to New -> Quick Action.

  • Select "text" in "Workflow receives current" (we will operate on the selected text). You can also limit the visibility of this action to particular applications (say, Keynote).

  • Open the action library and add the "Copy to Clipboard" to the workflow:

  • Add the "Run AppleScript" action:

  • Update the script as follows (feel free to tune the options to your taste):
on run {input, parameters}

    set command to "/opt/homebrew/bin/highlight --out-format rtf --syntax=ruby --style base16/chalk  --font \"Martian Mono\" --font-size 32"

    do shell script "/bin/bash -c 'pbpaste | " & command & "| pbcopy'"
    delay 0.5
    tell application "System Events" to keystroke "v" using command down
end run
Enter fullscreen mode Exit fullscreen mode
  • Save the workflow and go to Keynote to give it a try!

  • Go to System Settings -> Keyboard -> Shortcuts -> Services -> Text, find your action, and assign a keyboard combination to it. Now, you can highlight any code in Keynote by just pressing the specified keys!

This workflow is not ideal, but it has served me well for many years. You have to hardcode the theme, the font size, and the language (or have multiple actions). But you do that once per presentation (so, a few times a year max)—more than "good enough" for me.

Sometimes the magic "tell application" command doesn't work, and I have to hit Cmd+V myself—again, not a big deal.

I'm pretty sure the setup could be optimized further: highlight replaced with some fancy and faster Rust CLI; AppleScript with JavaScript (why not?); maybe, Automator gurus know how to add options selection (language, theme) to the context menu. The core idea would stay the same—and that's what I wanted to share with you in this post.

Happy conferencing!
Share your Keynote vs. code tips!


P.S. How to know what's been stored in the clipboard buffer after you hit Cmd+C? Run the following command in your terminal:

osascript -e 'the clipboard as record' | less
Enter fullscreen mode Exit fullscreen mode

Comments 0 total

    Add comment