View Full Version : Help formatting Source Code

07-14-2011, 02:27 PM
I am hoping that someone can point me to resources and how to's on how best to format computer source code for pub. Basically I have several programming books which have embedded blocks of source code that I wish to keep the formatting for. Is there a "best practice" for this? The original material is in MSWord and the book layout is being done in InDesign CS5.5 if that helps.

07-14-2011, 06:06 PM
The obvious answer is to wrap the code in a <pre> tag. This will set the font to monospaced and preserve all the whitespace and line-breaks. You can set the text to wrap if a line is too long for the display by using the following css:
pre {
white-space: pre-wrap;
This will wrap the code in ADE at least (most ereaders), and should also do so on the iPad. The problem is it looks a bit ugly, as the code wraps from the left side of the following line.

If you want to do this is InDesign, then you're making a lot more work for yourself, since you'll have to assign each line the correct level of indent. On the plus side, this will allow you to give the lines hanging indents so long lines wrap under their parent. You will need to edit the css after export to add font-family: monospaced; to the relevant selectors.

07-15-2011, 02:50 AM
"how best to format computer source code for pub."

I'll drink to that! :beer:

07-15-2011, 08:06 AM
I think you're best of creating a CSS style for that.