# Frame for a box depending on the position in the page

I would like to write a command \begin{myFramedBox} ... \end{myFramedBox} such that:

• If the block inside \begin{myFramedBox} ... \end{myFramedBox} is at the beginning of the page, then it is printed normally.
• Else: it is printed with a rule above it.

These commands would allow me to have blocks that are separated by rules unless there is a pagebreak.

How to do that? (I'm open to solution with tcolorbox)

• \pagetotal should be 0pt at the top of a page, but it is only changed between paragraphs. Dec 23, 2021 at 15:28

A dirty hack using the code from here: https://tex.stackexchange.com/a/41302/101651.

Let me know if it works in your case.

\documentclass{book}
\usepackage{mwe}% <--- For testing purpose only
\usepackage{etoolbox}
\usepackage{xcolor}
\usepackage[mathlines]{lineno}
%Code from: https://tex.stackexchange.com/a/41302/101651
\renewcommand\linenumberfont{\color{white}}
\makeatletter
\makeatother
\newenvironment{myFramedBox}
{\ifnumequal{\thelinenumber}{0}{}{\vspace{10pt}\hrule\vspace{10pt}}}{\par}
\begin{document}\linenumbers
\blindtext
\begin{myFramedBox}
This has a rule above
\end{myFramedBox}
\blindtext\newpage
\begin{myFramedBox}
This has no rule above
\end{myFramedBox}
\blindtext
\begin{myFramedBox}
This has a rule above
\end{myFramedBox}
\newpage
\begin{myFramedBox}
This has no rule above
\end{myFramedBox}
\blindtext
\end{document}


• Thanks! I will try in the coming days and let you know. Dec 24, 2021 at 10:52
• @Colas You're welcome! I hope it works. Dec 24, 2021 at 11:08
• Hi CarLaTeX. Unfortunately, it does not work. Thanks again anyway! Dec 25, 2021 at 12:41
• @Colas What error/anomaly does it gives you? Dec 25, 2021 at 12:55
• It's difficult to know because I've copied/pasted your solution in my document. Basically, the majority of the text disappears! Dec 25, 2021 at 15:21