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
  • http://www.mrblonde.ca Mr. Blonde

    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!

  • Heeectooor

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

    • http://www.evilonegraphics.com Trizicklo

      I second that.

    • http://bucketothought.com/loungekat/blog/ LoungeKat

      Thank-you for the suggestion :)

  • http://www.jonathanpatterson.com Jonathan Patterson

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

  • http://www.studio27.ie graphic design galway

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

  • http://www.skriftklog.dk Magnus

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

  • http://michaelsagerasfoto.blogspot.com Michael Sågerås

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

    Thanks for sharing/Michael from Sweden

  • http://tn-photographics.com Toby

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

  • http://www.jeremyshuback.com thebulfrog

    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.

    • http://www.ottonova.nl/ Otto Coster

      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.

  • stephanie

    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

      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)

      • http://www.ottonova.nl/ Otto Coster

        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.

    • http://www.ottonova.nl/ Otto Coster

      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.

  • Sue Armstrong

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

  • http://www.think360studio.com/ Think360 Studio – Web Design Company

    Thank you for nice article :)

  • Alessandro Muratore

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

  • Xander

    @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 ?

    • Atelier_Jloupf

      I Xander,
      I’ve made a paragraph style for HTML and CSS using GREP in Indesign. Search “GREP” on graphicriver and you wil find it. Tell me if it’s useful for you. Best regards, Atelier_Jloupf

  • James

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

    This is the most useful piece of code I've ever found on the web. Thank you so much.

  • http://millerdesign.co.uk Jonathan Gledson

    Thanks.

    It doesn’t work for the end of a “story”. is there a way to make it look for last space before the end of a paragraph AND OR a whole story? (shown by an invisible #)

  • Atelier_Jloupf

    Thank you Otto to speak about this cool InDesign tool!

    I’ve adjusted this cool tips to fix some complications:

    • Remove the lookbehind, we really don’t need to know what is before the last whitespace. And it could be a semicolon or a colon. In this case, the regex don’t work.

    •Add some precision in the lookahead : the last whitespace must be before:
    1- One or more word character (w+). You’re right at this point.
    2- After, it could be have any nonbreaking space (~S?)
    3- At the least, we find the punctuation ([[:punct:]])

    The point 2 is to give the possibility to work with a text in other langage than English. In french for exemple, there is always a nonbreaking space before exclamation or interrogation mark.

    At the end, don’t verify if what we’re looking for is before a line break (r) — in this case the regex don’t work for the last paragraph of your article — but before the end of the paragraph ($).

    Here is the complete regex with all this corrections :
    s(?=w+~S?[[:punct:]]$)

    Best regards.

  • Jurand

    for me the best way is to do this: ( )(w+)(?=$|.$|..$) and change it with ~S$2

  • Jurand

    and for Runts is: (s)([lu])( ) and change it with: $1$2~S

  • http://twitter.com/DarioSycco True Metalhead

    Wish I knew how to work this. This should theoretically be so easy. I have a monthly project where I need to layout a flyer in InDesign based on copy coming from Excel. There are headings that I’d like to format using Grep Styles. Looks something like this:

    Name Of The Product 200ml – 300g
    Description goes here. Blah, blah, blah…

    The size of product needs to be smaller than the name, and then the description in different font and size as well.

    I could do all the formatting the old traditional way, but that is time consuming. Is there not a way to assign the style to say “when it encounters a number – switch to smaller font until the end of the paragraph and then apply another font and size? This would make my life so much easier. The way it works (or doesn’t) for me so far is I can get the style to perform the font size change on “200″ but “ml” is back to the large size of the “Name Of The Product”. Wish I could get a better grasp of this.

    Any help would be much appreciated. Thanks a lot.