Members Only

Adding Section Heads in an InDesign Index with GREP

With GREP, it’s never too late to organize content with headers in InDesign

This article appears in Issue 79 of InDesign Magazine.

If you ever have to add section letters to an existing index in InDesign, you can do that quickly using a GREP query. 

You use a single query: Find what: ^(\u).+\r(\1.+\r)+ and Change to: $1\r$0. Translation: match and capture a capital ((\u)) at the beginning of a paragraph (^), and then match all following characters in the paragraph (.+) up to and including the return character (\r). Then match a letter that’s the same (\1) as the one we captured earlier, followed by all characters up to the end of that paragraph (.+\r), group that, and find as many as possible of the same (+). In the example above, ^(\u).+\r matches Barbera 14 (and the return character), and (\1.+\r)+ matches all following lines that start with B. And there’s your section. 

Now, to insert the section letter (which is the letter we matched by ^(\u)), we replace the section with the letter we captured ($1) followed by a return (\r) and the entire section ($0)—remember that $0 stands for “everything that was matched by the Find What expression.”

If you want to apply a paragraph style to the section letters, e.g., to add some space before and a font style, that has to be done using a very simple, separate query. At Find What, enter ^\u$, and in the Change format panel, enter the paragraph style—in other words, apply a paragraph style to all one-letter paragraphs. Make sure the Change To field is empty, and then click Change All.

The drawback of this method is that it makes a mess of any formatting, such as italics. You can convert formatted text to text tags, insert the section letters, and then convert the text tags back to the formatting.

Bookmark
Please login to bookmark Close

Not a member yet?

Get unlimited access to articles and member-only resources with a CreativePro membership.

Become a Member

Comments (2)

Leave a Reply

Your email address will not be published. Required fields are marked *

Loading comments...