Web Designer Pro Bundle - $500 of Site Templates, Stock Photos, Code, Graphics and more for only $20
Quick Tip: Automatic Formatting using GREP Styles in Adobe InDesign

Quick Tip: Automatic Formatting using GREP Styles in Adobe InDesign

Tutorial Details
  • Program: Adobe InDesign CS4
  • Difficulty: Intermediate
  • Estimated Completion Time: 1 hour

InDesign’s GREP styles are the magic of automatic formatting. In this Quick Tip we will explain how to set up GREP styles and how you can use them to for a consistent and efficient design.


Step 1: Understanding Regular Expressions

GREP styles are character styles that InDesign applies to a specific part of the text in a document. This part can be a single character, word or a pattern of characters. To specify the part a language called regular expressions is used.

With this language you can describe patterns in the text that need to have special formatting applied. You can do this in InDesign using the GREP Style option in the Paragraph Style Options dialog.


Step 2: Creating a New GREP Style

You can use GREP styles for a lot of things. To start simple, I’m going to use a GREP style to format image captions. Click the button New GREP Style. You’ll see a new style rule appear. First, we need to create a new character style. Click the option menu and choose New Character Style.


Step 3: Setting up the Character Style

For the captions, I’m using Myriad Pro Semibold in Small Caps as my new character style.


Step 4: GREP options

InDesign automatically puts the regular expression \d+ in the To Text field. \d is a wildcard which stands for Any digit and the plus symbol means repeat one or more times. So, as default, InDesign will apply your character style to digits as 14, 001 or 2010.

To see all the options available to construct a regular expression, click on the @ button.


Step 5: Setting up the Regular Expression

I’m going to delete the default regular expression and construct a new one for the image captions. The words I want to format with the new character style are top, left, right, bottom and middle. The expression I’m using is:

^(top|left|right|bottom|middle)\s

You should read this as: paragraph begins (^) with the words top or (|) left or right or bottom or middle, followed by a whitespace character (\s). In this way, only if the words appear at the beginning of a paragraph and are followed by whitespace get the character style applied. You can see the final result below:

Photo by peasap on flickr


Step 6: Preventing Orphans

One of the most useful things you can do with GREP styles is preventing orphans in your layout. An orphan is a single word that appears at the last line of a paragraph. With GREP you can apply a character style with No break checked to the last whitespace character in a paragraph. This will ensure the last line of a paragraph has at least two words on it. The expression for this is:

(?<=\w)\s(?=\w+[[:punct:]]*\r)

This expression uses positive lookbehind (?<=) to check if there is a word character (\w) before the whitespace (\s), and positive lookahead (?=) to check if there are one or more word characters (\w+) and zero or more punctuation characters ([[:punct:]]*) before the end of a paragraph (\r). As you can see, GREP styles can become somewhat complex.


Conclusion

GREP takes some time to master but when you do, you can save a lot of time with automatically applying formatting depending on the text. Various examples of other expressions can be found on the web. Please tell us in the comments which GREP styles you're using!

Otto Coster is ottonova on Graphicriver
Tags: Tips
Add Comment

Discussion 19 Comments

  1. Mr. Blonde says:

    Hey Otto thanks for this I had no clue this existed. Getting rid of damned orphans will now be a breeze. I thought the only way to do it was by hand!

  2. Heeectooor says:

    I think a video tutorial would be much more clear in showing how these function works.

  3. I’ve never heard of this feature either. Good to know!

  4. it’s a nice feature but like many others i’ve never used it. i’m using xml tagging instead

  5. Magnus says:

    It is an excellent feature I use for a whole bunch of repetitive stuff. Good idea eliminating the orphan with GREP styles.

  6. Excellent!:-)
    But as someone said, do a videotut thats explain how it´s works.

    Thanks for sharing/Michael from Sweden

  7. Toby says:

    Thank you very much for this tutorial, would be nice to see more stuff about GREP-styles!

  8. thebulfrog says:

    Whoah – this is huge. Thanks for sharing. I wrote a massive tutorial once, where I had to go through and edit the text individually to change the style of all of the drop downs (which I made bold) and the tools (which I set in a different font) If I had known about this, I could have automated the whole process. I’m not great at Regular Expressions, but I’d rather spend the time learning them, then searching through my text for each individual instance.

    • Otto Coster says:

      GREP does exactly that, saving you a lot of time. Most of the things you can do by hand in short documents, but when the page numbers increase you’ll need a more efficient way to do things.

      You can also do Find & Replace with GREP, that’s not something covered in this tutorial but when you know regular expressions, you should have no problems using that as well.

  9. stephanie says:

    Thanks for the tutorial! really got me into GREP…

    But i still got a problem eliminating those orphans with GREP – mainly i copy the GREP sequence, but it does not work!
    My question is: Why does it check if there are “one or more word characters” after the whitespace before the end of the paragraph, if what i want is it checking for ONE word characters after the \s befor the \r.
    if there would be more than one word characters after the whitespace it would not be an orphan-situation…?

    i think i’m getting someting wrong, because i tried the GREP in various examples but it doesn’t work for me.
    Would you mind to clarify?

    Thanks a lot in advance!

    • Jackie says:

      I’m having the same problem. In the GREP dialog box there are 2 fields. One is “Find what” and the other is “Change to”. Do I need something more for the GREP orphan style to work other than:

      (?<=\w)\s(?=\w+[[:punct:]]*\r)

      • Otto Coster says:

        I’m having the same problem. In the GREP dialog box there are 2 fields. One is “Find what” and the other is “Change to”. Do I need something more for the GREP orphan style to work other than:

        (?<=\w)\s(?=\w+[[:punct:]]*\r)

        You shouldn't use the GREP Find and Replace function for this, but the GREP functionality in the paragraph styles. Try it with the Basic Paragraph style, it should work.

    • Otto Coster says:

      My question is: Why does it check if there are “one or more word characters” after the whitespace before the end of the paragraph, if what i want is it checking for ONE word characters after the \s befor the \r.

      You want it to match a complete word (multiple word characters), that why the \w+. If you used \w, it would not match a word before the end of a paragraph.

  10. Sue Armstrong says:

    I copied the coding in your tut and am having the same experience as Stephanie—the widows are still there. Any solution?

  11. Alessandro Muratore says:

    I do this, but it doesn’t work. Why?

  12. Xander says:

    @Author:

    Hi there,

    Thanks for the info. I’m a webdeveloper and mostly I do not use Indesign. So this is very usefull.

    I found this article while I was looking for somebody who made a book with code examples, because
    I want to know how to have special paragraph styles that hightlight and also color the
    samples of PHP code or any other code.

    Im writing a manual which includes HTML, XML, PHP and Javascript. It would be nice to color the code.

    Do you know howto ?

Add a Comment