5

In the following MWE I have a centred piece of unbreakable text. Problem is that this causes an extra bit of vertical space, so that visually the centred text is not vertically centred between the two paragraphs. What is the best way to deal with this?

\documentclass{article}

\usepackage{lipsum}

\begin{document}

\lipsum[2]

\begin{center}
\texttt{a~sentence~that~is~long~and~continues~with~the~reason~so~that~it~sticks~out~a~bit}
\end{center}

\lipsum[3]

\end{document}
lockstep
  • 250,273
Geoff
  • 2,637

2 Answers2

4

Don't use center (which is internally a list):

\documentclass{article}

\usepackage{lipsum}

\begin{document}

\lipsum[2]

{\par\bigskip\centering
\texttt{a~sentence~that~is~long~and~continues~with~the~reason~so~that~it~sticks~out~a~bit}
\par\bigskip}

\lipsum[3]

\end{document}
Ulrike Fischer
  • 327,261
  • You say: Don't use center (which is internally a list) do you mean in general? or just in such cases as above? Your solution looks less obvious than simply using a center environment, so it seems to me that something is strange with the center environment. – Geoff Jan 30 '13 at 16:46
  • 1
    Well I would say in the majority of cases center is not the best choice (and similar flushleft and flushright). The idea to use a list structure to change the justification of a paragraph doesn't seems right. Generally I find it more natural to use commands like \raggedright or \centering. – Ulrike Fischer Jan 30 '13 at 16:47
  • If I simply did \renewenvironment{center}{\par\bigskip\centering}{\par\bigskip} are there any catastrophic consequences? I mean, I have always used center as if it acted like this and am somewhat surprised that something I thought was obvious is in fact not the case. – Geoff Feb 01 '13 at 19:18
4

You're wondering why there's more vertical space before than after the unbreakable text, right?

output

Ulrike told you a good way to deal with it, I'd like to explain a bit why this happens. The problem is indeed the somewhat unfortunate definition of the center environment. This starts with \trivlist\centering\item\relax, so it uses the \centering command inside a trivial list. Now \item places an (empty) label as the first object to be centered, and your unbreakable text doesn't fit on the line, so TeX decides to put it on the next line. You can visualize this by redefining \trivlist so that the label isn't empty anymore:

visualization

Note that the LABEL isn't centered since I defined it so that it sticks out to the left.

\documentclass{article}
\makeatletter
\def\trivlist{%
  \parsep\parskip
  \@nmbrlistfalse
  \@trivlist
  \labelwidth\z@
  \leftmargin\z@
  \itemindent\z@
  %\let\@itemlabel\@empty
  \def\@itemlabel{\llap{LABEL}}
  \def\makelabel##1{##1}}
\makeatother
\begin{document}
\noindent left \hfill right
\begin{center}
\texttt{a~sentence~that~is~not~so~long}
\end{center}
\noindent left \hfill right
\begin{center}
\texttt{a~sentence~that~is~long~and~continues~with~the~reason~so~that~it~sticks~out~a~bit}
\end{center}
\noindent left \hfill right
\end{document}

I think you're safe if you redefine the center environment to use Ulrike's suggestion. Note the \begingroup and \endgroup!

\renewenvironment{center}{\begingroup\par\bigskip\centering}{\par\bigskip\endgroup}
Hendrik Vogt
  • 37,935
  • Thank you for the response, I was indeed wondering about this lop-sided vertical space. Given that you say unfortunate definition of the center environment makes me wonder if there is ever any use for it. – Geoff Feb 01 '13 at 19:07
  • @Geoff: I said somewhat unfortunate :-) As long as you only have short enough lines, the environment is quite satisfactory, at least in my opinion. – Hendrik Vogt Feb 01 '13 at 22:31
  • I agree, but sometimes long unbreakables appear; which makes me wonder whether I should simply do \renewenvironment{center}{\par\bigskip\centering}{\par\bigskip} and then I don't need to worry about it. Unless there is something that can happen that I don't know about. – Geoff Feb 02 '13 at 08:56
  • @Geoff: See my edit. – Hendrik Vogt Feb 02 '13 at 10:03