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}
1
  • Just remove the last \rulesep command: there's no way to remove glue if there is no following page.
    – egreg
    Commented Jun 29, 2012 at 13:33

2 Answers 2

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.

12
  • 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. Commented Jun 29, 2012 at 7:42
  • @AbstractDissonance I've changed the implementation. Modify at will the parameters. However a 1pt thick rule is really too thick.
    – egreg
    Commented Jun 29, 2012 at 8: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. Commented Jun 29, 2012 at 9: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. Commented Jun 29, 2012 at 9:58
  • 1
    @DenisBitouzé The OP apparently added the code after I answered.
    – egreg
    Commented Oct 28, 2022 at 19:57
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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .