How do I enter computer code (monospaced, indentation preserved)?

I expected “Code Block” and “Code Span” to do what I wanted, but these are apparently not for entering computer code into a book. (What are they for?)

“Code Span” gives me a nice monospace font within a line. But when I compile, the font isn’t any different from the surrounding text. (I tried marking “Code Span” with “Preserve font,” but that didn’t seem to do anything.)

“Code Block” gives me a nice indented block with a monospace font and line indentation as I want it. When I compile, the result is monospaced but all lines begin in the same column.

In short, both look fine in the editor but not when compiled. I’ve scoured the compile settings, searched the web, and tried to search L&L, but without success.

Using the Code Block style will indent the code like a Block Quote style would, with a monospaced font but with all lines left-justified.

To solve the indentation problem, indent with non-breaking spaces (Insert -> Break -> Non-Breaking Space) instead of ordinary spaces.

On the Mac, you can make it relatively easy to type a non-breaking space by going to Apple -> System Preferences… -> Keyboard -> Shortcuts -> App Shortcuts, and choose something like Option-Space for the Scrivener app. For longer code blocks, it is probably best to use a good text editor to do the substitutions.