[Tex/LaTex] prevent “action” if top of page

page-breakingrules

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}

Best Answer

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.