9

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\mytitle[1]{\renewcommand\@mytitle{#1}}
    \newcommand\@myauthor{} % create macro for author
    \newcommand\myauthor[1]{\renewcommand\@myauthor{#1}}
    \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}

enter image description here

  • A good example where none but one of the % at line endings serves no purpose whatsoever! ;-) (Except for those introducing a comment, of course.) – egreg 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 :) – Sverre 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 – egreg Aug 30 '15 at 21:12
14

\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}.

| improve this answer | |
  • Ok, but since my macro \articletitle has {\centering ... \par ... \par ... \par}, why is the whole document centered? – Sverre Aug 30 '15 at 14:47
  • Nevermind - I understand now. – Sverre Aug 30 '15 at 14:49
  • 7
    @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. – Heiko Oberdiek Aug 30 '15 at 14:49
3

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

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.