Listing source code in documents

Forgive me if this a obvious question, but is there a way to insert source code listings with syntax highlighting in a scrivener document? I’m thinking of something like listings or minted: … /listings/


Code blocks are probably best done using the [b]Format/Formatting/Preserve Formatting[/b] feature. This will draw a blue box around the selected text and protect it from the compiler’s reformatting features. By default, any formatting or font adjustments, like using a fixed width font, you make within the blue box will come through verbatim in the final copy. What I’ve done for myself is create a Preset that already has everything set up the way I want, including the Preserve Formatting field. Then when I need to insert a code block, I just use the Preset menu to select the right option. To make a preset, just set up everything exactly how you want it to look in the editor, put your cursor anywhere in that paragraph, and use [b]Format/Formatting/New Preset From Selection...[/b], choosing as many attributes as you want to save, into the subsequent dialogue.

[size=80]Code block example appearance[/size]

One special thing to note: I’m not sure if you are using MMD, but since the examples you provided are LaTeX, just in case you are, you’ll want to be aware that a Preserve Formatting block automatically inserts a prefix tab to every line within it—invoking MMD’s syntax for a code span. So if you have things already indented with tabs in the editor, you don’t want to add a Preserve Formatting block around it. It’s useful if you want to avoid syntax as much as possible, though. Otherwise this field has no purpose in a plain-text document as no fonts or ruler settings survive the transfer to .txt anyway.

Finally, as to syntax highlighting: nothing like that. It’s a pretty niche thing, and has the potential to really bog down the editor. Most programs that feature syntax highlighting are not rich text. Rich text must use a different set of commands for temporary colour application, and this is quite a bit slower than actual text colouring, which you’d want to avoid in rich text as these can be used for other things and only one colour can be applied to a particular span of text.

Thanks, that was very helpful.