Thanks for coming to InDesignSecrets.com, the world's #1 resource for all things InDesign!

Automatically Applying Character Styles to Prices

Recently I was asked for the most efficient way to apply character styles to a bunch of prices in a document.The key to applying a character style to all of the prices is to look for a particular pattern. Depending on what version of InDesign that you are using there are a few different methods.

1. Regular find/change
This is the oldest method that I would have used in CS2 or earlier.
Find $^9 – (any digit)
Change – apply Price Character style.

This would find all of the $X prices. Next I would add another any digit ($^9^9) and find all of the $XX prices. Eventually you would get them all. You might have to run it again with $^9^9.^9^9 to change $XX.XX. As you can see this is a bit of a pain. Luckily there are other ways.

2. Nested Styles
Another option is to use nested styles. Edit your paragraph style and put in a new nested style that applies none up to 1 “$”
Then apply your character style through 1 word.
Then repeat the last two styles.


Every price in that particular paragraph style will have the style applied. The downside is that if there is a comma after a price ($XXX,) the comma will become formatted since the comma is considered part of the word.

3. Grep Find/Change
If you are still running InDesign CS3 you can run the following find/change expression to apply the character style. Which will find all numbers, commas and periods after a dollar sign.

\$[.,\d]+

Here is the breakdown of the grep code.

\$ – dollar sign

[.,\d]+ - this will find periods, commas and all digits in the word

If you are going to use this method be aware that if you make any changes you will want to run the operation again to make sure you didn’t miss anything.

4. Grep Styles
If you have CS4, grep styles are your best bet. You can use the same expression that was used in the find/change, but now it will be automatically applied to your text as it is input.

\$[.,\d]+

The only problem with this particular expression is that it will apply the character style to the entire word including any commas that may follow the price. If this is not desired here is another expression which will not apply the character style to any commas after the price.

\$\d+|[.,]\d+

This will apply the character style to the prices, but not a comma after them.  This expression will work well over prices in value of one million dollars.

$ dilemma

There is a chance that either of these methods will not work for you because of the \$ character. IF that is the case, Mr. Blatner found a workaround where you can use \x{0024} instead of \$.

Here is what \x{0024} means:

\x – insert this Unicode character”

{0024} – 0024 is the code for the $. You can look up the Unicode symbol by selecting a glyph in the glyph panel and holding your cursor still for a moment.

Regardless of which method that you use it is always a good idea to test your expressions thoroughly before you blindly trust them to work in your document.

Tags
Related Articles
Comments

17 Comments on “Automatically Applying Character Styles to Prices

  1. This expression is a logical next step for the first GREP style

    /$[/.,/d]*/d

    as GREP is greedy by default. The first half, [/.,/d]*, will pick up as many periods, comma’s, and digits as possible. Then a single digit should follow.

    The asterisk means ‘zero or more’, which means this will format ‘$0′ up to ‘$1,000,000,000.00′ (and higher).

    Untested, but I suppose you can auto-format old style superior cents “$100.00″ using this combo:

    /$[,/d]*/d/.(?=/d/d)

    – i.e., any regular formatted amount where a period and 2 digits follow –, and

    (?=/d/.)/d/d(!?/d)

    to superior, picking up cents after the comma.

    [Edit] Doh! Backslashes are eaten; just revert the forward slashes …

    [Edit #2] Why would the dollar sign cause problems? AFAIK, it just needs a backslash because it’s a GREP special character.

  2. For a test case of:

    This sentence contains many price variants, including $45, $45.99, $1, $1.00, $1,199, $1,199.99, $999,999,999 and $999,9999,999.99 and $999999999.99.

    I can find every price on the list with this expression:

    \$(\d{1,3},?)*(\d{1,3})(\.\d{2})?

    This essentially says there will always be a dollar sign followed by zero or more groups of 1-3 decimals which may or may not have commas after them, and there will always be a group of 1-3 decimals at the heart of it and there may be a period and two decimals after it.

    (PS: I think TextWrangler is an excellent way to “wrangle” these things out, because it color codes the GREP expression as you enter it.)

  3. Great, ignore that post. I just tried it in InDesign and while it works perfectly in TextWrangler, ID rejects it.

    Back to the drawing board… :-/

  4. LOLwut? OK, it’s because of the weird \$ bug. Thanks to David for figuring that one out!!! Would have taken me many more moons. So this expression DOES work in ID too:

    \x{0024}(\d{1,3},?)*(\d{1,3})(\.\d{2})?

  5. Here in Holland prices are formatted ? X.XXX,XX (points and commas are switched) so for the catalogs I make, I set up nested styles so “style A” (or “none”) is applied up to ?, “style B” applied through the comma and “style C” is applied to the two characters after the comma. This lets me set a separate size and baseline shift for the digits, giving a look that’s easier to read and highly accepted over here.
    These aren’t prices that’re found in-line though, but that’s the nature of catalogs :)

  6. Hallo everyone…

    GREP Styles look great and exactly what I was looking for…

    But here’s the challenge: I have to format a paragraph where I have 2 prices (full and discount)…

    The two prices should be on the sme line, so here’s what I have:

    Original Price ?XX.XXX,XX – Discount Price ?YY.YYY,YY

    Now, I need that:

    ? [Original Price] is formatted with italic white text
    ? [?XX.XXX,] is formatted with italic white and underlined text
    ? [XX] is italic white underlined apex text
    ? [-] is white plain text
    [Discount Price ?YY.YYY,] is formatted with bold black text
    ? [YY] is bold black apex text

    Tthe prices can vary from 1.000,00 to 100.000,00

    Any idea of which code I should use?

    Thanks in advance guys!

  7. GREP styles sure could do that, but … so can nested styles!

    GREP styles are ideal when the text it should mark may occur anywhere inside a paragraph. However, since this is a simple list of style A for x words, style B for the next y words etc., nested styles are far more economical, and easier to set up as well.

    Go to this style’s style definition, Nested Styles section, switch on Preview, and add the following definitions (creating the appropriate character styles when necessary):
    italic through 2 Words
    italic + underline through 1 ,
    apex through 1 Words
    white text through 1 Words
    bold through 1 ,
    apex bold through 1 Words

    The single comma in the 2nd and 5th entries are what you actually have to enter, rather than selecting something from the drop down menu.

  8. Hi Jongware and thanks for your reply…

    Your suggestion is good and I also used it. The trouble is that the words “Original Pricee” and “Discount Price” can change occasionally. The data are fetched from an Excel file and sometimes the “Original Price” can be “Prezzo di listino” (it’s an Italian catalogue) or “Prezzo originale” or simply “Listino” and the “Discount Price” can be “Super Offerta” or “Prezzo Super Scontato”, etc… As you can see the words can vary in number so I cannot apply the rule on tha basis you suggest.

    The standard line is something like that:
    [Sentence A] [?XX.XXX,XX] – [Sentence B] [?YY.YYY,YY]
    where Sentence A and B can have variable words and prices can be from thousands to hundreds of thousands… The only fixed character is the [-] between the two prices… And remember that the 2 digits after the comma should inherit the style of the previous digits but also be superscript (or apex)…

    Hope that everything is clear :-)

    Thanks again for your nice support!

  9. Ah, okay. This is the same, using GREP styles. Take care not to end the lines with one or more spaces; the final digits test relies on an end-of-line.

    ?italic white? to ?^[\\w ]+?; ?italic white+und? to ??[\\d.]+,(?=\\d\\d -)?; ?italic white apex? to ?(?<=,)\\d\\d(?= -)?; ?plain white? to ? – ?; ?bold black? to ?(?<=- ).+,?; ?bold apex? to ?(?<=,)\\d\\d$?

    (I hope all backslashes come through first time…)

  10. Blast! Foiled again … You only need one single backslash before each of the ‘\w’ and ‘\d’ codes.

  11. Just another little question…

    What if I decide to have only the Discount Price?
    If I change from

    [Sentence A] [?XX.XXX,XX] ? [Sentence B] [?YY.YYY,YY]

    to

    [Sentence B] [?YY.YYY,YY]

    the [Sentence B] is formatted as [Sentence A] (while the price maintans the right formatting) and I do not want that…
    If you want, I can send you a .indd file as example…

    Really really thank you in advance!

  12. Well, you need another set of GREP styles … I’d recommend making another paragraph style for those.

    I think it might just be possible to try and write a single set of GREPs that differentiate for a dash somewhere in the text, but that’s really stretching ID’s GREP caps. GREP styles get applied “as you go”, and making them overly complicated would certainly slow down your entire system at some point.

    For this simpler case, I would use a nested style; you can apply the text style “up to ?”, so it doesn’t matter how many words are before it.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>