7

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:

Bad example

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:

Good example

David M.
  • 197
  • 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 02:45
  • @cfr Unfortunately that has no effect – David M. Sep 28 '17 at 02:49
  • 2
    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 03:04
  • @barbarabeeton Unfortunately that did not work either! – David M. Sep 28 '17 at 03:07

2 Answers2

5

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}
Alan Munn
  • 218,180
  • 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 03:12
  • @DavidM. I've elaborated a bit (more or less what barbara said.) Is that clearer now? – Alan Munn Sep 28 '17 at 03: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 03: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 03:16
  • @barbarabeeton Is there a significant difference between this approach and CarLaTeX's approach? – David M. Sep 28 '17 at 03:19
  • there are many different ways to do some things with (la)tex. often the choice between them comes down to which one you happen to think of first, or are more familiar with. i learned tex before latex existed, so i tend to think of more "primitive" methods first. – barbara beeton Sep 28 '17 at 03:21
  • @barbarabeeton Ahhh sounds about right. Thank you! – David M. Sep 28 '17 at 03:22
  • 2
    @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 03:22
  • Eyeballed, my answer is only a workaround, yours is the correct one! – CarLaTeX Sep 28 '17 at 03:30
  • @AlanMunn: This is maybe a silly question, but wouldn't the inserted \par sometimes cause a page break before the closing rule? If yes, how to avoid it? Would a simple \nopagebreak just before the closing rule work in all cases? – Michel Fioc Sep 28 '17 at 08:12
3

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}

enter image description here

CarLaTeX
  • 62,716
  • 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 03: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 03: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 03:17
  • (poking my nose in again.) center adds space above and below. `\centering' does not. – barbara beeton Sep 28 '17 at 03:18
  • @CarLaTeX I am sorry I cannot accept both answers! – David M. Sep 28 '17 at 03:24
  • @barbara I don't think the issue is due to {center} vs \centering. – Alan Munn Sep 28 '17 at 03:25
  • @DavidM. See my edit, don't worry, they correct answer is Alan's one, mine is only a workaround! – CarLaTeX Sep 28 '17 at 03:29
  • @AlanMunn -- i don't think so either, since the additional space would be symmetrical. but the (implied) question was, what is the difference between the two. there are many situations (usually in floating environments) where the additional space from center is distinctly unwanted because it causes the final object to be so large that it results in unwanted page breaks or "relocations". – barbara beeton Sep 28 '17 at 03:30