September 24 2007 • 3:34 PM

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.

17 Responses discussing this post. Add yours below.

  1. September 24th, 2007 • 3:52 pm • Link

    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. David Blatner
    September 24th, 2007 • 3:57 pm • Link

    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. Eugene
    September 24th, 2007 • 5:52 pm • Link

    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. September 24th, 2007 • 6:48 pm • Link

    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. David Blatner
    September 24th, 2007 • 7:26 pm • Link

    I totally agree with both points, Sandee! Adobe? You listenin’?

  6. Eugene
    September 24th, 2007 • 7:30 pm • Link

    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.

  7. September 24th, 2007 • 7:37 pm • Link

    It is indeed Dave’s script. It can be downloaded from here:

    http://jsid.blogspot.com/2006/06/smart-title-case-revisited.html

  8. David Blatner
    September 24th, 2007 • 7:56 pm • Link

    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.)

  9. September 24th, 2007 • 8:45 pm • Link

    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

  10. Peter Kahrel
    September 25th, 2007 • 7:31 am • Link

    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

  11. Eugene Tyson
    September 25th, 2007 • 8:40 am • Link

    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?

  12. Eugene Tyson
    September 25th, 2007 • 8:42 am • Link

    Edit to last line: Which is great, because who wants one to do things one way in InDesign anyway?

  13. Peter Kahrel
    September 25th, 2007 • 11:13 am • Link

    >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

  14. Eugene Tyson
    September 25th, 2007 • 11:22 am • Link

    Yeh I know, I did really well up to the last sentence.

  15. Ken Darby
    February 26th, 2009 • 5:23 am • Link

    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)

  16. jenn stockwell
    January 15th, 2010 • 11:46 am • Link

    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!!

  17. Jongware
    January 15th, 2010 • 2:25 pm • Link

    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.

Subscribe to the Discussion

Get the ongoing discussion surrounding "GREP Solution to Flawed Title Case Feature" delivered to you. Click here to subscribe via RSS.

Leave a Reply

You can use limited HTML tags, such as <em></em> for emphasis/italics and <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> .

InDesignSecrets reserves the right to edit and/or remove posts and comments.