[Tex/LaTex] Insert text before end of page, and at the beginning of the next page

environmentsmacrospage-breaking

I'm putting a template together and I have created a custom command which I'm going to use to set specific margins on the text passed to its argument and a few more formatting things, like this:

\newcommand{\mytextbox}[1] {#1}

I want it so text passed to this command splits across new pages nicely but before the new page begins, I want to print a new separate line saying "Continued on next page" and on the next page, print a line above the remaining text saying "Continued from previous page".

So for example if I do this near the bottom of a page:

\mytextbox{\lipsum}

It will print a few lines before flowing to the next page, but have my custom titles at the bottom and top of the next page, like this:

Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
Ut purus elit, vestibulum ut, placerat ac, adipiscing vitae,
felis.
**Continued on next page**

-----new-page------------------------------- 

**Continued from previous page**
Curabitur dictum gravida mauris. Nam arcu libero,
nonummy eget, consectetuer id, vulputate a, magna. Donec
vehicula augue eu neque. Pellentesque habitant morbi tristique
senectus et netus et malesuada fames ac turpis egestas. Mauris
ut leo.

I've been banging my head against the wall trying to figure out a way to do this. Nothing seems to work for me. My best guess is finding something in TeX that controls when it wraps the lines, checking if its the last line of the page and inserting a \newpage command in to the text but I haven't found a way to do this and it seems a bit convoluted. I don't care whether mytextbox is a command or environment as long as it behaves as above.

Best Answer

Here is a solution, assuming you generally want the plain page style with only the page number at the bottom. The only thing that I couldn't fix is the space between the "continued" lines and the actual text. Suggestions on that are welcome! I added the italics, you can just remove that if you don't like it. This code will add the "continued" texts no matter if the text you pass to \mytextbox actually breaks onto the next page or not.

Once you start messing with the page layout, further adaptions might be necessary.

\documentclass{article}% This only works for one-side documents! For two-side documents, more meddling with oddfoot and evenfoot is necessary.

\usepackage{lipsum}% filler text
\usepackage{afterpage}% for commands to be executed after the page
\usepackage{fancyhdr}% for direct access to headers and footers
\usepackage{array}% improved tables


\makeatletter
% Define new page style "contbottom"
\newcommand{\ps@contbottom}{%
    \renewcommand{\@oddhead}{}% empty header
    \renewcommand{\@oddfoot}{%
        \begin{tabular}{@{}>{\raggedright}p{.4\linewidth}@{}>{\centering}p{.2\linewidth}@{}>{\raggedleft}p{.4\linewidth}@{}}% table to get spacing correct
    \textit{**Continued on next page**} &% = footer left
    ~\\\thepage &% = footer center, put empty line above page number
    % = footer right
    \end{tabular}}%
}

% Define new page style "conttop"
\newcommand{\ps@conttop}{%
    \renewcommand{\@oddhead}{\textit{**Continued from previous page**}\hfill}% header
    \renewcommand{\@oddfoot}{\begin{tabular}{@{}>{\raggedright}p{.4\linewidth}@{}>{\centering}p{.2\linewidth}@{}>{\raggedleft}p{.4\linewidth}@{}}% table to get spacing correct
    &% = footer left
    ~\\\thepage &% = footer center, empty line as well so all pages look the same
    % = footer right
    \end{tabular}}%
}
\makeatother

% Put page number one line lower for normal pages
\pagestyle{plain}
    \fancyfoot[C]{~\\\thepage}

% Define command \mytextbox
\newcommand{\mytextbox}[1]{%
    \thispagestyle{contbottom}% page style contbottom for this page
    \afterpage{\thispagestyle{conttop}}% page style conttop for the next page
    #1% print text
}

\begin{document}

\lipsum[1-6]

\mytextbox{\lipsum}

\lipsum[1-6]

\end{document}

pages 1 and 2 pages 3 and 4

Related Question