Auto Style the Last (or First) Paragraph of an InDesign Story
Want the last paragraph of a story to appear different? This trick can do it in a jiffy!
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:

And here’s the result:

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.
This article was last modified on December 21, 2021
This article was first published on September 25, 2011
