6

I've been playing around with LaTeX trying to really learn how spacing works in-depth. Here's a silly bit of code that I've been experimenting with:

\documentclass[]{article}

% Draw a raised \hrulefill
\newcommand{\hmidrule}[1]
    {\leavevmode \leaders
     \hbox{\rule[#1]{1pt}{.4pt}}
     \hfill \kern0pt }

% Draw a divider with a given ornament in the middle
\newcommand\divider[2][0.33em]
    {\par\noindent\hmidrule{#1}{#2}\hmidrule{#1}\par}

\begin{document}
\divider{\large\ A\ }
%%
{\hspace*{\fill}
    \huge {This is the First and Last Line.}
\hspace*{\fill}}  % <--- Keep this line in mind!
%%
\divider{\large\ $\Omega$\ }
\end{document}

This produces exactly the result I'd expect from this: minimal spacing on the top and bottom of the centered text.

The output of the first file

Then I put \par on this line:

% ...
    \huge {This is the First and Last Line.}
\hspace*{\fill}\par}  % <--- Added `\par` here.
%%
% ...

And I expected that no change would occur, since ending this paragraph would cause nothing; the paragraph was already ending there, but I also thought that it could feasibly add some vertical space below the centered text.

However, this was the result: vertical space before the centered text!

The unexpected output of the second file

I really don't understand why this is happening. Why would adding this command after the text cause spacing before it? Could it be that LaTeX trying to format this as paragraphed text? If so, why didn't it add space there before I added \par?

Any other feedback would be greatly appreciated.

I'm compiling my code on Overleaf, with pdfLaTeX. The same result occurs with XeTeX.

  • 3
    welcome to tex.sx. i'm sure this (or something like it) has been asked before, so this is just a comment. \huge is a command that takes no argument. it you place it within the scope of the braces -- {\huge This is the First and Last Line.} -- the \par won't have that effect. – barbara beeton Oct 10 '18 at 18:42
  • @barbarabeeton I actually intended to put \huge within the same braces as the \hfills, but I see that my formatting makes it look like I intended otherwise! – AJFarmar Oct 10 '18 at 20:03
7

The difference is that with \par the paragraph is set with the baselineskip relative to \huge, whereas it isn't without it.

If you want to control precisely the spacing between the decorated rules and the title, the best is to disable the interline spacing at the transition, with \nointerlineskip.

I provided a different definition of \divider that avoids the need to guess a dimension.

\documentclass[]{article}

% Draw a divider with a given ornament in the middle
\newlength{\dividerht}
\newcommand{\hmidrule}{%
  \leaders\hrule
    height \dimexpr0.5\dividerht+0.2pt 
    depth  \dimexpr-0.5\dividerht+0.2pt
  \hfill\kern0pt
}
\newcommand\divider[1]{%
  \par\noindent
  \settoheight{\dividerht}{#1}%
  \par\noindent\hmidrule{#1}\hmidrule\par
}

\begin{document}

\divider{\large A}\nointerlineskip\vspace{6pt}
{\centering\huge This is the First and Last Line.\par}
\nointerlineskip\vspace{6pt}
\divider{\large $\Omega$}

\bigskip

\divider{\large A}\nointerlineskip\vspace{6pt}
{\centering\huge This is the First line and this title is so long that 
  it splits across lines.\par}
\nointerlineskip\vspace{6pt}
\divider{\large $\Omega$}

\end{document}

enter image description here

If you want that the bottom rule is spaced with respect to the baseline of the bottom line in the title, you need to remove the depth:

\divider{\large A}\nointerlineskip\vspace{6pt}
{\centering\huge This is the First line and this title is so long that 
  it splits across lines.\par}
\vspace{-\prevdepth}\nointerlineskip\vspace{6pt}
\divider{\large $\Omega$}

enter image description here

  • That's a very clever way of defining the rule! I appreciate the explanation very much. – AJFarmar Oct 10 '18 at 20:08

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.