The following tip is the fourth tip in a series of tips on GREP. It’s one of my personal favourites.
Part 1: Automatically apply a character style to all text between parentheses
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)
Probably because I’ve worked with so many people in my carreer who set those Ads you find in your letter box twice a week. The local supermarket product advertising material is what I’m talking about. Because of the amount of GREP styles involved in creating this Paragraph Style, I won’t add screenshots for each step, but instead provide you with step-by-steps on how to get to the end result.
In this fourth tutorial we’ll be formatting price tags containing dollars and cents with multiple character styles using GREP.
Paragraph Style
We will be formatting our Paragraph Style with 6 different character styles, in order to do this I’ve started out by creating a very simple Paragraph Style called “PriceTag”. It merely sets the Alignment of the style and nothing else.
Character Styles
Next with the Paragraph Style applied to two samples, format the samples as you would like them to look. Basically what you’re doing is prepping for the Character Styles. One tip is that I would advice you to change the Kerning setting to Optical so that the spacing for figure pairs is just that bit better looking.
Once you’ve got the format right. select the formatted text bit-by-bit and define Character Styles for all of the components.
Dollar Symbol is used to set the ‘$’ in $99.99
Dollars is used to set the first ’99’ (or other number) in $99.99
Decimal Point is used to make the full-stop . disappear in $99.99
Cents is used to set the second ’99’ in $99.99 in a smaller font and with baseline shift (or superscript if you prefer).
Cents Big is used only when defining a 99c type price and sets the ’99’.
Cents Symbol sets the ‘c’
Make the decimal disappear
To make the decimal disappear, I set it to 1pt font size and 1% horizontal scaling and change the fill colour to [none]. You might need to play around with this a little till you get things right.
Before we start with the GREP styles… Let’s reset our original text again and clear the character formatting changes we applied for the purpose of character style development.
Right-click the Paragraph Style, and choose Apply “PriceTag” Clear Overrides.
GREP Styles
Time to start creating our 6 different GREP styles that will make up this PriceTag style.
Right-click the Paragraph Style and choose Edit “PriceTag”… Then in the Paragraph Style Options, click the New GREP style button for each of the styles listed below and follow the rest of the prompts listed.
Setting Dollar Symbol
Change Apply Style to: Dollar Symbol
Clear the Expression in To Text. Cool so the literal text we’re look at here is the dollar sign itself. Unfortunately we can’t just enter a $, as that character is one of the reserved characters for GREP. We’ll “escape” it by placing a backslash in front of it ‘\‘.
To Text: \$
Setting Dollars
Now to those dollars. Change Apply Style to: Dollar Symbol
The Expression in To Text has already got the /d+ in it (Wildcards > Any Digit, Repeat > One or More Times), to allow for the dollars digits to be formatted.
Place the cursor after the /d+
It’s now time to insert a Positive Lookahead. The dollar ‘digits’ are placed ahead of the full-stop (decimal point) and that’s what we’re looking for when defining the GREP expression that sets these. We need to tell the expression that ‘if’ the digits are ahead of the full-stop, then the character style must be applied.
From Special Characters for Search choose: Match > Positive Lookahead. This adds (?=) to the expression. All we need to do know is, insert the full-stop inside this part of the expression. Again, the ‘.‘ actually has a reserved meaning in GREP, so we must escape it with the ‘\‘. Insert these characters before the closing parenthesis.
To Text: \d+(?=\.)
Setting Decimal Point
Change Apply Style to: Decimal Point.
Clear the Expression in To Text.We’re once again inserting literal text here ‘.’ and as in the previous GREP style we must escape the character and are thus inserting ‘\.‘
To Text: \.
Setting Cents
Wow, we’ve made it to the Cents. That means we’re half way there 🙂 Change Apply Style to: Cents.
As previously mentioned the expression in To Text has already got the /d+ in it (Wildcards > Any Digit, Repeat > One or More Times), to allow for the cents digits to be formatted.
Place the cursor in front the /d+
It’s now time to insert a Positive Lookbehind. The cents ‘digits’ are placed behind of the full-stop (decimal point) and that’s what we’re looking for when defining the GREP expression that sets these. We need to tell the expression that ‘if’ the digits are behind the full-stop, then the character style must be applied.
From Special Characters for Search choose: Match > Positive Lookbehind. This adds (?<=) to the expression. All we need to do know is, insert the full-stop inside this part of the expression, similar to how we did this for the Positive Lookahead (?<=\.)
To Text: (?<=\.)
Setting Cents Big
Ha, time for another Positive Lookahead the bigger cent digits ahead of the ‘c’ 😉
Character Style setting first… Change Apply Style to: Cents Big
The Expression in To Text has already got the /d+ in it. Place the cursor after the /d+
It’s now time to insert the second Positive Lookahead. The bigger cent ‘digits’ are placed ahead of the ‘c‘. We need to tell the expression that ‘if’ the digits are ahead of the ‘c‘, then the character style must be applied.
From Special Characters for Search choose: Match > Positive Lookahead. This adds (?=) to the expression. Then insert ‘c‘ characters before the closing parenthesis.
To Text: \d+(?=c)
Setting Cents Symbol
We’ve made it to the last GREP style, another Positive Lookbehind… remember the ‘c’ behind the bigger cent digits 🙂
Character Style setting first… Change Apply Style to: Cents Symbol.
The Expression in To Text has got the /d+ in it. This time we’ll clear that.
Instead we will insert the ‘c’ first and place our cursor in front of the ‘c’.
We’ll insert a Positive Lookbehind. The ‘c’ is placed behind the bigger cent ‘digits’. We need to tell the expression that ‘if’ the c is behind the digits then the character style must be applied.
From Special Characters for Search choose: Match > Positive Lookbehind. This adds (?<=) to the expression.
My instinct was to insert \d+ inside this expression, but that doesn’t work. Instead we’ll have to insert a Character Set that tells the expression the c can sit behind a 0,1,2,3,4,5,6,7,8 or 9.
Place the cursor in front of the closing parenthesis. Then choose from Special Characters for Search choose: Match > Character Set. This adds [] to the previous insertion.
We’ll now insert the characters, and if we enter 0-9 we’re basically defining all of digits.
To Text: (?<=[0-9])c
Wow… we’ve made it to the end 🙂 Click OK to update the Paragraph Style.
Ready to apply our new Paragraph Style with Grep Styles to our Price Tag text frames.
If you want to make things even more exciting, then enable the Paragraph Style option as part of a fancy Object Style… and all your formatting will be applied through the Object Style.
Video Tutorial
Cari,
Do you have a way to kern the amount of space in a price. In a lot of fonts the spacing between the number 1 and another number tends to leave a gap and between a 7 and another number it is too close. Can you use GREP to find these numbers and change the kerning?
Hoi Cari,
bedankt voor de extra info ;-).
Ik volgde je al langer via ‘MacMickey’ en de IndesignUsersgroep. Soms beantwoord ik daar ook wat vragen, maar ik kom er jammer genoeg niet aan toe om ze daarna ook te volgen (dus ik probeer me niet teveel te mengen tussen de actieve leden)… Ik heb inmiddels al een aantal grep-stylen gebouwd en het maakt een hoop werk een stuk eenvoudiger.
Bedankt!
@Niels,
Ha leuk. groetjes terug. Kijk ook even naar al wat oudere ‘Grip op GREP’ notes die ik voor IDUG Melbourne maakte op http://www.indesignusergroup.com/chapters/melbourne/files/1502/SpeakerNotes_GREP.pdf daar haal je wellicht ook nog wat tips uit. Het belangrijkste met GREP is niet te ver vooruit denken en je ‘patroontjes’ in delen op te bouwen. Je kunt ze behoorlijk complex maken trouwens… Kijk hier maar even: http://daringfireball.net/2010/07/improved_regex_for_matching_urls een voorbeeldje om URLs te vangen… van allerlei soorten 😉
Hoi Cari,
eventjes je 4 tutorials bestuderen en de toepassing van grep wordt duidelijk. Bedankt voor de duidelijke uitleg! Benieuwd of het nu in de praktijk ook zo logisch is…
Niels
(groeten van een oud klasgenoot van jou, Henk)
Hi Cari
I am very new to Grep styles and want to know is it possible to make a line of text disappear using GREP. I am working with large document and have set up a paragraph style for my running head text variable. I would like to make my paragraph style be invisible in the main copy but still be there to be picked up for the running head. Is this at all possible?
@Kerry that is definitely possible, but to pick up the content for the text variable, you must ensure the text physically exists within the main copy. So you’d need to define this paragraph style as something that is like 1pt font and 1pt leading and make the stroke and fill colour none. So although the text is physically there… it won’t be visible 🙂
Hello Cari,
this is an SOS from the Netherlands. Love your site en above all your tuts about GREP. I’ve got a problem which I can’t solve. What I want is this: Any filename with any extension as in newsletter.psd and adverb.tif has to be a different style.
I found out this \.*[.].*$ but it works only for the dot+extension. I know you can! But please will you help me? Greetings from Holland. Leo