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}
4
  • 1
    possible duplicate of When should we use \begin{center} instead of \centering? Jan 30, 2013 at 14:11
  • @Martin I'm also interested to know why we get the extra vertical space just because of some horizontal text that's too long. (I just realised that I didn't add that in the original question)
    – Geoff
    Jan 30, 2013 at 16:47
  • Because Lists (of which center is one) add space before and after them. Jan 30, 2013 at 17:08
  • @Martin: The question here is not the extra space around the center environment (which of course also occurs without long unbreakable text). The question is why there's additional space only before the environment! (So no, it's not a duplicate of that other question.) Jan 31, 2013 at 21:53

2 Answers 2

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}
3
  • 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, 2013 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. Jan 30, 2013 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 1, 2013 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}
4
  • 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 1, 2013 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. Feb 1, 2013 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 2, 2013 at 8:56
  • @Geoff: See my edit. Feb 2, 2013 at 10:03

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.