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

GREP Solution to Flawed Title Case Feature

Many people don’t realize that InDesign has a Change Case feature in the Type menu (or the Context menu). For example, you can select some text that someone TYPED IN ALL CAPS and change it to lowercase, Title Case (all words are capitalized), or Sentence case (only the first letter in each sentence is capitalized). Unfortunately, while Title Case is great for headlines, it misses the mark because it doesn’t follow a basic rule: that most conjunctions, articles, and prepositions (such as and, but, the, for, a, an, or, and nor) shouldn’t be capitalized.

Fortunately, GREP and Scripting guy Peter Kahrel wrote to us recently with a fun solution he had found to this Title Case problem based on CS3’s GREP searching feature:

  1. Select the text you want to change to Title Case.
  2. Open the Find/Change dialog box (Command/Ctrl-F) and switch to the GREP tab.
  3. Set the Search pop-up menu to “Selection” (so only the selected text is modified).
  4. Copy this (including the initial colon) into the Find what field
    :\s\l|[-\s]\l(?!(f|he|nd?|o|y|rom|or)?\b)
  5. Leave the Change what field blank, but click in the Change Format section and turn on the All Caps styling.
  6. Click Change All to make the change.

In Peter’s words:

The words to ignore are in the list separated by vertical bars, without their first letter: (f|he|nd?|o|y|rom|or) tells the GREP to skip of, and, in, on, to, from, and for (nd? matches an, and, in, on; the single letter o matches to). It may overgeneralise a bit in that ‘if’ and ‘she’ are kept in lower case, which might not be correct, but preventing that is complicated and makes the expression even more unreadable than it already is; and the errors it makes are so few that some manual editing doesn’t bother me. The GREP does two more things: it converts to upper case any word that follows a colon or a hyphen.

He then points out that you can add more words to the exclusion list by typing them — without their first letter, and separated by a vertical bar — in the list. For example, I noticed that “but” was being capitalized, so I added that by changing the grep expression to: :\s\l|[-\s]\l(?!(f|he|nd?|o|y|rom|or|ut)?\b)

Perfect solution? Far from it. In my newer example, the words “gut” and “shut” won’t get capitalized. But I still think it’s better than Adobe’s built-in solution. Thank you, Peter!

Remember that you can save your Find/Change queries, so after you set this up once, click the Save button (it looks like a floppy disk icon… how quaint!) and give it a name.

Note that you can find more GREP solutions in the Search feature on this page (upper-right corner); we’re also developing a list of GREP solutions here.

David Blatner
David Blatner is the co-founder of the Creative Publishing Network, InDesign Magazine, and the author or co-author of 15 books, including Real World InDesign. His InDesign videos at Lynda.com are among the most watched InDesign training in the world. You can find more about David at 63p.com.
David Blatner

Latest posts by David Blatner (see all)

Related Articles
Comments

28 Comments on “GREP Solution to Flawed Title Case Feature

  1. I love this, but I’m a little unclear on a couple things.

    – Is it a replacement for the Change Case > Title Case command? Or are you supposed to run it on titles you’ve already changed, to uncapitalize the listed words.

    – I assume you’re supposed to select the text first and make sure the Find/Change is run on the Selection, yes? Or is it smart enough to recognize which bits of a story are titles and which are not. I’d be really surprised if it could do that … heh … but you/Peter didn’t mention setting the scope of the F/C so I feel compelled to ask.

  2. Hey, it’s Monday morning; I can’t be expected to write entirely coherently! ;)

    I’ve gone ahead and changed the step-by-step above to answer the questions: Yes, use this instead of Title Case; and Yes, you should select the text you want to change first.

  3. Often when I’m running my GREPs I want certain things to remain untouched, like the body text or a the second and third level headings. So I only run the search on that particular Paragraph Style, rather than selecting the text manually.

  4. But the true moral of the story is that we need two improvements to the Change Case command.

    #1: A list of exclusions for the Title Case command as well as a list of exclusions for the Make Lowercase command.

    #2: A way to apply the command as part of a Find/Change routine.

  5. I got a script from someone, perhaps Mr. Dave Saunders, called, Smart Title Cases, I’ve been using it for a while now, I can’t find and replace with it or set it as part of the Style, but I can use a shortcut for it. Still better than selecting individual words and etc.

  6. Thanks, Bob, and thanks very much, Dave! Yes, I should have remembered that script. It’s very good, and much easier than the GREP solution. (Though the GREP is more fun, as it makes your eyes boggle when you look at it.)

  7. One piece of advice I would give people using that script is: if you use and external text file to hold the words, change the internal array definition to say that’s what you’re doing so you don’t waste time editing the array inside the script only for it to be ignored because of the text file you forgot about.

    Does it sound as though I’m speaking from experience?

    Dave

  8. A script is obviously more versatile and powerful for things like title case. The point of this example was to show that you can get very far with GREP expressions.

    Peter

  9. Hi Peter, my intention was not to divert attention from the GREP solution. I didn’t mention that I like the GREP solution and I think it’s great. You could use this solution for a lot of things. Like if you wanted certain words to be a cap first letter perhaps in the sentence. So you could search multiple words or variants of these words to make UpperCase and so on.

    I still for the life of me can’t figure out after 3 versions of CS that there is no option in the Paragraph or Character styles to have Title Case and so on, or have an editable table or something that can adjusted accordingly for Title Cases, leaving out conjunctions or in as needed. It’s just something that has annoyed me, and I read somewhere that Adobe had corrected this in CS3 but the feature just wasn’t there.

    Nor was it included in the few free GREPs that were included by default.

    I just don’t want you think that I haven’t taken note of this code and saved it in my GREPs for future use or reference, because I have.

    I only thought to mention the script because I feel I’m not alone in not having Smart Title Cases. Now am I not only not alone but I’ve also got 2 ways of achieving the same thing. Which is great, because who wants one to do things in InDesign anyway?

  10. >who wants one to do things in InDesign anyway?

    Nice Freudian slip :)

    Thanks for the nice words. You’re right: it shouldn’t be so difficult to do something with an editable list, similar to Autocorrect. Let’s hope it’ll make it in the next version. There have been many requests for it.

    Peter

  11. At the risk of sounding like a GREP newbie, which I am,
    what purpose does the initial colon serve in this expression? I can neither find it on Mike Witherell’s cheat sheet nor at http://www.regular-expressions.info.

    Snip
    —–
    Copy this (including the initial colon) into the Find what field
    :sl|[-s]l(?!(f|he|nd?|o|y|rom|or)?b)

  12. Hi David,

    I realize this is slightly off topic, but I am trying to do what seems like a relatively simple find/change using GREP and can’t quite get it to work.

    I need to search for everything up to and including a colon at the beginning of a paragraph and set the character style sheet to ‘none.’

    I can go into detail if you need, but it’s sort of complicated, the reasoning behind it…

    Thanks for any help!!

  13. Ken (better late than ever!),

    The GREP does two more things: it converts to upper case any word that follows a colon or a hyphen.
    The colon stuff is in the first half, before the very first pipe character.

    Jenn, that GREP should be

    ^[^:]+:

    I can go into great detail on this GREP expression when asked … ;-)

    However, there are more than a few problems. You can apply “[None]”, either with search & replace, or using a nested grep style. But: applying the None char style doesn’t do anything. It removes present character styles, but the original formatting is preserved.
    You can create a new character style in which you set every possible attribute to Normal (not underlined, normal case, no superscript, etc. for everything). That does not work in a GREP style for manually applied formatting, you need a search-and-replace for that.

  14. This looks great. I need to have a style sheet that forces the text into Title Case. How do I get the correct GREP command string to put into the paragarph style sheet?

  15. Very nice. I just made this part of my SubHead Style Sheet.

    I just created a Character Style “All Caps”
    Clicked the “Basic Charcter Format” from the list and selected “All Caps” from the dropdown selection for Case:

    Then I edited my SubHead Style Sheet in the GREP Style section. Click New GREP Style, paste the command string from above :\s\l|[-\s]\l(?!(f|he|nd?|o|y|rom|or)?\b) and select “All Caps” for the Apply Style selection.

    Hope this proves helpful for other InDesigners.

  16. Pingback: InDesignSecrets Podcast 156 | InDesignSecrets

  17. how can i convert al numbers
    before
    4846303
    393251561
    44010
    14560656
    12702185
    44763

    that what i need
    4,846,303
    393,251,561
    44,010
    14,560,656
    12,702,185
    44,763
    how can i do it please

  18. GREP style : CAPS

    \<\w(?=\w{3,}\s)

    Change first letter of any word larger than 3 letters

    (It is not perfect)

Leave a Reply

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