# Change page layout after first page

currently I'm trying to prepare a letter head with XeLaTeX. The first page of the letter should have a sidebar on the right. For this sidebar, the text width should be shrinked to approx. 80% of its normal size, so that text won't go into the sidebar. For the following pages, the text width should be normal, i.e. without any additional margins.

I realized that changing the page layout in LaTeX mid-document isn't a trivial task and there exist some solutions, for example, the \newgeometry command within the geometry package.

One of my first ideas was to use the atbegshi package in conjunction with the geometry package in order to achieve the desired layout change after the first page:

\documentclass{article}
\usepackage{atbegshi}
\usepackage{geometry}
\usepackage{lipsum} % just to have dummy texts

% define the main page layout

% restore the main page layout for all following pages
\AtBeginShipoutNext{\restoregeometry}

\begin{document}
% define a new page layout for the first page

\lipsum[1-30]
\clearpage
\lipsum[31-50]

\end{document}


Unfortunately, this won't work. It seems that every atbegshi hook (\AtBeginShipout, \AtBeginShipoutFirst, \AtBeginShipoutNext) ignores \newgeometry and \restoregeometry commands.

I tried several other methods, but this one seems the most sophisticated one, since it would give a very clean way for changing the page layout just for the first page (for example, consider the following MWE; not working for some reason):

\documentclass{article}
\usepackage{atbegshi}
\usepackage{geometry}
\usepackage{lipsum} % just to have dummy texts

% define the main page layout

% define the first page layout

% restore the main page layout for all following pages
\AtBeginShipoutNext{\restoregeometry}

\begin{document}

\lipsum[1-30]
\clearpage
\lipsum[31-50]

\end{document}


Does there exist another way of achieving the desired result? Or is it just a bug I encountered with atbegshi/geometry?

Note:
Because I neither know the text to be placed at the first page nor its length, the \restoregeometry command can't be placed manually.

-
The main problem is not to change the geometry between pages but to change it in the middle of a paragraph: If the pagebreak is in the middle of a paragraph this paragraph will use the geometry of the first page. – Ulrike Fischer Aug 2 '12 at 10:45
@UlrikeFischer: Thank you for your comment. Is there a way to change the layout mid-paragraph? Nevertheless, as you may note by executing the first MWE, the geometry isn't restored using \AtBeginShipoutNext{\restoregeometry} -- even not after a page break. – DigNative Aug 2 '12 at 11:03

In the model that TeX uses there is no way to achieve your goal other than manually or in very very restricted contexts. As TeX is first generating paragraphs and only at the end of a paragraph checking whether or not to cut a page you will always end up on the second page with some part of the paragraph already been typeset (and thus to the width of the previous page).

You can achieve your goal by manually splitting the paragraph yourself (and effectively restarting a new one (with different settings afterwards)). I.e., if you know what the last word on the first page is you can put special command at this point, say, \EndOfFirstPage. The define this command in the preamble or in your class file as follows:

\newcommand\EndOfFirstPage{%
\setlength\parfillskip{0pt}%  line should end at the right
\par                       %  finish paragraph for now
\setlength\parfillskip{0pt plus 1fil}% default again
\pagebreak
...                        % <--- change your geometry settings here
\noindent                   % new paragraph on the new page that claims to be
% a continuation
}


Of course, whenever you later change the text of the first page you may need to move the split point around. Or you could combine the above with your approach using the atbegshi package for setting the geometry and the \EndOfFirstPage only to produce the split. This has the advantage that the geometry would be nearly correct even if the command is not there to indicate the split (only the par at the page break wrong) and you could delay adding \EndOfFirstPage to a time when you are mostly done with the editing.

Automatic methods would involve keeping track of where you are on the page (how many more lines would fit) somehow capturing each paragraph untypeset, trial typesetting it to find out how long it would be, then if necessary generating a complex \parshape so that the lines of the last paragraph on the page change width on the page boundary.

Bottom line, such thing doesn't exist and it is not something that is in any way easy or even possible to be worked out reliably (non of the experts in the last 20 years overcame that limitation). We now think that we do have code for it in the LaTeX3 project but so far only an idea (and one that wouldn't work with current 2e's output routine, so this isn't going to help easily).

-
Frank: Just for the record - DocScape is doing exactly that :-) – Stephan Lehmke Aug 4 '12 at 8:19
@StephanLehmke doesn't surprise me, but that isn't TeX doing it, you keep a lot of processing outside ... which reminds me ... experimental license? – Frank Mittelbach Aug 4 '12 at 8:50
Er wut? DocScape is a pure TeX macro package. Concerning the license - my boss is in Shanghai right now. I'll remind him when he's back ;-) – Stephan Lehmke Aug 4 '12 at 9:08
@StephanLehmke then perhaps I misunderstood: "... is doing just that" means doing it manually like that? or doing the automation? – Frank Mittelbach Aug 4 '12 at 9:28
DocScape is implementing the automatism you outlined: At a page break, the next page can have any geometry, and if a paragraph falls on the page break, a \parshape is automatically calculated to reflect the difference in width. This kind of automatism is neccessary anyway because the following workflow is possible: When the new page is created, some objects are placed on it which the text needs to flow around. Hence, when beginning to format the paragraph, it is impossible to know what its shape is gonna be. So it has to be re-formatted. – Stephan Lehmke Aug 4 '12 at 9:58