[Tex/LaTex] Change page layout after first page

geometrymarginspage-breaking

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
\geometry{a4paper, includeall, left=10mm, right=10mm, top=10mm, bottom=10mm, head=0mm, foot=0mm, marginpar=0mm, marginparsep=0mm, headsep=0mm}

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

\begin{document}
    % define a new page layout for the first page
    \newgeometry{left=10mm, right=50mm, top=10mm, bottom=10mm, head=0mm, foot=0mm, marginpar=0mm, marginparsep=0mm, headsep=0mm}

    \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
\geometry{a4paper, includeall, left=10mm, right=10mm, top=10mm, bottom=10mm, head=0mm, foot=0mm, marginpar=0mm, marginparsep=0mm, headsep=0mm}

% define the first page layout
\AtBeginShipoutFirst{\newgeometry{left=10mm, right=50mm, top=10mm, bottom=10mm, head=0mm, foot=0mm, marginpar=0mm, marginparsep=0mm, headsep=0mm}}

% 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.

Best Answer

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).