Why doesn't \par end this \centering?

As per this answer, \par should properly end the effect of \centering. But in the following case, it doesn't. Why?

\documentclass{article}

\makeatletter
\newcommand\@mytitle{} % create macro for title
\newcommand\@myauthor{} % create macro for author
\newcommand{\articletitle}{%
\centering%
\fontsize{18bp}{18bp}\selectfont%
\@mytitle\par%
\vspace{\baselineskip}%
\fontsize{14bp}{14bp}\selectfont%
\@myauthor\par%
\fontsize{12bp}{12bp}\selectfont%
\vspace{2\baselineskip}\par}%
\makeatother

\mytitle{This is my title}
\myauthor{This is the author}

\AtBeginDocument{\articletitle}

\begin{document}
I start writing here
\end{document}


• A good example where none but one of the % at line endings serves no purpose whatsoever! ;-) (Except for those introducing a comment, of course.) Aug 30 '15 at 19:25
• @egreg I think you mean none but one serves any purpose i.e. only one serves a purpose. What you've said means that they all serve a purpose excepting one, which serves no purpose.
– cfr
Aug 30 '15 at 21:05
• @egreg But, if you put it into informal Welsh, it will mean what you think ;).
– cfr
Aug 30 '15 at 21:06
• @egreg I just developed the habit of putting % at the end of every line just in case, as I didn't really have a good understanding of when it was needed or not. I understand more now, but not enough that I dare ridding myself of this habit :) Aug 30 '15 at 21:11
• @Sverre Look at my talk at the TUG meeting, paying particular attention to the “missing required space syndrome”. ;-) river-valley.zeeba.tv/conferences/tug-2015 Aug 30 '15 at 21:12

\par does not end the effect of \centering, the end of the paragraph is just the place, where TeX uses the values set by \centering. A group can be used to limit the effect of \centering. After the group, the settings before the group are in force again.

\begingroup
\centering
...
\par
\endgroup


or {\centering ...\par}.

• Ok, but since my macro \articletitle has {\centering ... \par ... \par ... \par}, why is the whole document centered? Aug 30 '15 at 14:47
• Nevermind - I understand now. Aug 30 '15 at 14:49
• @Sverre The curly braces are not a group here, but they fence the definition text. Use \begingroup and \endgroup, the clearest version, or additional curly braces, if the number of key strokes matter more. Aug 30 '15 at 14:49

...to further elaborate on Heiko's answer:

A macro provides a means to match some parameter text - this includes the actual macro and it's arguments or "delimiters" - gobbles these up, and replace them with the macro's replacement text - everything inside the outer {..}. This is informally referred to as expansion, where the input stream finds some macro, and expands or replaces it with something else.

In the case of your example, \articletitle has the following generic form:

\newcommand{\articletitle}{\centering <stuff>}


With the idea of replacement, every \articletitle is purely replaced by \centering <stuff> (everything inside the outer {...}). Note that while it seems \centering is contained within some scope inside the macro definition, the replacement text is void of such scope. If, however, you defined

\newcommand{\articletitle}{{\centering <stuff>}}


the replacement text would be {\centering <stuff>} where the limited scope (or group) follows the macro replacement text.

So, in order to limit the scope of a switch or declaration (like \centering) inside a macro, ensure that you include the necessary grouping {...} (or similar).