# Centering full-page tikz images - with mid-document paper size change

I just got this question answered:

xetex - Centering full-page Tikz image on page without margins with xelatex? - TeX - LaTeX

... and all works there.

However, here is my problem - I basically have a document whose normal pages are A4, whereas these SVG->tikz images are supposed to be A5. So, I try to:

• define the document as A4;
• use the trick from Change paper size in mid-document with setting \pdfpagewidth/height; to switch the necessary pages to A5

... however, but that seems to mess up the page calculation Tikz uses.

Here is a MWE (same as in first link, changed so it includes the tikz image instead):

test.tex

\documentclass[12pt,a4paper]{article}

\usepackage[pass]{geometry}
%   %\usepackage{hyperref}
\usepackage{tikz}

\begin{document}

% \thispagestyle{empty}
\pdfpagewidth=148mm \pdfpageheight=210mm

\input{testin.tex}

\end{document}


testin.tex

\definecolor{cff0000}{RGB}{255,0,0}

\begin{tikzpicture}[overlay,remember picture,anchor=north west,inner sep=0pt, outer sep=0pt]

\node at (current page.north west) {%

\begin{tikzpicture}[overlay,remember picture,y=0.80pt,x=0.80pt,yscale=-1, inner sep=0pt, outer sep=0pt,anchor=north west]

\begin{scope}[shift={(0,0.34461553)}]% layer1

% rect3016
\path[color=black,fill=cff0000,line width=0.800pt,rounded corners=0.0000cm]
(0.0000,-0.3446) rectangle (524.4094,743.7499);

\end{scope}

\end{tikzpicture}

};

\end{tikzpicture}


Here is a little experiment with these files I did; when it says (clear state), means I've removed the .aux beforehand:

1. With \documentclass[12pt,a5paper]{article}; without \pdfpagewidth/height setting
1. xelatex first pass (clear state), .aux:
\relax
\pgfsyspdfmark {pgfid2}{5924114}{69025514}
\pgfsyspdfmark {pgfid1}{5891346}{29900006}
2. xelatex second pass, (page OK), .aux:
\relax
\pgfsyspdfmark {pgfid2}{32768}{39125508}
\pgfsyspdfmark {pgfid1}{5891346}{29900006} 
2. With \documentclass[12pt,a4paper]{article}; without \pdfpagewidth/height setting
1. xelatex first pass (clear state), .aux:
\relax
\pgfsyspdfmark {pgfid2}{7955730}{101470942}
\pgfsyspdfmark {pgfid1}{7922962}{46122720}
2. xelatex second pass, (img aligned, but page is bigger; as expected), .aux:
\relax
\pgfsyspdfmark {pgfid2}{32768}{55348222}
\pgfsyspdfmark {pgfid1}{7922962}{46122720}
3. With \documentclass[12pt,a4paper]{article}; WITH \pdfpagewidth/height setting; no clear state
1. xelatex first pass (continued from previous a4paper - page is OK!), .aux:
\relax
\pgfsyspdfmark {pgfid2}{32768}{39125508}
\pgfsyspdfmark {pgfid1}{7922962}{29900006}
2. xelatex second pass (image goes out of page), .aux:
\relax
\pgfsyspdfmark {pgfid2}{32768}{55348222}
\pgfsyspdfmark {pgfid1}{7922962}{29900006}
4. With \documentclass[12pt,a4paper]{article}; WITH \pdfpagewidth/height setting
1. xelatex first pass (clear state; first pass img not even visible), .aux:
\relax
\pgfsyspdfmark {pgfid2}{7955730}{85248228}
\pgfsyspdfmark {pgfid1}{7922962}{29900006}
2. xelatex second pass (image goes out of page), .aux:
\relax
\pgfsyspdfmark {pgfid2}{32768}{55348222}
\pgfsyspdfmark {pgfid1}{7922962}{29900006}

The only thing I can conclude from this experiment, is that the page is OK, when the .aux file (after the xelatex run) contains:

\relax
\pgfsyspdfmark {pgfid2}{32768}{39125508}
\pgfsyspdfmark {pgfid1}{7922962}{29900006}


... so apparently, it is possible to position the image correctly, even if documentclass is A4 page, and I switch "manually" to A5 using \pdfpagewidth/height; just don't know how to do this correctly :)

So, is there a way to write the .tex, so I can position the A5 tikz box all over the page, which is manually switched to A5 ?

(NB: I'd like to keep all this as tikz/latex code, I'm not too keen on exporting individual PDFs and including them with say pdfpages or the like).

Cheers!

-

It is possible to position the image without redefining the page geometry. Redefining the page geometry in general can give you problems.

First let us use some TikZ code to position a block at the top of the page. the block will be half an A4 size i.e., an A5 paper size. When we use an overlay as you see from the image below:

The MWE is shown below:

\documentclass[a4paper]{article}
\usepackage{tikz,lipsum}
\usepackage{hyperref}
\begin{document}
\thispagestyle{empty}
\begin{tikzpicture}[overlay,remember picture,anchor=north west,inner sep=0pt, outer sep=0pt]
\node at (current page.north west) {%
\begin{tikzpicture}[overlay,remember picture, inner sep=0pt, outer sep=0pt,anchor=north west]
\path[color=black,fill=purple]
(0,0) rectangle ++(\the\paperwidth,-0.5\paperheight);
\end{tikzpicture}
};
\end{tikzpicture}
\lipsum
\end{document}


Now of course this is not satisfactory as you want to position the text after the image or the TikZ picture. In order to achieve this you enclose the tikz picture in a \vbox set at half-page height.

\documentclass[a4paper]{article}
\usepackage{tikz,lipsum}
\usepackage{hyperref}
\begin{document}
\vbox to 0.5\paperheight{\begin{tikzpicture}[overlay,remember picture,anchor=north west,inner sep=0pt, outer sep=0pt]
\node at (current page.north west) {%
\begin{tikzpicture}[overlay,remember picture, inner sep=0pt, outer sep=0pt,anchor=north west]
\path[color=black,fill=purple]
(0,0) rectangle ++(\the\paperwidth,-0.5\paperheight);
\end{tikzpicture}%
};%
\end{tikzpicture}}
\section{Test}
\lipsum
\end{document}


If you replace the code in the node with an includegraphics command, your image should be imported at the top of the page with no problems.

It is also possible to do the same without any tikZ code. I will post this a bit later if this is what you are after.

As pdf uses different parameters for paperheight and paperwidth, I normally use the package hyperref to ensure they are corrected. If you do not want to load it you can use:

\newcommand*{\fixpdflayout}{%
\pdfpageheight=\the\paperheight
\pdfpagewidth=\the\paperwidth
\ifxetex\else
\ifdim\pdfvorigin=0pt\pdfvorigin=1in\fi
\ifdim\pdfhorigin=0pt\pdfhorigin=1in\fi
\fi}

-
Awesome - many thanks, @YiannisLazarides; I just replaced my \pdfpagewidth... stanza with \paperwidth=148mm \paperheight=210mm \n \fixpdflayout; and all seems to work great now! (PS: here I'm not after "without any tikZ code", because here I'm using inkscape2tikz, which I didn't mention in this question). Many thanks again - cheers! –  sdaau May 11 '12 at 16:21

What TikZ needs is to know the paper dimensions and apparently it uses \paperwidth and \paperheight:

{\clearpage
\thispagestyle{empty}
\pdfpagewidth=148mm \pdfpageheight=210mm % for PDF output
\paperwidth=148mm \paperheight=210mm     % for TikZ
\input{testin.tex}
\clearpage}

-
Many thanks for that, @egreg - works great! I put the accept with @YiannisLazarides' answer, since it includes some tips others may be interested in. Cheers! –  sdaau May 11 '12 at 16:22