is now part of CreativePro.com!

GREP Solution to Flawed Title Case Feature

34

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 is the co-founder of the Creative Publishing Network, InDesign Magazine, CreativePro Magazine, and the author or co-author of 15 books, including Real World InDesign. His InDesign videos at LinkedIn Learning (Lynda.com) are among the most watched InDesign training in the world.
You can find more about David at 63p.com

Follow on LinkedIn here
  • Anne-Marie says:

    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.

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

  • Eugene says:

    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.

  • vectorbabe says:

    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.

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

  • Eugene says:

    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.

  • Bob Levine says:

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

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

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

  • 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

  • Peter Kahrel says:

    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

  • Eugene Tyson says:

    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?

  • Eugene Tyson says:

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

  • Peter Kahrel says:

    >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

  • Eugene Tyson says:

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

  • Ken Darby says:

    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)

  • jenn stockwell says:

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

  • Jongware says:

    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.

  • kenm says:

    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?

  • kenm says:

    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.

  • […] plug-in from InTools (does Change Case magic) > Change Case with GREP, InDesignSecrets post here and forum thread here > Using Configurator in InDesign to change case > Is it Upper Case or […]

  • Chris says:

    :\s\l|[-\s]\l(?!(f|he|nd?|o|y|rom|or)?\b)

    Hi Can someone please explain what this GREP code is actually doing?

  • ayman sayed says:

    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

  • Chris Haynes says:

    GREP style : CAPS

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

    Change first letter of any word larger than 3 letters

    (It is not perfect)

  • Andy McGinnis says:

    Hi,

    Does anyone know how I can make this work for selected text in multiple table cells? When I run the query currently, the first letter of each word in each cell isn’t capitalized, but the rest of the words DO have their first letter capitalized (Hope that makes sense). Ideas?

    Thanks in advance!

    • Chris Voros-Haynes says:

      Hi check to see if you have any other grep or nested or character styles that might be overriding the first word … are you using my suggested grep style? … \<\w(?=\w{3,})

  • I do this using GREP in paragraph styles:
    First caps letter
    \<[\l\u]

    No caps letter
    (?i)the |or |by |of |on |at |with |in |and |see |de |to |'s|-\l+-|(?i)page
    (you can add or remove other conditions that won't be necessary to capitalise)

    • Chris Haynes says:

      Hi Ricardo

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

      This I what I use as it applies capitals to the first letter to a word that is 4 (1+3) characters in length or longer as (generally) the words you don't want to be capitalized are less than 4 characters in length … Of course, there are some exceptions which you can then do a local override for

      • Jean-Lou Désiré says:

        HI How can include the first letter of the first word. Thanks

  • Chris Voros-Haynes says:

    Hi Jean

    If you only want to change the first letter of the first word of a paragraph then use the following:

    ^\<\w

    and apply a "CAPS" character Style

    ^ (Beginning of Paragraph)
    \< (Beginning of Word)
    \w (Any Word Character)

  • >