8

I am trying to force a figure and an image to show up on the same page back to back. I read the solution of using the \afterpage which works great. Except now the \label is undefined and I cannot refer to them!

What I have so far:

\afterpage{
    \begin{figure}
        \centering
        \includegraphics[scale=0.28]{PATH_TO_IMAGE}
        \caption{Some Caption For My Image}
        \label{myFigure}
    \end{figure}
    \begin{table}
        \centering
        \caption{Some Caption For My Table}
        \label{myTable}
        \includegraphics[scale=0.09]{PATH_TO_TABLE}
    \end{table}
} 

Then I refer to these normally, using \ref{myFigure} or \ref{myTable}. But the compiler says undefined references, and the PDF shows ??. If I remove the /afterpage, then the reference works just fine.

Why?

EDIT

This is for my dissertation paper at my university. The following template is provided by the university to be used.

The path to the main .tex file is here

Kousha
  • 737
  • 1
    Can you please add a full minimal working example so that people can see what packages etc you need to get your code to work (but don't add code that is not needed). This makes it much easier for people to help you. –  Sep 11 '14 at 01:47
  • 1
    Your configuration seems to be causing the problem as I just threw together a quick MWE and the links were fine, both with and without hyperref (and \autoref). Please post a MWE! –  Sep 11 '14 at 01:57
  • @Andrew, thanks for the response. This is a dissertation paper and I am using the template provided by the university. I provided the links above. Is this enough as a MWE? – Kousha Sep 11 '14 at 03:06
  • If you wish to keep things together on the same page back-to-back, you could include them in the same float. Use \captionof (from capt-of or caption) to provide the correct \caption for the alternative float. – Werner Sep 11 '14 at 03:45
  • Heiko's answered the question although it's hard to see why you need afterpage here. – David Carlisle Sep 11 '14 at 10:57
  • @DavidCarlisle, I need the \afterpage cause I need to force the figure and table to be on the same page. – Kousha Sep 11 '14 at 19:08
  • @user834045 afterpage is a highly invasive package that does lots of stuff and more or less none of that is at all relevant to keeping floats on the same page. In what way are you using it? I wrote the thing and I can't guess:-) – David Carlisle Sep 11 '14 at 19:17
  • @DavidCarlisle, thanks for the response. I Googled the question on how to force an image and a table to be on the same page, and I got this page: http://tex.stackexchange.com/questions/65606/join-figure-and-table-on-the-same-page where it was suggested to use \afterpage. – Kousha Sep 11 '14 at 19:18
  • @user834045 urg I wouldn't do that, although I suppose it sort of works (although it doesn't guarantee they end up on the same page it makes it a bit more likely) I'd do what werner suggests in the comment above, which is guaranteed to keep things together – David Carlisle Sep 11 '14 at 19:21
  • @DavidCarlisle, will this work with figure and tables? And each will have a different label/numbering? – Kousha Sep 11 '14 at 19:57
  • @user834045 yes – David Carlisle Sep 11 '14 at 19:59

1 Answers1

7

It's a timing problem between afterpage and the end of \include. After the .tex file is read, \include calls \clearpage, which is redefined by afterpage. First the page is output, then the stuff inside \afterpage is executed. The stuff are float objects, thus they are not yet set, waiting for the next call of the output routine. In the meantime, \include closes the .aux file for the included file and finishes its job. Then, when the floats are output, the labels are written to the old .aux file for the former included file, but this .aux file is closed, thus the labels are written to the .log file and console instead. In the next LaTeX run the labels remain unknown.

Workaround: An explicit \clearpage at the end of the included file helps. Then this \clearpage triggers the afterpage hook to execute, and the \clearpage at the end of \include will trigger the floats to output before the .aux file gets closed.

Heiko Oberdiek
  • 271,626