# Odd Cross-Referencing Behaviour

This is not a question about solving a problem, because I already know how to solve it - several ways, in fact. It's more that I just want to understand what is going on.

Here is my code example, stripped down to the barest essentials.

\documentclass[a4paper]{article}

\setlength{\textwidth}{150mm}
\setlength{\textheight}{225mm}

\usepackage{graphicx}

\newcommand{\includePicture}[2]{\begin{center}\includegraphics[width=#1\linewidth]{#2}\end{center}}

\begin{document}
\section{First section}
Reference page \pageref{examplePicturePage}.

\section{Second section}
\includePicture{0.9}{example-image-10x16}\label{examplePicturePage}
\end{document}


This generates an undefined page reference, regardless of how many times the code is compiled (pdflatex). If the \textwidth length is made one millimeter smaller, or the \textheight is made one millimeter longer, the error goes away. Removing the \begin{centre} and \end{centre} in the command definition also make the error go away, in this case, regardless of the \textwidth and \textheight definitions (I put in 2mm for \textheight, still no undefined reference.)

So I guess there is an overflow, when the included picture, formatted to the specified width, is too long to fit on the page. What I think is happening is, the overflow occurs while everything is being processed within the center environment. Then, the \end{centre} closes that, and the processing continues onto a new page. But there is no content on the new page, nothing at all, except the \label command. So this becomes a reference to a non-existent page. However, when the centre environment is removed, the \label gets processed before we get pushed off to the second page.

The obvious solution to this is, don't put images that are too big for the page, but I just want to make sure my understanding of what is happening is correct. Is it?

Edited at the suggestion of Mico: The original example had no counter (the real-world document does, but I stripped it out); I have added section commands now to eliminate confusion about whether the issue is \pageref or \ref. In the current incarnation, the issue is essentially the same with either \pageref or \ref.

Edited at the suggestion of Torbjørn T. - example image used to make the file self-contained, with no need for other files.

• Welcome to TeX.SE. Your code isn't compilable by folks who don't have access to the file BigPicture.pdf. If I make it compilable, e.g., by specifying the option demo while loading the graphicx package, the problem you describe goes away in the narrow sense of \pageref producing the number 1. Of course, this masks a much bigger problem: you (a) haven't defined a suitable counter variable and (b) haven't provided a suitable method for incrementing the counter variable so that LaTeX can associate this counter with a \label statement. Hence, \ref statements must fail. – Mico May 16 '18 at 7:02
• I do not seem to be able to add an attachment; however, just printing any old web page (I used this one) with the page preferences set to A4 paper seems to produce a usable "BigPicture" of the proper dimensions. I am not using \ref (well, not for this particular reference), only \pageref, so there is no need for a counter (the original full file actually has one). If I change the dimensions \textheight and/or \textwidth on the example above, the result is a *.aux file with an empty counter reference and a correct page reference, versus no entry at all with the dimensions indicated. – RLK May 16 '18 at 7:22
• In that case, you should probably edit your original query to clarify what it is (and is not) that you're trying to fix -- or, at least, understand better. – Mico May 16 '18 at 7:40
• You can use example-image-10x16 instead of BigPicture. The example-image images are included with the mwe package, which is part of TeX Live and MikTeX, so most people will have that image directly available. – Torbjørn T. May 16 '18 at 8:03
• Thanks for that tip, the example is now self-contained. I had to adjust the lengths a bit, but that wasn't a big deal. – RLK May 16 '18 at 8:10

You're being unlucky, but your command is not well defined nonetheless.

What happens is that the center environment adds vertical space before and after it. This pushes \label to be evaluated when the page has already been ejected; as there's no more vertical material available, the \label refers to no page.

The \label should be inside the center environment, next to the picture or the page reference might be incorrect anyway.

I suggest doing

\newcommand{\includePicture}[3][]{%
\begin{center}
\includegraphics[width=#2\linewidth]{#3}%
#1% some material, possibly the \label
\end{center}
}


and call it like

\includePicture[\label{examplePicturePage}]{0.9}{example-image-10x16}


You can also make the fraction optional, but in this case it's better to change the syntax, having the \label as trailing optional argument.

\documentclass[a4paper]{article}
\usepackage{xparse}
\usepackage{graphicx}

\setlength{\textwidth}{150mm}
\setlength{\textheight}{225mm}

\NewDocumentCommand{\includePicture}{O{0.9} m O{}}{%
\begin{center}
\includegraphics[width=#1\linewidth]{#2}%
#3% some material, possibly the \label
\end{center}
}

\begin{document}

\section{First section}
Reference page \pageref{examplePicturePage}.

\includePicture[0.2]{example-image}

\section{Second section}
\includePicture{example-image-10x16}[\label{examplePicturePage}]

\end{document}

• I think I will do something like this - thanks for the suggestion! I had wanted to make the size argument optional as well, but I think I'll just make it "pseudo-optional" by defining something like a \defaultPictureSize command, so that I can change the default throughout the document with a single edit, while letting individual inclusion statements override the default. That way, I don't have to get into multiple optional arguments (apparently, there are ways to do this, but I don't think I'm quite at that level yet . . .). – RLK May 16 '18 at 11:24
• @RLK I added code for making two optional arguments. – egreg May 16 '18 at 11:40
• that sounds great, I haven't implemented this, but I will definitely give it a try. I did not realise it was possible to change the command syntax to have an optional argument after, rather than before, the required arguments, but I guess the odd syntax of the "picture" environment should have given me a clue. Many thanks! – RLK May 17 '18 at 5:50