Take the 2-minute tour ×
Graphic Design Stack Exchange is a question and answer site for Graphic Design professionals, students, and enthusiasts. It's 100% free, no registration required.

I am currently creating a book layout with inline code and code-blocks.
For those code parts, i would like to add a background-color. It should handle code similar to the way it is displayed on StackExchange.

Codeblocks

As there is no option in paragraph-styles, that would add a background, I tried to use paragraph rules, underline rules, strikethrough rules and some other experimental approaches.

Strikethrough rules
The striketrough rules are displayed above the text, making it completely useless for this purpose.

Underline rules
Underline rules are displayed under the text, which makes it possible to use it as a background. The problem is, that it depends on the character-length. That makes it possible to use it over lines that have an automatic line-break.
The problem is, that you can’t add margins to it and that you can’t insert a background with a fixed width.

enter image description here

Paragraph rules
Paragraph rules are pretty close to a solution. You can determine, if you want the line above, or below your text, you can add margins and the line fills the whole width of the block. The problem here is, that it is a paragraph line, meaning that it adds a rule for the whole paragraph instead of each line. That is no problem if every paragraph is only one line high, but if a paragraph that includes multiple lines, you end up with a gap in the background. I could add a forced line-break here, but I would like to avoid that.

enter image description here

A solution would be, if I could add a forced line-break on the end of each line through a GREP-style, but I haven’t seen any way to add a character this way.

Inline Code

I am using character styles for inline Code. Underline rules seems to be the most appropriate way to go here. The problem is, that I am massing margins here. If I add a space on the beginning and the end of the inline code, I end up with a nice result, but that means I have to add characters to the text, which I would like to avoid and it would not work on the beginning and the end of a line-break, because inDesign deletes the character if it is not needed.

enter image description here

I am searching a way to add some space before and at the end of the inline code.

So, that would be it. I am searching solutions to this problem for a long time now, so any ideas would be appreciated.

Oh and sorry for the crappy image quality. The missing retina display support of inDesign made it possible.

share|improve this question
1  
Table cells? At least for the code blocks. Won't work for inline text though. –  Scott Mar 25 '13 at 17:41
    
Combine table cells and inline text. You have two different problems, so you need two different solutions. Make the styles match, but the execution is different for blocks and for inline. –  Lauren Ipsum Mar 25 '13 at 19:11
    
Thanks, I haven’t thought about using tables. It would work, but I would have to define a paragraph style, a table style and a cell style to do it the way I want to have it. And additionally, I have to create a table for every codeblock. I would say it is a solution, but it feels like a hack. A more comfortable way (only paragraph styles and character styles) would be much nicer. @Lauren: Inline tables are not possible as far as I know and with character styles I am still struggling with margins. –  Afterlame Mar 26 '13 at 12:27
    
You can use text boxes inside textboxes. Saves you at least some table styles –  tim human May 13 at 13:46

2 Answers 2

I used table for my own report and it worked really good for me. For every code block that I needed, I just did a copy/paste of previous table. For syntax highlighting , I defined character style first but it was tedious to apply those styles for every pieces of code. Therefore just copied the code from IDE, preserving the color and font, to InDesgin. Here is how it looks like:

InDesign Code Block

The only problem is that you cannot copy a block of code and you have to copy and paste line by line since every table row represents one line of code.

share|improve this answer

You can use a paragraph rule to block in the whole measure or the text underline to highlight just the text. The trick is to get the offset just right then set a width value that matches or exceeds your leading value.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.