3

My naive approach to center text in a minipage using \centering is clearly not working:

enter image description here

\documentclass{standalone}
\begin{document}
\begin{minipage}{3cm}
\centering
    ~\newline
    X\newline
    XXX\newline
    XXXXX\newline
    $XXXXXXX$\newline
    X
\end{minipage}
\end{document}

What am I doing wrong?

2
  • 4
    or replace newline with \\ .
    – Zarko
    Aug 4 at 6:52
  • 1
    BTW, you can use display math (e.g. \begin{equation}...) in a minipage and they will be centered without \centering. Aug 4 at 15:26
8

In "normal" text \newline and \\ are basically the same. However, \centering changes that:

\def\centering{%
  \let\\\@centercr
  \rightskip\@flushglue\leftskip\@flushglue
  \parindent\z@\parfillskip\z@skip}

so within a minipage with \centering, \newline still has its original definition, while \\ is now \@centercr. After a couple of expansions (which I omit) in the end you'll get that

  • \newline is essentially \unskip\relax{\relax\relax}\nobreak\hfil\break,
  • \\ is essentially \unskip\par\addvspace{-\parskip}\ignorespaces.

This means that \newline still has the "normal text definition" with \hfil, which is supposed to fill the rest of the line. Perfectly reasonable in justified text, but wrong in centred text, because it adds additional glue on the right.

Long story short: use \\.

\documentclass{article}

\begin{document}

\fbox{\begin{minipage}{3cm}
\centering
    X\newline
    XXX\newline
    XXXXX\newline
    $XXXXXXX$\newline
    X
\end{minipage}}
\fbox{\begin{minipage}{3cm}
\centering
    X\\
    XXX\\
    XXXXX\\
    $XXXXXXX$\\
    X
\end{minipage}}

\end{document}

enter image description here

3
  • Thanks for this very instructive answer. Wouldn't it make sense for minipage then to redefine \newline as well?
    – bers
    Aug 4 at 8:09
  • 1
    @bers Uh, you should ask someone from the team about that, but I rather think not. (BTW this is about \centering, not minipage.) It's the same in tabulars and similar: \\ is redefined to do something specific to the current environment, but one should have a "backup" of the standard behaviour.
    – campa
    Aug 4 at 8:29
  • Of course, \centering :) Agreed - having both \` and \newline` (doing different things) can be very helpful in a tabular. Thanks!
    – bers
    Aug 4 at 9:33

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.