Embedding code snippets in iBooks Author

Solution 1:

There is a way more simple way to do this... I'm using iBooks Author 2.0 it has a "Paste and Retain Styles" option that works like a charm.

I edit the code snippets in Xcode then select and copy the section I want: enter image description here

Then control click where I want to paste it into my book: enter image description here

Done! The snippet is included with syntax highlighting: enter image description here

Pro Tip 1: Take time to set up Xcode to use the font you want so you don't have to fiddle with it once the code is pasted into iBooks Author: enter image description here

Pro Tip 2: Here are some other useful settings for your code snippet paragraphs, keep the lines together and disable auto-hyphenation.

enter image description here

Solution 2:

I just tried the trick I use to get syntax-highlighted code into Keynote, and I think it works, though I don't have an iPad handy to export the book and try it.

  1. Edit the code in MacVim
  2. Make sure syntax highlighting is on:
    1. Hit escape
    2. Type ":syn on" and hit return (don't type the quotes)
    3. If you don't see syntax highlighting, you may have vim mis-installed or are using a language that doesn't have a highlighting file installed by default; search github and you'll probably find something
  3. Export the buffer as HTML
    1. Hit escape
    2. Type ":TOhtml" and hit return (don't type the quotes)
    3. This will open a second buffer with the HTML of your text in it. Save it via ":w" and hitting return
  4. Open the file in your browser
  5. Copy the contents
  6. Paste inside iBooks Author

When I did this, I got a text box with the highlighted code in it.