# Insert text before end of page, and at the begining of the next page

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.

-
An suggestion is given in the post: tex.stackexchange.com/questions/24106/… –  Marco Daniel Oct 16 '11 at 10:32
Welcome to TeX.sx! A tip: You can use backticks  to mark your inline code as I did in my edit. –  doncherry Oct 16 '11 at 10:35
Are you using headers and footers by fancyhdr? If not, I could attempt to write up a solution using that package. –  doncherry Oct 16 '11 at 10:36
@doncherry I'm not using fancyhdr but I'll consider anything as long as the titles are directly below and above the text as in the example, and only occur for mytextbox items. –  Sophie Welles Oct 16 '11 at 10:41

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{array}% improved tables

\makeatletter
% Define new page style "contbottom"
\newcommand{\ps@contbottom}{%
\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{\@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}
`

-
Thank you, this is looking promising. –  Sophie Welles Oct 16 '11 at 12:58