56

My document should be letterpaper and my attachments should be a4paper. I had high hopes for the \newgeometry command from the geometry package, but the documentation says

\newgeometry is almost similar to \geometry except that \newgeometry disables all the options specified in the preamble and skips the papersize-related options: landscape, portrait and paper size options (such as papersize, paper=a4paper and so forth).

Any help is appreciated!

doncherry
  • 54,637
u17
  • 4,976

3 Answers3

49

The following works in PDFLaTeX:

\documentclass{article}
\begin{document}
Normal page
\eject \pdfpagewidth=3in \pdfpageheight=10in
Tall page
\eject \pdfpagewidth=10in \pdfpageheight=3in
Wide page
\end{document}

That should work with Xelatex. See the SO qn, Change paper size in the middle of a latex document?, for more.

Charles Stewart
  • 21,014
  • 5
  • 65
  • 121
  • 3
    When I use that, how can I reset everything to the page dimension of the document class for the next page? – WarrenFaith Feb 18 '11 at 14:28
  • 3
    @Warren: These are normal dimension variables, so you can save the current values of the pages in the usual way using, say, \classpageheight=\pdfpageheight at the beginning and restore using \pdfpageheight=\classpageheight. – Charles Stewart Feb 19 '11 at 21:33
  • I am a complete beginner, so I don't know the "usual way". Thanks for the information! – WarrenFaith Feb 19 '11 at 22:56
  • IMHO \clearpage or \newpage should be used in a LaTeX document instead of the plainTeX \eject. – Martin Scharrer Jun 22 '11 at 10:53
  • Also this seems not to adjust the \textheight and \textwidth. In my tests the footer is still placed at the old position, etc. – Martin Scharrer Jun 22 '11 at 11:14
  • @Martin: \clearpage/\newpage - I don't appreciate the advantages. I tend to use macros that do the same thing with both Latex and Context. \textheight and \textwidth - good point, and I suppose all layout dimensions would benefit from fine-tuning. It would be a rather longer answer if I did that, but I guess more useful. – Charles Stewart Jun 22 '11 at 15:47
  • 5
    @CharlesStewart : do you mean "\newlength{\classpageheight} \setlength{\classpageheight}{\pdfpageheight}" instead of "\pdfpageheight=\classpageheight"? (as I cannot get the stanza with the = to work)... Thanks! – sdaau May 11 '12 at 10:08
  • 2
    @sdaau: Yes, you need to tell Tex that \classpageheight is a dimension using \newdim or \newlength, or as you have found, the assignment (using = or \setlength) won't work. – Charles Stewart May 14 '12 at 18:45
  • 1
    Anything that works with lualatex? – MaxNoe Feb 19 '18 at 14:56
  • 1
    With this, page number is not at the bottom but at it's usual location with huge space below it. Because of this my table is moving to next page. Any idea how to fix it? – Nagabhushan S N Feb 13 '20 at 05:49
  • 1
    Checkout this answer to make page numbers go to bottom – Nagabhushan S N Feb 13 '20 at 09:23
  • If am using \eject \pdfpagewidth=500pt \pdfpageheight=500pt \includepdfmerge[fitpaper]{/home/simha/latex/test.pdf, 497} then fitpaper does not respect the \eject \pdfpagewidth=500pt \pdfpageheight=500pt It changes the size of the paper. I am expecting fitpaper to fit into the 500pt x 500pt page size and also align it in the top left corner – Santhosh Aug 01 '20 at 08:23
20

You can use the typearea package (KOMA-Script bundle) to change the paper size and also recalculate all dependent margins, header and footer positions etc. The following code demonstrates the technique but generates some warnings related to offended typesetting rules (placing 10pt font on A3 is not recommended). You might need to adjust some further typearea settings for better results.

\documentclass{article}
\usepackage[paper=A4]{typearea}
\usepackage{lipsum}% dummy code

\begin{document}
\lipsum% Placed on A4

\KOMAoptions{paper=A3}
\recalctypearea
\lipsum% Placed on A3

\KOMAoptions{paper=A4}
\recalctypearea
\lipsum% Placed on A4 again

\end{document}

Some KOMA-Script options trigger a recalculation of the type area and the “page setup” automatically, but paper does not. Nevertheless, a recalculation mid-document results in a new page, so no \clearpage, \newpage or \eject is necessary here.

Martin Scharrer
  • 262,582
  • 2
    if anyone else got wondering what's the package name responsible for this code, it is koma-scripts – igorsantos07 Jan 09 '16 at 00:12
  • 2
    Checkout this answer to use custom sizes pages – Nagabhushan S N Feb 13 '20 at 09:27
  • If you do not need specific margins simply use:

    ""\KOMAoptions{paper=250mm:500mm, paper = landscape, DIV=15} \recalctypearea""

    -- DIV sets roughly the margins based on the font. -- paper = landscape or portrait sets the larger length as the width/height respectively

    – Lorenzo Chiaverini Apr 12 '23 at 23:31
6

Here is an abridged example which switches between two custom page sizes with corresponding layouts (margins), which can compile with pdflatex - for more details, see my answer in Problem with setting correct custom page size with 'geometry' in xelatex:

\documentclass{article}


% reminder: US letter: 596pt x 795pt

\newlength{\pagewidthA}
\newlength{\pageheightA}
\setlength{\pagewidthA}{300bp}
\setlength{\pageheightA}{400bp}

\newlength{\pagewidthB}
\newlength{\pageheightB}
\setlength{\pagewidthB}{400bp}
\setlength{\pageheightB}{500bp}

% stockwidth and stockheight - from memoir,
% here just for cheating `layouts` warnings
\newlength{\stockwidth}
\newlength{\stockheight}


\usepackage{geometry}

% this command will take effect into L1 layout just after \begin{document}
% but the L1 geometry will otherwise be ignored
\geometry{twoside,inner=50bp,outer=30bp,top=50bp,bottom=50bp}

\usepackage{tikz,enumitem}
\usepackage{fix-cm}

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

\usepackage{lipsum}



\makeatletter
\newcommand{\printpagevalues}{%
  % from geometry.sty:
  * paper: \ifx\Gm@paper\@undefined<default>\else\Gm@paper\fi \\%
  * layout: \ifGm@layout<custom>\else<same size as paper>\fi \\%
  \@ifundefined{ifGm@layout}{}{%
  \ifGm@layout
  * layout(width,height): (\the\Gm@layoutwidth,\the\Gm@layoutheight) \\%
  \fi
  * layoutoffset:(h,v)=(\the\Gm@layouthoffset,\the\Gm@layoutvoffset) \\%
  }%
  \pagevalues % from package layouts
}
\makeatother


\newcommand{\generatePageLayouts}{%
  \newgeometry{layoutwidth=\pagewidthA,layoutheight=\pageheightA,left=1mm,right=5mm,bottom=1mm,top=1mm}
  \savegeometry{LayoutPageA}

  \newgeometry{layoutwidth=\pagewidthB,layoutheight=\pageheightB,twoside,inner=2.5cm,outer=0.5cm,top=1.5cm,bottom=1.5cm}
  \savegeometry{LayoutPageB}
}


\newcommand{\switchToLayoutPageA}{%
  % switch page size first:
  \pdfpagewidth=\pagewidthA \pdfpageheight=\pageheightA % for PDF output
  \paperwidth=\pagewidthA \paperheight=\pageheightA     % for TikZ
  \stockwidth=\pagewidthA \stockheight=\pageheightA % hyperref (memoir)?!
  \loadgeometry{LayoutPageA} % note; \loadgeometry may reset paperwidth/h!
}

\newcommand{\switchToLayoutPageB}{%
  % switch page size first:
  \pdfpagewidth=\pagewidthB \pdfpageheight=\pageheightB % for PDF output
  \paperwidth=\pagewidthB \paperheight=\pageheightB     % for TikZ
  \stockwidth=\pagewidthB \stockheight=\pageheightB % hyperref (memoir)?!
  \loadgeometry{LayoutPageB} % note; \loadgeometry may reset paperwidth/h!
}



\begin{document}
  % here geometry layout L1 is instantiated;
  % without anything else, paper size defaults to US letter!
  % \restoregeometry command restores L1!!

  \fontsize{8}{9}\selectfont % this nowork in preamble!


  % generate page layouts first based on layoutwidth as page size;
  % don't switch actual page sizes yet:
  \generatePageLayouts{}


  %%% start with content

  % start with LayoutPageA (includes switching page size)
  \switchToLayoutPageA{}


    \pagestyle{empty} % no page numbers here
    \\
     \the\paperwidth
    \lipsum[1]
    \printpagevalues{}
    \clearpage

  % switch to LayoutPageB (includes switching page size)
  \switchToLayoutPageB{}


    % start page numbering here ("this will reset the page number"):
    \pagenumbering{arabic}
    % make page numbers visible
    \pagestyle{plain}


    \the\paperwidth
    \lipsum[1]

    Trying

    some

    text

    \printpagevalues{}
\end{document}
David Carlisle
  • 757,742
sdaau
  • 17,079