6

I'm trying to define a command that adds a horizontal line after text blocks, but I'm struggling to find a way to omit the line at pagebreak – the text blocks should be separated by either the horizontal line or a page break, never both.

Here's a MWE showing my approach:

\documentclass[paper=a5]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage{blindtext}

\newcommand{\mycommand}[1]{%
  #1%
  \vspace{.1ex}\begin{center}\rule{.25\textwidth}{0.4pt}\end{center}\vspace{.1ex}%
}

\begin{document}

\mycommand{\blindtext[2]}
\mycommand{some text some text some text some text some text some text some text}

\end{document}

Output

output

There's a horizontal line at the top of the second page that I want to be omitted. How can I do that?

I read Reacting to a page break in LaTeX but I didn't find a way to adapt its solution to my problem.

  • At the top, I agree, but why shouldn't you have a rule at the bottom? You need to mark where the block ends, don't you? – Bernard Dec 24 '17 at 11:07
  • Do you mean consecutive text blocks have to be on the same page? – Bernard Dec 24 '17 at 11:24
  • @DavidCarlisle Just tried that, but I didn't get \leader to work with \rule. – dessert Dec 24 '17 at 11:50
  • @dessert \leaders\hbox{\rule.....} – David Carlisle Dec 24 '17 at 12:06
1

The old trick of leaders should do.

\documentclass[paper=a5]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage{blindtext}

\newcommand{\mybreak}{%
  \par
  \nointerlineskip
  \cleaders\vbox to 5ex{%
    \vss
    \hbox to \textwidth{\hss\vrule width 0.25\textwidth height 0.2pt depth 0.2pt\hss}
    \vss
  }\vskip5ex
}

\begin{document}

\blindtext[2]

\mybreak

some text some text some text some text some text some text some text

\mybreak

some text some text some text some text some text some text some text

\end{document}

enter image description here

3

From an old solution of mine that can be used here too (https://tex.stackexchange.com/a/396216/120578) (I used your command combined with mine):

\documentclass[paper=a5]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage{blindtext}

\newsavebox{\mybottombox} % Box to save the text of the command 
\newlength{\mybottomlength} % The length of our text inside the command
\newlength{\availafter} % The available length left on the page after placing our text

% Optional argument is the minimum length after the nobottom text for not pagebreak. Change it to your needs
\newcommand{\nobottom}[2][40pt]{\savebox{\mybottombox}{\vbox{#2}}\setlength{\mybottomlength}{\ht\mybottombox}%
\setlength{\availafter}{\dimexpr\textheight-\mybottomlength-\pagetotal\relax}\ifdim\availafter<#1%
\pagebreak%\noindent\usebox{\mybottombox}%
\else%
\noindent\usebox{\mybottombox}%
\fi%
}%

\newcommand{\mycommand}[1]{%
  #1\vspace{.1ex}\par%
  \nobottom{\noindent\begin{center}\rule{.25\textwidth}{0.4pt}\end{center}\vspace{.1ex}}%
}

\begin{document}

\mycommand{\blindtext[2]}
\mycommand{some text some text some text some text some text some text some text}
\mycommand{some text some text some text some text some text some text some text}
\end{document}

Output:

enter image description here

I just not adding the line if it is at the bottom of the page. The bottom, is defined in the optional argument of my command \nobottom and for the example I used 40pt.

We can also give my optional argument as optional to your command too if needed for specific paragraphs.

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.