NOTE: Special thanks to Jacob Farmer from Identity Management who helped extensively with the regular expression portion of this post.
If you missed the Adobe Infoshares with Adobe Senior Solutions Engineer Tim Plumer on Tuesday and Thursday, you missed a lot of great information. Even someone like me, who thinks they know everything learned a thing or two about a thing or two.
One of the things that jumped out at me was GREP Styles in InDesign. If you’re not familiar with grep(traditionally spelled in all lower case on UNIX-based systems, but listed in capital letters in InDesign), it’s a pattern matching tool used mainly in UNIX, but InDesign has added it’s functionality to the Paragraph styles panel. grep allows us to search for a piece of text within a file, like so:
grep Adobe indesign.txt
The line above would search for the word “Adobe” inside the text file called “indesign.txt”. Things get much more complicated when we involve regular expressions. Regular expressions allow us to search for more specific information by including various symbols within the text. For example:
Would search for the word Adobe, but only at the beginning of a line(indicated by the ‘^’ symbol). This is an incredibly simple example, and regular expressions are almost a programming language unto themselves. For example:
This regular expression will match duplicate words separated by any amount of whitespace. Often times, regular expressions are described as “Write once, read never” because they are so difficult to decipher once they are complete.
So why would an InDesign user care?
With GREP styles in InDesign, we can not only search for a specific pattern, but we can style that pattern automatically. Imagine if you wanted a name in bold every time it appears in your text, or a phone number to be italicized. GREP Styles make it easy.
Let’s look at how to do this now.
- To begin, use the text tool to create a text box and type: InDesign CS4
Now, let’s create a GREP style.
- On the keyboard hit F11 to bring up the Paragraph Styles panel, and click the New button at the bottom of the panel to create a new Paragraph Style. Name this style “GREP Test“.
The New Paragraph Style window appears.
- On the left side of the screen, click GREP Style.
- To create a new GREP Style, click the New GREP Style button.
Now we get into the good stuff. We need a Character Style to apply, so let’s do that quickly. This character style is the style that’s going to be applied to any of the text that our pattern matches.
- Next to Apply Style, click [None], and then click the drop down arrow on the right side. Click New Character Style, and when the new dialog box appears, move to Basic Character Formats, and let’s set the font size to 48pt, so that we can see the changes.
OK, so now we have a style that will be applied to any text that matches our pattern, so what is our pattern? By default, InDesign places this pattern in the “To Text:” field:
This cryptic command will match any digit (\d), and the plus symbol instructs it to match at least one digit, but multiple digits as well. So this would match any number of digits in a row, but it will not match if there are no digits.
Programmers spend lots of time memorizing all these rules and symbols, but InDesign makes it easier for us by including a dropdown list of all the things we can add, and placing them for us. However, we still need to know what to search for. Let’s start with a simple word.
- In the To Text: field, type: InDesign CS4.
Now we have a style that will apply a font size of 48pt to any text reading InDesign CS4 in our document. Let’s test it out.
- Click the OK button.
The paragraph reappears. Let’s apply the style.
- To apply the style, with the type tool, click inside the paragraph, and in the Paragraph styles panel, click “GREP Test“.
If you had the text “InDesign CS4″ in your paragraph, it should now appear in 48pt. If the word in your paragraph was capitalized differently, it will not match. This is because grep is case sensitive by default in InDesign.
But what if we wanted to match other versions of InDesign, like CS, or CS2, or even versions previous to CS, when versions were indicated by numbers? Let’s modify our regular expression now.
- In the Paragraph Styles panel, double click “GREP Test“. In the resulting dialog box, on the left side, click GREP Style.
- Click the text reading InDesign CS4.
We need to modify this so that we create a pattern that matches InDesign CS and InDesign CS2, but not InDesign CS12345. We’d also like it to match InDesign by itself. Let’s do this now. For every step, I’ll be typing in what your code should look like at that moment.
- First off, lets remove the number 4, since that will be replaced by a pattern.
- Now we need to match any single digit number in that field, let’s add \d to match any digit.
We need to match exactly one digit or no digit, since CS would be valid. We have a dropdown list of many of the options we might use, so let’s see if one will work for our needs.
- Click the @ symbol on the right side of our text, and click Repeat, and then click Zero or One Time.
A question mark appears at the end of our line:
Now we know that the ? means that it will match if there is no digit, but also if there is only 1 digit.
We also need it to match if the CS is not there, but a number is, since before the Creative Suite moniker, InDesign versions were indicated by numbers. We need a way to tell InDesign that we want it to match either the code we have there now, or a single digit. Let’s see if there’s something in the dropdown list.
- Click the @ symbol, click Match, click Or.
A vertical bar (|) called the pipe symbol appears. This indicates that if anything in that position matches something on either side of the pipe symbol, it will be styled.
- Add \d to match a digit in this place.
This code is looking for a pattern that matches the word InDesign, followed by a space, and followed either by the letters CS and a single digit, or a digit by itself.
We now need to place the second part of our code in parentheses, so the pipe symbol is forced to only look at those two cases.
- Place the code CS\d?|\d in parentheses.
One problem we might have is that we have hard coded a space into this regular expression. That means that grep will look for the word InDesign, followed by a space, followed by either CS and a digit, or a digit by itself. This would not match the word InDesign with a period behind it. What we can do to fix this is to tell InDesign to look for a whitespace character after the word InDesign, and use the ? symbol to tell it to match zero or one time.
- Remove the space after InDesign, and click the @ symbol, click Wildcard, click Any Whitespace.
At this point, this code will only match the word InDesign if it’s followed by whitespace. Once thing we can do to make the code more efficient, is to wrap everything after InDesign in parentheses, and place a question mark at the end. This way, the code will match either the word InDesign, or InDesign with our matching pattern after it. Let’s do this.
- Place the code ‘\s(CS\d?|\d)’ in parentheses, and place a ? at the end.
We see our final code:
- Click OK, and test out the code in your paragraph.
Every time InDesign appears, with or without a single digit, or the phrase CS with a single digit after it appears, it should be styled in a different font size.
Now we have our code, the phrase “Write Once, Read Never” should make some more sense. Hopefully, now that you’ve got your feet wet, you can do some personal learning on regular expressions, and incorporate them into your work.
With this powerful tool at your disposal, styling tasks in InDesign could potentially become much easier. The best thing is that grep is such a well known and widely used command, that there are many resources for it out there, and InDesign’s implementation uses all the common commands and syntax. So dig in and start working.