is now part of CreativePro.com!

Auto Style the Last (or First) Paragraph of an InDesign Story

18

Some newsletters and magazines put the author’s byline at the end of the story, on it’s own line, in its own style. Wouldn’t it be spiffy if you could just have InDesign apply that formatting for you automatically? You can, with a slightly non-intuitive grep style.

The key to making this work is to consider how the last line of the story is different than all the other paragraphs. Remember, if you can identify a pattern, you can target it with a grep expression. In this case, the last line of the story should never end with a return character! The story must end at the conclusion of the paragraph. In that case, the pattern we can search for is “any paragraph that does not end with a return.” Here’s the grep style I as I wrote it:

^.+?$(?!\r)

Here it is in context, in the paragraph style:

grep style for last paragraph

And here’s the result:

styling the last line

Note that I would have been at a loss as to how to write this if it weren’t for Kris Coppieters at Rorohiko pointing out that “no return at the end” idea. Thanks, Kris!

However, literally as I type this, I’m thinking… duh, there’s a far easier way! This code literally means “the last paragraph” of the story:

^.+?\Z

I had forgotten about the “\Z” code, meaning “the end of the story.” You could select the first paragraph of the story with \A.+?$ (the dot-plus-question means “all the text”; the \A, \Z, ^ and $ are all for marking location: beginning of story, end of story, beginning of paragraph, and end of paragraph).

Now, someone is going to accuse me of cheating here because I’m applying a character style to the entire paragraph. I plead guilty! I would ordinarily avoid applying a character style to an entire paragraph, but there’s no other way of automating this, besides using a third-party plugin or a custom script. If cheating gets the job done efficiently and doesn’t cause too many other problems, then I’m all for it.

Also, someone else is going to argue that this will cause problems if there is other local formatting already applied to that text, such as a different style. Nope, try it yourself. Local formatting is always added after the grep style, so it will not be overridden by this grep style.

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
  • Glenn Kramer says:

    Great article! I need to change the last paragraph formatting for all the articles in my company’s newsletters. Thanks, David!

  • albastru22 says:

    Well, usually I want that last paragraph to be right aligned. With this trick only character level formating can be achieved, so for me it?s still manual formating.

  • Glen Saville says:

    David, that is truly awesome, love it :)

    @ albastru22 trick I use for that is to add a Shift Tab before the text I want right aligned, which forces it over :)

  • You can also use \z (lowercase) for “end of the story”.

  • Ed says:

    To style the first paragraph I use the paragraph-style NextStyle feature

    Title (with NextStyle > FirstParagraph)
    FirstParagraph (with NextStyle > RestOfStory)
    RestOfStory (with NextStyle > SameStyle)
    LastParagraph (with NextStyle > SameStyle)

    That last Paragraph has a ‘SpanAllCollumns’ (to auto adjust multicollumn layout). So I still need to style that last paragraph manualy.

    I’m impatiently waiting for a last-Paragraph ParagraphStyle in Indesign.

  • Cari Jansen says:

    And the character style cheat is totally acceptable in that case I reckon David ;-)

  • Jongware says:

    It might be worth mentioning that these codes \A or \Z to locate start or end of a story isn’t the Start and End of it all. If you accidentally applied the GREP style to text in a table, anchored object, or footnotes, you’d be surprised to find all of them have starts and ends of their own little story as well.

  • JulieB says:

    David, this works great and is going to save me a good chunk of time!!! How can I add space before that last paragraph either with spacing or with an extra return? I can’t do that with a Character Style.

  • @JulieB: If (and only if) the last paragraph is only a single line, you could do this by applying another grep style. This time, you’d search for .\\z (dot backslash z) and apply a character style that simply adds additional leading. The larger the leading, the larger the “space before.” (That character style is being added to the last line in the paragraph.)

    I don’t see any way to do this if you have more than one line. You’d need the grep to “find” the first character of the last paragraph, which is tricky because positive lookahead can’t be an arbitrary number of characters, I think.

  • JulieB says:

    Ok. I usually do have more than one line since it’s a newspaper column.

    I just discovered your podcasts, and they are totally changing the way I work. I’m now dutifully learning about GREP and expanding my Object Styles horizons. Thanks very much!!!

    And, for the record, I’ve registered for the forums so I won’t be tempted to post questions in the comments next time.

  • Erica Gamet says:

    @JulieB Not as automated, but you could do a Find/Change using David’s GREP string. Under the GREP tab in Find/Change, put that code into the Find What field and then in the Change Format field, choose your paragraph style with the extra space before and any other paragraph styling. Find/Change has so many more formatting options, but has to be run near the end of your process (something that can be overlooked with tight deadlines).

  • JulieB says:

    Thanks Erica. I had the same thought and have been experimenting with that technique – which is working!

    Very true that it’s easy to forget the final touches when you’re trying to get something out the door!

  • Marie Doherty says:

    I need to set paragraphs starting at top of pages full out (i.e. without a first-line indent). Just wondered if there was any way that this could be automated?

  • Janman says:

    Hi all,

    I have a question about the \A GREP (start of story).
    I’m creating a script which re-layouts an InDesign generated TOC, in which I wish to add returns before main sections, only not at the 1st line of the textFrame (the TOC).
    So, what I want to use is (after findGrep preselected character style to select the main sections):
    “[^\\A.+]”
    i.e. all entries wich are NOT at the beginning of the story
    changing it to “\\r\$0”

    However, it applies the return exactly where I don’t want it, namely at the beginning of the story. Is it actually possible to use \A in combination with the caret or did I simply put it incorrect in the findGrep?

  • Ricardo Francé says:

    Hi David! How can I apply a character style in the first line of a paragraph with a lot of lines. Thank you!

  • >
    Notice: We use cookies on our websites to give you a great online experience. If you keep browsing, we'll assume you're ok with this. For more information, see our privacy policy. By closing this banner, you agree to the use of cookies.I AGREENo