# Unexpected spacing in \newenvironment

Disclaimer: I am asking this question because (1) I would like to solve this problem, but (2) also because I would like to learn about what's going on under the hood with LaTeX here. Thus, I am not seeking out a package that can do this, or comments on how or why I'm doing this. I simply want an explanation of the behavior in this specific instance.

I am playing with creating a new environment with the \newenvironment command. Here's what I have:

\documentclass[12pt]{article}

\newenvironment{mytitle}{
\begin{center}
\begin{minipage}{0.5\linewidth}
\begin{center}
\rule{\linewidth}{0.4pt}
\LARGE\bfseries
}{
\rule{\linewidth}{0.4pt}
\end{center}
\end{minipage}
\end{center}
}

\begin{document}
\begin{mytitle}
Lorem Ipsum Dipsum Gypsum
\end{mytitle}
\end{document}


The result:

Question: Where is that space coming from between the end of the text and the rule? I have tried putting percent signs all over the place, changing the \parskip value, doing different kinds of line breaks, etc. What is causing this? The natural followup question is how to get rid of it, so that it looks more like this:

• The { followed by a new line enters a space. New lines are spaces. Use % to comment the line ending. It doesn't happen before because the line ending is preceded by a control sequence, which gobbles the space. – cfr Sep 28 '17 at 2:45
• @cfr Unfortunately that has no effect – David M. Sep 28 '17 at 2:49
• try inserting \par just after the brace that opens the "end of environment" block. (not tested) i think you're still in horizontal mode, and since the rule is the full textwidth, it will be set on a new line, at the baseline. (don't have a tex installation on my laptop, so this is just a somewhat educated guess.) – barbara beeton Sep 28 '17 at 3:04
• @barbarabeeton Unfortunately that did not work either! – David M. Sep 28 '17 at 3:07

The issue is correctly described by barbara in her comment but the solution needs more than just a \par: when the rule is drawn in your code it's at the baseline of the \LARGE paragraph. So you need to end that paragraph (which is what the \par does) and then go back to the normal baseline (which is what \normalsize does.)

When TeX sets a fontsize, it sets two sizes: the fontsize itself, and the leading (baseline height). So your \LARGE command sets the fontsize to 18pt and the leading to 22pt. So the total height of a line is 22pt. This height will remain in place until you change it or the group ends. So in your code, the rule is drawn on a line that is 22pt, which is where the extra vertical space comes from.

The command \normalsize in a 10pt document sets the fontsize to 10pt on a 12pt baseline. This can only be done after the \par is issued to end the previous paragraph.

You don't really need the inner \begin{center} ... \end{center} environment; this could be done by adding \centering instead of the \begin{center} and removing the inner \end{center}.

\documentclass[12pt]{article}

\newenvironment{mytitle}{
\begin{center}
\begin{minipage}[t]{0.5\linewidth}
\begin{center}
\rule{\linewidth}{0.4pt}
\LARGE\bfseries
}{  \par\normalsize
\rule{\linewidth}{0.4pt}
\end{center}
\end{minipage}
\end{center}
}

\begin{document}
\begin{mytitle}
Lorem Ipsum Dipsum Gypsum
\end{mytitle}
\end{document}

• Brilliant! This did it. Can you please help with part (2) of the question? What do \par and normalsize do here? Why are each needed? – David M. Sep 28 '17 at 3:12
• @DavidM. I've elaborated a bit (more or less what barbara said.) Is that clearer now? – Alan Munn Sep 28 '17 at 3:12
• What do you mean by "go back to the normal baseline"? What did the rule do to the baseline? – David M. Sep 28 '17 at 3:14
• the rule didn't do anything to the baseline -- the \normalsize caused the baseline to decrease. another approach is to shift the rule upwards, which is what @carlatex's approach does. – barbara beeton Sep 28 '17 at 3:16
• @DavidM. More explanation. :) There's not too much difference between my approach except for where @CarLaTeX got her .4\baselineskip value from (is is principled or eyeballed?) – Alan Munn Sep 28 '17 at 3:22

The correct answer is Alan Munn's one, however, as a workaround, you could raise a bit your rule adding [.4\baselineskip], the \rule macro syntax is:

\rule[raise]{width}{thickness}


As for \centering instead of center environment, Barbara Beeton has already answered in her comment, however, see here: When should we use \begin{center} instead of \centering?.

\documentclass[12pt]{article}
\newenvironment{mytitle}
{%
\begin{center}
\begin{minipage}{0.5\linewidth}
\rule{\linewidth}{0.4pt}
\LARGE\bfseries\centering
}{%
\rule[.4\baselineskip]{\linewidth}{0.4pt}
\end{minipage}
\end{center}
}
\begin{document}
\begin{mytitle}
Lorem Ipsum Dipsum Gypsum
\end{mytitle}
\end{document}


• I copied and pasted your code into my editor (modulo the linewidt thing), and unfortunately it had no effect... – David M. Sep 28 '17 at 3:08
• @DavidM. Sorry, a cut and past error, try now (I'm working on my smartphone at the moment and it's a bit difficult) – CarLaTeX Sep 28 '17 at 3:13
• I totally understand! Your newest edit works. Can you please explain what the [.4\baselineskip] does? Also, why did you change from the center environment to \centering? – David M. Sep 28 '17 at 3:17
• (poking my nose in again.) center adds space above and below. `\centering' does not. – barbara beeton Sep 28 '17 at 3:18
• @CarLaTeX I am sorry I cannot accept both answers! – David M. Sep 28 '17 at 3:24