includegraphics[page=\pageref… possible? how to set [page=] to a variable?

I'm currently writing a quite complex document with a lot of graphics created using TikZ. Hence I decided to put all graphics into one big file called tikz.tex creating a multiple page PDF with one tightly cropped page per graphic.

The tikz.tex (which I compile separately) generates a clean PDF and .aux file. Here is a minimal version of it:

\documentclass{article}
\usepackage{tikz}
\usepackage[pdftex,active,tightpage]{preview}
\tikzset{
%... my tikzset ...
}
\begin{document}
\begin{preview}
\label{pic1}
\begin{tikzpicture}
%... tikz ...
\end{tikzpicture}
\end{preview}
\begin{preview}
\label{pic2}
\begin{tikzpicture}
%... tikz ...
\end{tikzpicture}
\end{preview}
\end{document}


the minimal version of my Master file

\documentclass[]{article}
\usepackage {xr}
\usepackage []{hyperref}
\externaldocument[tikz:]{../Bilder/tikz}
\begin{document}
\input{../Kapitel/Kapitel_1}
\end{document}


a minimal version of the actual chapter, which is included in the master file, would look similar to this

\chapter{name}
\begin{figure}[htb]
\includegraphics[page=\pageref{tikz:pic2}]{../Bilder/tikz.pdf}
\end{figure}


this returns:

! Missing number, treated as zero. <to be read again> \protect l.12
...KOS},width=\textwidth]{../Bilder/tikz.pdf} A number should have
been here; I inserted 0'.


The idea was to use the labels in my main document to refer to the correct page number. of course i could just hard code the page number but since I intend to use this tikz file for quite some time I'd prefer referencing the page number by \pageref or something similar. I do not want to split the PDF into single files.

It works if my chapter looks like this. in this case the \pageref is shown correct

\chapter{name}
\begin{figure}[htb]
\includegraphics[page=2]{../Bilder/tikz.pdf}
\end{figure}
correct page number of pic 2 is \pageref{tikz:pic2}


Any ideas how to get \includegraphics[page=\pageref{...}] working?

-
Note that chapters are usually inserted in the main document using \include not \input. See e.g. When should I use \input vs \include? for more details. –  Martin Scharrer Oct 13 '11 at 18:16
–  Lev Bishop Oct 13 '11 at 18:41
Your right, I should rather use \include but since its the standard layout of my department and the slow down is acceptable changing this would be a lot of work and, for the time being, unnecessary. –  Argysh Oct 14 '11 at 8:43

I would use the zref package for this. It comes with a zref-xr module (i.e. sub-package, i.e. package) and allows to extract the page number from reverences as plain number which can be used in \includegraphics. The normal \pageref usually included other code which makes it unsuitable. You might need to fully expand the page number before you pass it to the page key of \includegraphics. It is also possible to define your own key just for this purpose.

A minimal example would be:

% tikzstuff.tex
\documentclass{article}
\usepackage{tikz}
\usepackage[user]{zref}
\usepackage[pdftex,active,tightpage]{preview}
\begin{document}
\begin{preview}
\zlabel{pic1}
\begin{tikzpicture}
\draw (0,0) -- (10,10);
\node at (5,5) {\large\sffamily 1};
\end{tikzpicture}
\end{preview}
\begin{preview}
\zlabel{pic2}
\begin{tikzpicture}
\draw (0,10) -- (10,0);
\node at (5,5) {\large\sffamily 2};
\end{tikzpicture}
\end{preview}
\end{document}


for the TikZ diagram and the following main file:

\documentclass{article}
\usepackage[xr]{zref}
\usepackage{graphicx}
\usepackage{hyperref}
\zexternaldocument[tikz:]{tikzstuff}

\makeatletter
\define@key{Gin}{bylabel}{%
\edef\@tempa{{Gin}{page=\zref@extract{tikz:#1}{page}}}%
\expandafter\setkeys\@tempa
}
\makeatother

\begin{document}

...

\includegraphics[bylabel=pic1]{tikzstuff}% Include image reference as 'pic1' in the external document

...

\includegraphics[bylabel=pic2]{tikzstuff}

\end{document}


Note that you could just place every TikZ picture in an own file and use the standalone bundle to compile them on their own while also be able to insert them into a main document. This saves you the issues with the page references.

-
That works just like i wanted, thanks a lot! I used the standalone package before I decided to put everything into one file. by this i can create some sort of graphics library which I can use for all my future related work. yet splitting up a picture or inserting one variant of it doesnt effect referencing due to new page numbers anymore. –  Argysh Oct 14 '11 at 8:33

try

\chapter{name}
\begin{figure}[htb]
\edef\temp{page=\pageref{tikz:pic2}}
\expandafter\includegraphics\expandafter[\temp]{../Bilder/tikz.pdf}
\end{figure}

-
That would only expand the p of page before \includegraphics. When hyperref is loaded \pageref isn't expandable anyway, AFAIK. –  Martin Scharrer Oct 13 '11 at 18:17
@MartinScharrer This would correctly expand \temp; but the remark about the unexpandability of \pageref is correct. Loading the refcount package one can use \getpagerefnumber that is expandable. –  egreg Oct 13 '11 at 20:57
@egreg: My comment was about the first version which didn't used \temp. The hint to refcount` is good. I didn't know it before. –  Martin Scharrer Oct 14 '11 at 17:28