Formatting Source Code

I am using Scrivener to write a programming book.

As a result, I want to include source code listings.

How to I mark a section of text as being a chunk of source code?

To explain it differently, in MS Word, I’d merely define a style (Courier New for the font, etc. ) and then highlight the code and apply the style.

What is the equivalent or parallel to doing that in Scrivener?

Do I need the MultiMarkdown module?

I searched the forums here for an answer, but found nothing. I suspect that I don’t know the right “terms of art” in the Scrivener world.

Thanks for any assistance.

I have the exact same question - in what ways can this be easily done?

I have already made an text document with the different formatting (font, line distance, spacing), and now copy that into the document I’m writing so that I can give some part of the document another style without having to manually change all the options, but this is quite tedious and breaks my writing ‘flow’.

What workarounds do other users use for this?

Since Scrivener doesn’t yet have formatting presets, which will be the easy way to do this down the road, you could do a couple things depending on how you use the differently formatted sections in context. Using the Format > Font > Copy/Paste Font command will let you copy the character formatting from one section to another, so you could format a chunk the way you want it and then copy the styling and just paste that as necessary onto new chunks while you’re writing. (There’s also Format > Text > Copy/Paste Ruler if you need that.) One important note with this method is that since you’re doing this formatting directly in the editor, you’ll not use compile to do any of the formatting at that stage so as not to override what you did. Thus you’ll want to make sure that all your formatting in the editor is how you want it to compile. You can still add things like title text and “Chapter” prefixes and such during compile, but the actual text in the documents should be formatted beforehand.

If you’re going that route of formatting in the editor, you might also want to consider using script settings for this. Switching to script mode would mean you didn’t get the word count in the footer (though you can still get the count via Project Statistics or Text Statistics), but it would somewhat automate the process of moving between the formatting types. Check out the Undergrad Essay project template from the Non-Fiction category to get an idea of how you could use script settings this way–in that example, there’s a basic “main text” element format and a separate “block quote” element with different formatting, and it’s easy to move back and forth just using Tab and Return. You could set up something similar for your standard text and code blocks.

Alternatively, if the sections of text that need different formatting are their own paragraphs, you could split them into their own documents and put them at a separate level in the binder so that when you compile, you can apply a certain format rule to the main text pieces and a different one to the “code” pieces. Doing this, your binder might looks something like this:

Draft

  • Chp 1 (folder)
    • text (doc group)
      • code snippet (doc)
    • text (doc)
  • Chp 2 (folder)
    • text (doc group)
      • code snippet (doc)
    • text (doc group)
      • code snippet (doc)

In this example, the two chapter folders are at Level 1 in the binder (immediate descendents); the main text documents are all Level 2, and the code snippets are all Level 3. Thus, although the main text uses both single documents (no subdocuments) and documents groups (the document has subdocuments), it can still be differentiated in compile from the code snippet single text documents, because of the different levels. You can then use any kind of formatting in the editor when you’re drafting and clean it all up at compile time by applying the specific Courier font and size, etc. to Documents Level 3 and your other main text format to Documents Level 2 and Document Groups Level 2. This is all handled in the expanded Compile dialogue (click the blue arrow to the right of the “Format As” pop-up in Compile if you haven’t seen this interface) in the “Formatting” section.

Thank you, that is an excellent idea and quite a convenient way to paste the formatting over a selected text.

Thanks for these additional suggestions & tips and tricks MimeticMouton - I’ll try them out to see how they ‘fit’ into my routine and documents. Even if they don’t ‘work out’ for me, the ‘paste formatting of text’ option is already very helpful and a big improvement.

I owe you a big latte for this helpful advice. :wink: