insert a textbox in scrivener

Hello,
I am writing a programming book and I would like to insert programming code inside a textbox with a background colour.
Could you help with this issue, please!!

Thank you in advance for your help!!

Someone might have a much better method than this, but I would make a single-cell table with a one-pixel border and fill it with whatever colour you like.

Tables in Scrivener can be a little temperamental (courtesy of the OSX coding, I believe) but I’ve found they behave if they are kept simple.

It’s also possible to nest one table inside another but perhaps this is asking for trouble…

A single-cell table works fairly well for code blocks and call-out boxes. You may note the interactive tutorial uses these in a few places (scroll down in Step 17 to the yellow box to see an example of a call-out).

But personally I prefer a more flexible approach. Tables can be a bit of a pain to work with (and that isn’t exclusive to Scrivener). Having a block of text that is instead just treated special, that can be styled using layout tools dynamically, will in the long run probably be much better for you. After all, what happens if you get a editor feedback about using a shaded box and you decide to take their advice and go for another look? If you used hundreds of single-cell tables with a background fill, you’re in for a huge amount of work. If this was all done via a stylesheet in Word, it’s a few button clicks to take the shading out.


[size=80]Code blocks from Scrivener to Word[/size]

(Bear with the fact that I messed up the first tab stop in Word so that it is barely visible.)

Here is an approach that, on the left side at least, is very close to how I work. That blue box is coming from Format/Formatting/Preserve Formatting command in Scrivener. That will instruct the compiler to leave the formatting of the contents of the box alone, leaving the rest of the text outside of the box agile (in my example, the text goes from Cochin to TNR 12pt, but not the code block, that stays fixed width). So long as the formatting in here is unique, but consistent between all code blocks, it is then trivial to select them all in Word and apply a prepared stylesheet, as show in the example on the right.

To make things easy on yourself, you could set up a code block to look the way you want it to look while working on it (forgetting about its final look perhaps), and then use the Format/Formatting/New Preset From Selection menu command. This will save the blue box, as well as any other characteristics you choose to preserve (I’d recommend them all since you’re using this for its distinct formatting). Now whenever you need to paste in a code block you can do so, select the text and apply this preset and move on.

As a side benefit, at least in my opinion, the blue box stands out, making it easy to spot code blocks while scrolling—and you can also step through your code examples with the Edit/Find/Find by Formatting… tool, searching for “Text with Preserved Style”. You can’t do that with tables.