6

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?

1
  • 2
    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, 2015 at 13:33

2 Answers 2

5

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}

centred line

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

7
  • 1
    You don't need % after \end{tightcenter} (neither after \begin{tightcenter}) if you insert \ignorespacesafterend in the end part of the environment. Mar 7, 2015 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. Mar 7, 2015 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? Mar 7, 2015 at 14:05
  • @BryanElrod -- this should answer your question about percent sighs: What is the use of percent signs (%) at the end of lines? Mar 7, 2015 at 14:48
  • @BryanElrod \centering is not the same as \centerline....
    – cfr
    Mar 7, 2015 at 15:46
1

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}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .