# Problem with setting correct custom page size with 'geometry' in xelatex

I'm trying to set the page size using the geometry package as shown in the following MWE, compiled with xelatex test.tex:

\documentclass[12pt]{article}

\usepackage[b5paper,pass]{geometry}
\geometry{b5paper}
\usepackage{tikz,enumitem}

\usepackage{fix-cm}

\usepackage{fontspec}
\defaultfontfeatures{Ligatures=TeX}
\setmainfont{Junicode}

\usepackage{layouts}

\usepackage{etoolbox}
\patchcmd{\drawpage}{\ifdrawparameters}{\iftrue}%
{\typeout{^^J*******\string\drawpage fixed*******^^J}}%
{\typeout{^^J*******\string\drawpage not fixed*******^^J}}

\usepackage{lipsum}

\begin{document}

\lipsum[1]

\end{document}


If I then try to check the page size using pdfinfo (on Ubuntu), I get size of US letter (?!):

$pdfinfo -box -f 1 -l 1 test.pdf | grep Page Pages: 1 Page 1 size: 612 x 792 pts (letter) Page 1 MediaBox: 0.00 0.00 612.00 792.00 Page 1 CropBox: 0.00 0.00 612.00 792.00 Page 1 BleedBox: 0.00 0.00 612.00 792.00 Page 1 TrimBox: 0.00 0.00 612.00 792.00 Page 1 ArtBox: 0.00 0.00 612.00 792.00  However, if I enter b5paper in \documentclass[b5paper,12pt]{article} - then I can just as well delete all the other references to geometry using b5paper - because when I compile with that change, I get: $ pdfinfo -box -f 1 -l 1 test.pdf | grep Page
Pages:          1
Page    1 size: 498.9 x 708.66 pts
Page    1 MediaBox:     0.00     0.00   498.90   708.66
Page    1 CropBox:      0.00     0.00   498.90   708.66
Page    1 BleedBox:     0.00     0.00   498.90   708.66
Page    1 TrimBox:      0.00     0.00   498.90   708.66
Page    1 ArtBox:       0.00     0.00   498.90   708.66


... which (I guess) corresponds to correct B5 measures.

Now, as far as I had believed until now - one should be able to change the paper size with geometry - as long as those changes are issued before \begin{document}, is that correct? If so, why doesn't it work here?

I'm wandering, because I wanted to set some custom page sizes with geometry in this way (and which thus I couldn't propagate to the \documentclass command).

Many thanks in advance for any suggestions on how to set a custom page size in this example,
Cheers!

-
You have to pass b5paper as an option to \documentclass rather than to geometry. –  egreg May 13 '12 at 20:21
Thanks for that, @egreg - but I wanted a solution outside of \documentclass, since I want to set custom page sizes.. I came up with a sort of a hack below, but I'm not sure if that is the way custom page sizes should be handled.. Thanks again - cheers! –  sdaau May 13 '12 at 20:31

Well, finally I think I got somewhere with switching between custom page sizes and page layouts with geometry in mid-document (the MWE is below (see also previous post on geometry's pass); click on image for full size):

First of all, a couple of words about geometry:

• You can call \geometry only in preamble, and there multiple commands have simply the effect of replacing previously set values of parameters
• geometry first calculates a layout (margins etc) after \begin{document}, which the manual calls layout L1
• The command \restoregeometry can only restore L1!
• The command \newgeometry can only be called after \begin{document} (and thus, after L1 is created)
• Layouts created with \newgeometry can be \save/\loadgeometry-ied
• \newgeometry does NOT care about \paperwidth/height! In fact, if you thought you could cheat with:
\setlength{\paperwidth}{100mm}
\setlength{\paperheight}{100mm}
\newgeometry{twoside,...}
, geometry will care not a bit, and will in fact reset paperwidth/height back to the documentclass setting (or I guess, the last named page size setting in geometry, if any).
• However, geometry does take into account parameters called layoutwidth and layoutheight, which serve the purpose of page size limits.

Thus, how geometry calculates margins, is independent of the actual page setting in \paperwidth/\pdfpagewidth. Having all this in mind, the following checklist can be submitted for managing custom page sizes and layouts:

• Prepare in preamble custom page sizes as new lengths
• Prepare in preamble commands \generatePageLayouts, \switchToLayoutPageA and \switchToLayoutPageB
• After \begin{document}, call first \generatePageLayouts, not doing any actual page size switching with \paperwidth/\pdfpagewidth
• it will call \newgeometry with layoutwidth/height set to respective custom pagesize newlengths, and then \savegeometry
• Then \switchToLayoutPageA and typeset content
• it will do actual page switching with \paperwidth/\pdfpagewidth, and call respective loadgeometry
• Then \switchToLayoutPageB and typeset content
• it will -- || --

Note that even \loadgeometry resets \paperwidth/height - which can be seen on the image, in the table produced by layouts: there it is reported, on both pages, that: \paperwidth = 614.295pt - even if neither of the pages is of that size:

Page    1 size: 288.92 x 358.66 pts


Notably, I thought I asked for 290 x 360 pts page; but pdfinfo says the size is actually 288.92 x 358.66 pts. Turns out, what I need is the 'big points' (bp) LaTeX length units - so if I rewrite:

\setlength{\classpagewidth}{290bp}
\setlength{\classpageheight}{360bp}


... then indeed pdfinfo reports the requested size:

\$ pdfinfo -box -f 1 -l 1 test.pdf | grep Page | grep size
Page    1 size: 290 x 360 pts


Still not sure if this is the way things are supposed to be done, though ... Cheers!

-