Dec 312011

(Okay, this is excessively nerdy talk meant primarily for the InDesign-heads out there. It’s also incomplete. There are a TON of reasons I love GREP styles, but here I’m gonna focus on one.) A few versions back, InDesign added these things called GREP styles, which use the pattern-matching power of regular expressions to cause formatting to happen intelligently and automatically inside of a paragraph style. I used to be a Perl jockey, and that programming language really sings when you get cozy with regular expressions, so when this stuff started showing up in InDesign I was pretty damned happy. With the layout work I’ve done for Hero Games, I’ve used GREP styles to take care of a ton of the formatting that you see in stat-blocks, which is a huge boon. I did similar when doing D&D 4E statblock layout for One Bad Egg. This, however, is not about statblocks. It’s about using GREP styles to perform little tweaks to your header styles. So I’m looking at the Atomic Robo logo. It’s swanky.

And I’m wondering — is there a font that matches this? Clearly there’s some custom type design in the logo, but it’d be nifty if I could put together a header style that did a solid job of matching its look. I do some searching around via things like WhatTheFont, and I come across a pair of fonts that are sort of a match: Melrose Modern One & Two Okay, so Melrose Modern One is a bit closer of a fit, and so I start with that as my header style, getting something like this: Now the hunt for mismatches begins. The A isn’t rounded in One, but it is in Two. The I has serifts on it in One, but is a straight simple line in Two. The M isn’t really a match in either, so I have to set that aside as “it’d be nice, but this will be close enough”. Obviously the lightning bolts are added after the fact, so if I want those, I’ll have to hand-craft them. And the O in Atomic is simply different from the other O’s in the logo — but it looks a lot like the O in Two. I can solve this problem with the addition of two GREP styles (I could probably solve it with one depending on how far I wanted to go, but two will be easier for the teach, here). First, let’s look at the A and the I. I’d like these to be in Melrose Modern Two whenever I type them in the header. Prior to GREP styles, I’d need to find all the instances where I did, and apply a character style that makes them use the Two font instead of One. I’ll start by setting up a character style (“Mel 2”), because that much hasn’t changed. But to apply this effect simply and automatically in my header, I’m going to do this with a GREP style. I edit the paragraph style that I’m using for this header, and I go into the GREP Styles pane of it for the details. I create a new style that applies my character style, Mel 2. For the pattern, I want to do a single-character match for these, and I want it to apply whenever it encounters an A or an I (and as I mess around with it later, when it encounters an R and an N too, so I throw those in for good measure). My pattern is simple:


That’s regular expression speak for “a single character that is either uppercase A, I, N, or R”. What’s the header look like after I make that one change to the paragraph style, without me directly applying any character styles? Much better! And for a workaday header that feels consistent with the logo that inspired it, it’d work pretty much as is. But that O in Atomic hounds me a bit. It’d be fun if every time I typed “ATOMIC”, I got the alternative O from the Melrose Modern 2. I set up another GREP style in my header’s paragraph style, same as the first, but with a different pattern. This one’s a little more complicated — I want it to know that it’s “inside” of the ATOMIC word, but I don’t want it to apply my character style, Mel 2, to the other letters in the word — just the O. So I need it to be able to look to the left and the right of an O, and see if the letters around it spell AT MIC. That’s a concept called “positive lookbehind” and “positive lookahead”, which is a fancy way of saying look and verify, but don’t touch. In regular expression speak, that’s:


Look to the left: is AT there? That’s the first part. Look to the right: is MIC there? That’s the last part. The actually-matched thing, the O, that the character style is applied to, is in the middle. (Important tip: these are case sensitive by default. You’ve got to fiddle with the dials a bit if you want case insensitive.) If I use Mel 2 as my character style, I get this:

Boom! If I really wanted to go nuts on this, I could create a copy of Mel 2 that was maybe 10-15% bigger in size, with a baseline adjustment so the big O would drop down a bit below the other letters on the line, but that probably won’t look as good as I’d want it to because the weight of the O would change as it increased in size. So for now, with the font that I’ve got here, this is my best fit. (If you’ve been paying attention, you’ll see that before all of this I did a font size override on Robo to make it smaller than Atomic, and gave it some different spacing settings, but that’s outside of scope for what I’m demoing here.)

Now, that might seem like a lot of trouble just to do a few letter substitutions in a single logo or header when I could simply apply the character style a few times and be done with it. But as with anything involving regular expressions, the real power comes in when you have to do something a few hundred or more times. With the GREP Style enabled header I’ve made, I can just type FRED DEFINITELY LOVES THOSE ATOMIC GREP STYLES and I get all my A’s, I’s, N’s and R’S substituted out all proper, without having to do anything other than apply the paragraph style to the text — here’s a before and after, without and with the GREP styles involved:

This isn’t always about one font for another font, of course. It’s all in what you do with the character style that you’re applying. Maybe you’re just changing the weight of the same typeface; maybe you’re giving it a different color or a slightly different size; maybe you’re turning on or off certain OpenType features (like swashes) — I did a lot of that last bit with the headers for the Dresden Files RPG, because its header font, Newcomen, has some crazy-great OpenType features, but not always ones that I like consistently turned on or off for every letter or letter combo.

GREP styles are incredibly versatile, and with a few smartly constructed patterns, you can save yourself a lot of work and cause your text to conditionally format itself with a single click.

Can’t recommend them enough.


  9 Responses to “Why I Love GREP Styles”

  1. Out of curiousity, do you have a techie background, and thus a previous familiarity with regular expressions? (Regular expressions would be the pattern language InDesign uses for grep styles.  Based on your examples, it looks like they’re derived from Perl’s implementation, probably PCRE.)  As a programmer, I use them frequently, but had always viewed them as too cryptic for general use. I’m happy to know that InDesign has them, but I’m curious if they’re really only used by people with techie backgrounds, or if others

    • Well, like I said above (perhaps too briskly), I used to be a Perl jockey, so, yeah, techie. My editor of choice is vim. 🙂 But I really hope the less techie background folks out there who use InDesign will still look into what regexes can do so they can make the most out of GREP styles. (It also has a nice dropdown menu for constructing them, but you still have to understand the resulting syntax.)

    • Doh! Indeed you did, and quite clearly. Apparently I glossed over it too quickly in my eagerness to see you hacking the logo together. Thanks!

    • My experience is that non-techie ID users are still making use of basic GREP styles, especially in heavily-templated environments like catalog production. There’s a very good PDF book from O’Reilly that is suitable for InDesign users who have never encountered GREP before or just need a good refresher/reference:

  2. That’s darn spiffy and a great time saver. Have you got any examples of a RegEx that went slightly skew-if in a hilarious fashion or are you well tight with your greedy expressions?

    • Not that I’m calling immediately to mind — but GREP Styles in action don’t spread beyond the individual paragraph in which they trigger. Sometimes I can end up with an automatically formatted inline header that grabs more than I want it to, but that’s not particularly hilarious. 🙂

      One trick I like doing, too, is making sure that ‘XX’ always shows up in magenta when it’s encountered, so the places where a “see page XX” occur are extra-obvious during proofing. That can get a little hairy when “XX” also occurs naturally in a character’s name…

    • The trick to that one is to have the GREP style look for ” XX” or “XX-XX” — variants needed depending on how you handle page range references, but I can’t think of a style that doesn’t have a space before the page number.

    • yep, totally fair. And honestly, I usually have it highlight the “page” or “pg” beforehand, too, if it’s there — bigger visual footprint. Though doing a “find” for “character color = magenta” is a good want to catch all the places it’s firing off regardless of the breadth of the footprint.

  3. […] in the formatting, I caught a post from Evil Hat’s Fred Hicks about using Indesign’s intelligent styles. It really helped save me from headaches, and I just wanted to tip my hat to one of the people who […]

Sorry, the comment form is closed at this time.