How to center text without adding space using modified center environmet

Firstly I'm very sorry because this question was in fact answered already. I only have a question to the following answer and I'm not allowed to comment there directly: https://tex.stackexchange.com/a/24197/73704

I liked this response and Leo's small example worked perfectly. Unfortunately, the document I use is quite big and has already a parskip defined (it should stay in there).

I adjusted his example to fit my scenario and the problem becomes visible: there is a vertical space after "foo".

\documentclass{article}

\parskip1.5ex plus0.5ex minus0.5ex

\newenvironment{tightcenter}{%
\setlength\topsep{0pt}
\setlength\parskip{0pt}
\begin{center}
}{%
\end{center}
}

\begin{document}
text text text text text text text text text text text text text text text text text text text text text text text text
\begin{tightcenter}
foo
\end{tightcenter}
text text text text text text text text text text text text text text text text text text text text text text text text

\end{document}


Does anybody know why this problem appears and how it can be solved?

• Welcome to TeX-SX! In fact asking a new question is exactly how it should be done, so no worries. We don't have follow-up commenting in the answers area usually so this is much better for a specific focus. – percusse Mar 7 '15 at 13:33

Maybe something like this though I'm not sure it is the best solution.

The center environment adds vertical space, whereas \centering does not. TeX reads a newline as a space so the newline after the end of the environment causes the next line to look partially indented as a space is added. Commenting the newline avoids this issue.

\documentclass{article}

\parskip1.5ex plus0.5ex minus0.5ex

\newenvironment{tightcenter}{%
\setlength\topsep{0pt}%
\setlength\parskip{0pt}%
\par\centering}{\par\noindent\ignorespacesafterend}

\begin{document}
text text text text text text text text text text text text text text text text text text text text text text text text
\begin{tightcenter}
foo
\end{tightcenter}
text text text text text text text text text text text text text text text text text text text text text text text text
\end{document}


EDIT Avoid need for % signs thanks to karlkoeller's suggestion.

• You don't need % after \end{tightcenter} (neither after \begin{tightcenter}) if you insert \ignorespacesafterend in the end part of the environment. – karlkoeller Mar 7 '15 at 13:44
• Thank you very much. Works like a charm. You either of you explain me what % does after a line? I saw it at other latex examples but thought it is simply a way to allow comments after the command. – Bryan Elrod Mar 7 '15 at 13:55
• I just now looked closely at your solution. The answer where I got the original example from said one should not use centering because it is not suitable for long texts. I'll use your solution now but is there one using center? – Bryan Elrod Mar 7 '15 at 14:05
• @BryanElrod -- this should answer your question about percent sighs: What is the use of percent signs (%) at the end of lines? – barbara beeton Mar 7 '15 at 14:48
• @BryanElrod \centering is not the same as \centerline.... – cfr Mar 7 '15 at 15:46

This is a nice example why \parskip should not be touched. ;-)

Let's abuse display math mode.

\documentclass{article}

\newlength{\normalparskip}
\AtBeginDocument{%
\setlength{\normalparskip}{1.5ex plus0.5ex minus0.5ex}%
\setlength{\parskip}{\normalparskip}%
}

\newenvironment{tightcenter}{%
\setlength\abovedisplayskip{0pt}
\setlength\abovedisplayshortskip{0pt}
\setlength\belowdisplayskip{0pt}
\setlength\belowdisplayshortskip{0pt}
$$\begin{minipage}[t]{\textwidth}\centering }{% \unskip\strut\par \end{minipage}$$
}

\begin{document}

text text text text text text text text text text text text
text text text text text text text text text text text text
text text text text text text text text text text text text
text text text text text text text text text text text text
text text text text text text text text text text text text
text text text text text text
\begin{tightcenter}
foo\\
foo
\end{tightcenter}
text text text text text text text text text text text text
text text text text text text text text text text text text
text text text text text text text text text text text text
text text text text text text text text text text text text
text text text text text text text text text text text text
text text text text text text text text text text text text

text text text text text text text text text text text text
text text text text text text text text text text text text
text text text text text text text text text text text text
text text text text text text text text text text text text
text text text text text text text text text text text text
text text text text text text text text text text text text
\end{document}