InDesign CS3 introduced the ability to perform GREP Find/Changes, making it easier for us to apply character styles to text pattern strings such as ‘all text’ between parentheses. InDesign CS4 continued the “GREP-theme’ through its ability to lock this pattern based application of character styles into your Paragraph Styles.
The following tutorial is part of a 4-part series on GREP Styles.
Part 2: Change the appearance of numbers (figures) within a given paragraph style
Part 3: Converting text typed in Capital Letters to Small Caps (OpenType feature)
Part 4: Formatting price tags containing dollars and cents with multiple character styles
In this first tutorial we start with a simple GREP style tutorial that looks at how we can automatically apply a character style to all text between parentheses.
Task at hand
All of the paragraphs for which the text between parenthesis must be set in Italic have already had a Paragraph Style “Body Text” applied to it.
Define a Character Style
In preparation for the GREP style, I prefer to create the Character Styles required prior to defining the GREP style components (Character Style and Expression).
To define a Character Style: Highlight some text in the Body Text paragraphs and change its formatting. I’ve changed the Font Style to Medium Italic.
Next choose New Character Style from the Character Styles panel menu and Name the Style. I’m naming mine “Medium Italic”.
Click OK to add the style to the Control Panel. I’m purposely not clicking “Apply Style to Selection’, as the text I formatted with Medium Italic is only used as the basis for the Character Style definition.
Because we set some of the text in Medium Italic (Font Style), our Paragraph Style now indicates with the ‘+’ icon that this format is not native to the Paragraph Style. Let’s remove this format override, by clicing the Clear Overrides icon at the bottom of the Paragraph Styles panel.
It is now time to add the GREP Style to our Paragraph Style.
GREP Style
Right-click the Paragraph Style and select Edit “Body Text” to edit the style. The Paragraph Style Options dialog appears. Click GREP Styles.
First of all lets set the Character Style that we’re going to apply to the GREP Expression we’re inserting in just a moment. From the Apply Style drop down choose the Character Style created earlier.
The Character Style we want to apply is “Medium Italic”.
Oops did you forget to create the Character Style? Don’t worry, from InDesign CS4, you have the ability to create your Character Style right here in the Paragraph Style Options dialog. Have a look at the bottom of the Apply Style pop-up and notice the “New Character Style”.
GREP Expression
It’s time to get GREPPING… so what was that pattern again we were looking for? An Open Parenthesis character, followed by any type of text (alpha-numerical, spaces etc. all allowed), finished off with a Closing Parenthesis character.
Symbols
If you are already a GREP wiz you might be able to enter the required GREP expression in the To Text field. However, I personally prefer constructing the expression bit-by-bit. The Special Characters For Search pop-up is a great help in constructing the expression. InDesign will automatically insert the correct expression data.
Our Open Parenthesis Character is part of the Symbols group.
As you select this component, you might notice that the text entered in the To Text field is now set to \(. Certain symbols are reserved by GREP for particular expression components. The Parenthesis form part of this group of characters. For our GREP Expression to recognise the “(” as a literal character it must be preceded by a “\” character. This “\” is referred to as an Escape Character.
Each of the following characters is reserved by GREP:
* ^ } ] ) $ . { [ ( | + ? ~
If you want to use these characters as a literal character in a GREP expression, you must escape them with the “\”.
Wildcards
Next we’re after our next part of the expression: The any text or words etc. that are inserted prior to the closing parenthesis.
When you are looking for ANYTHING… Think of the fact that you know that this ANYTHING might be an “a” or a “b” or really any character of the alphabet, and even numbers or spaces.
Wildcards are a way of defining an “ANYTHING”. And as we’re looking for any type of character, not just Digits (numbers), Letters (alphabetical) or White Space (spaces), we’re choosing Wildcards > Any Character.
A “.” is added to the GREP expression.
Repeat
You might think at this stage that you’re there… Not quite… GREP Expressions need to know home many times on ef these Any Characters can occur… So we need to know tell the Expression how many times this Any Character can repeat itself.
Let’s assume we don’t encounter “()” (without text between the parentheses). So we’re looking at One or More occurrences.
Shortest Match
So what would happen if there is more than one set of “()” within a paragraph? Well, this is where the Shortest Match option comes in handy. Shortest Match looks for the first occurrence of the “(” and the first occurrence of the “)”, the shortest possible string it can find in the paragraph, and that is exactly what we’re after 🙂
So let’s choose Repeat > One or More Times (Shortest Match). This adds “+?” to the expression.
We’re almost there at this stage. All we need to add to our expression now is the Closing Parenthesis character. We jump back to Symbols for that one.
Choose Symbols > Close Parenthesis Character
Our finished expression is \(.+?\)
Any text between brackets is now set using our GREP style. Click OK to apply the GREP style to the Paragraph Style and see the finished result.
Video Tutorial
Leave a Reply