28

I have a manual list of stuff that are sometimes separated by an hrule(or my own custom ruler) for visual purposes.

Sometimes the hrule ends at the top or bottom of a page which makes it useless.

Is there a way to prevent it from being added if it is at the top or bottom of a page?


MWE of problem:

\documentclass{book}
\usepackage{tikz}
\begin{document}
\newcommand\rulesep{%
  \par % make sure we end a paragraph
  \vskip5pt % space above the rule
  \leaders\vrule width \textwidth\vskip0.4pt % rule thickness is 0.4pt
  \nointerlineskip % disable interline glue here
  \vskip5pt % space below the rule
}
\def\dotikzcircle#1{\begin{tikzpicture}\draw(0,0)circle(#1);\end{tikzpicture}}
\rulesep
\noindent This is a test\noindent
\rulesep
\noindent End of test
\rulesep
\dotikzcircle3
\rulesep
\begin{center}\dotikzcircle3\end{center}
\rulesep
\begin{center}\dotikzcircle3\end{center}
\rulesep
\dotikzcircle1
\rulesep
\dotikzcircle2
\rulesep
\dotikzcircle3
\rulesep
\dotikzcircle4
\rulesep
\dotikzcircle5
\rulesep 
\end{document}
Werner
  • 603,163
  • Just remove the last \rulesep command: there's no way to remove glue if there is no following page. – egreg Jun 29 '12 at 13:33

2 Answers2

22

Make your rule into a discardable item:

\newcommand\disappearingrule{%
  \par % make sure we end a paragraph
  \vskip5pt % space above the rule
  \leaders\vrule width \textwidth\vskip0.4pt % rule thickness is 0.4pt
  \nointerlineskip % disable interline glue here
  \vskip5pt % space below the rule
}

Because of the page breaking rules of TeX, no break can be taken at the vertical leaders or at the following \medskip. If a page break is taken at the \bigskip, the following glue (or leaders) will disappear.

egreg
  • 1,121,712
  • Thanks. Can you explain how to adjust the thickness and what \leaders does? Also, I want the same space to be on both sides. You seem to hard code the lower half space. My current rule is \newcommand\rulesep{\par\kern 5pt\hrule height 1pt\kern 5pt\par} which has 5pt's on both sides. Yours is not usable by me unless both top and bottom space is the same. – AbstractDissonance Jun 29 '12 at 07:42
  • @AbstractDissonance I've changed the implementation. Modify at will the parameters. However a 1pt thick rule is really too thick. – egreg Jun 29 '12 at 08:43
  • Yours is still not symmetric for me. I've added an MWE. My original hrule is symmetric expect between centered and non-centered environments as centering seems to add some extra vertical space for some reason. I center all my graphics so this is not an issue since all get the same extra space. With your rule, while it does solve the original problem seems to still have more space above it than below which I can hard code to get an approximately symmetric rule but would like something like my original code. – AbstractDissonance Jun 29 '12 at 09:56
  • note that I replace the vkips with kern and it seems to work in my original code except I end up with the rules at the top and bottom. (this suggests it may be a more difficult problem) i.e., vskip gives the correct behavior but not symmetric and kern gives the wrong behavior but symmetric. – AbstractDissonance Jun 29 '12 at 09:58
  • @AbstractDissonance The problem is that book uses flushbottom, so the "asymmetry" you see is due to the \parskip that's enlarged to fill the page. Use \raggedbottom in that context. – egreg Jun 29 '12 at 10:05
  • @AbstractDissonance Moreover, the center environment provides a good break point, so that the mechanism for "swallowing" the rule won't work with it, and it adds vertical space on its own. In this case I would say \centerline{...}, which should do what you want. – egreg Jun 29 '12 at 10:23
  • @egreg How I can force \disappearingrule to be "glued" to the given "block"? In some case, the \disappearingrule that is supposed to be above the given "block" is printed at the end of the page and the "block" is on the next page. So far, I'm dealing with this issue with \needspace{} but I was wondering if there is a better solution. Thanks. I can also open a new question. – Colas Jul 29 '21 at 10:13
  • @Colas The clues are too few, I'm afraid. A complete example is needed. – egreg Jul 29 '21 at 10:35
  • OK, I'll do that in a separate question. Thanks – Colas Jul 29 '21 at 11:22
  • I don't see any difference between your \disappearingrule and the \rulesep in the OP. Could you provide a MWE? – Denis Bitouzé Oct 28 '22 at 19:54
  • 1
    @DenisBitouzé The OP apparently added the code after I answered. – egreg Oct 28 '22 at 19:57
  • I don't see the benefit in his MWE (still rules at the bottom of the pages but, okay, not at the top). – Denis Bitouzé Oct 28 '22 at 20:08
3

Here is an extended demonstration of egreg’s solution.

\documentclass{book}
\usepackage{tikz}
\usepackage{pgfpages}
\pgfpagesuselayout{8 on 1}[a4paper,landscape,border shrink=2.5mm]
\newcount\absdiscnt
\newcommand\rulesep{%
  \par\vskip5pt
  \leaders\vrule width \textwidth\vskip0.4pt
  \nointerlineskip\vskip5pt
}
%\raggedbottom
\begin{document}
\absdiscnt0
\loop
  \advance\absdiscnt by 1\relax
  \ifnum\absdiscnt<121\relax
  \rulesep
  \centerline{Eh, this line is by Abstract Dissonance!}\endgraf
  \rulesep
  \edef\tempa{\the\dimexpr.25\baselineskip}
  \edef\tempb{\the\dimexpr\textwidth-2\tempa-\parindent}
  \begin{tikzpicture}
  \draw[fill=blue](0,0)circle(\tempa);
  \draw[fill=red](\tempb,0)circle(\tempa);
  \end{tikzpicture}
\repeat
\end{document}

enter image description here

Ahmed Musa
  • 11,742