Fun with GREP #2
In a previous tip, I explained a little bit about GREP and what it can do. In that example, we were using GREP to do a find/change search on text. Since CS4, GREP can be built directly into styles. Keep in mind, the GREP that you layer into a style can do merely that: style text. It can’t return a different order and add or replace current text…that’s what GREP in find/change is for.
I’m going to show a couple of examples of using GREP in a style to speed up your work and make formatting so much easier. After that, I’ll explain the step-by-step for achieving each style. If you haven’t worked with text styles, get familiar with those before attempting GREP in styles.
A great use of GREP is to add style to numbers in your text. For instance, you might want to have all numerals be Old Style and you might want numbers formatted as fractions to be displayed as actual fractions:
The examples above are achieved using OpenType fonts, which have extra styling options for things like Old Style and Fractions.
Let’s look at how this was achieved with a Paragraph style.
In the New Paragraph Style dialog box, under the GREP Style pane, click New GREP Style. Next to Apply Style, choose New Character Style from the drop-down list.
Create a Character style called Old Style. The only styling you’ll apply will be under the OpenType Features: choose Proportional Oldstyle from the Figure Style drop-down menu.
In this Paragraph style, we’re also going to build in the fractions functionality…so this style will have all numerals as oldstyle and any numerals that look like fractions converted to actual fractions. So, click New GREP Style again and create a new Character style (if you haven’t built them all ahead of time). This time, under OpenType Features, check the Fractions checkbox. Call this Character style Fractions.
Now we need to tell InDesign what pattern of type to look for so that it can apply our new Character styles on top of the Paragraph style we’re creating. In the first GREP style (Old Style), we need to tell it to look for any digit: the GREP expression for any digit is \d and the expression for one or more times is +. So, in the GREP Style pane we’d enter: Apply Style: Old Style To Text: \d+
In the next spot, we need to tell it to Apply Style: Fractions To Text: \d+/\d+ (which means: any digit, one or more times, followed by a forward slash to create the fraction, followed by any digit, one or more times)…like so:
Sample text before:
After creating the rest of the Paragraph style, remembering to use an OpenType font, and applying it to the text:
So, if we style text with this style, any time we type a number, it will automatically be styled with oldstyle text. If we create any string of numbers with a forward slash in it, it will be formatted as a fraction.
In this final example, we’ll create a style to use when typing prices, for instance on a sale flyer or a catalog.
Create a new Paragraph style and call it something like Prices. You’ll also need two Character styles, either formatted ahead of time, or on-the-fly. Call them something like Dollar Sign and Cents (for US price styling). In the Dollar Sign Character style, the only attribute should be to choose the Position “Superscript” under Basic Character Formats. For the Cents Character style, choose the same (although you may want special formatting for each of these styles).
In the GREP Styles pane, choose New GREP Style. Apply the style “Dollar Sign” to any dollar sign using the GREP expression \$. Then create another New GREP Style and apply the style “Cents” to any two digits immediately after a period. To do this, you’ll need to use what’s called a positive lookbehind, which is expressed in GREP as: (?<=). So to tell it to look behind (or after) the decimal point, we express it as: (?<=\.) and we follow that with the expression for any two digits: \d\d. So, the final GREP expression for the cents is: (?<=\.)\d\d
Here’s the before:
And the after:
Don’t let the looks and sound of GREP scare you. Do some online searches to find GREP expressions, because chances are someone else has already come up with the expression you need. And remember, once you create an expression (and you’ve tested it to see if it works) you can use it over and over in other styles and share those styles among documents. There are even InDesign GREP Cheat Sheets floating around out there that just deal with the subset of expressions that InDesign uses. Once you see how much time and energy you can save using GREP, you’ll kick yourself for not starting sooner!







