# \addcontentsline on a page with image

According to Added chapter with \addcontentsline does not appear on ToC, the reason why section 2 doesn't show up in the TOC is because it is an empty page. However, even when I added an image, TOC still doesn't include section 2. I'm guessing because of the same reason (page with empty TEXT). Is there a way to get around this problem? FYI, I'm able to add text into section 2 and it works perfectly but just want to know a clean way of doing this.

\documentclass{article}

\usepackage{graphicx}

\begin{document}
\tableofcontents

blub
\newpage
%empty page

\begin{figure}[t]
\centering
\includegraphics[width=0.9\textwidth]{pic.jpg}
\caption{pic A}
\label{picA}
\end{figure}

\end{document}

-
Try replacing [t] by [h] or [ht]. Putting \newpage before a float doesn't mean that the float is placed in the next page. – hpesoj626 Feb 13 '13 at 7:00
tried your suggestions but still not working. – ktran9.lbs Feb 13 '13 at 7:14
The float is inserted after the empty page has already been "thrown away". You can put the \addcontentsline inside the float. Or use the float package and the [H] option. Or don't use a float (figure) at all but print the caption with \captionof. – Ulrike Fischer Feb 13 '13 at 10:10
Ulrike, ALL of your suggestion works and they are also simple and easy to understand. If you'd like to, you can post them as answer and I'll accept it. Thank you. – ktran9.lbs Feb 13 '13 at 14:38

## Disclaimer

This solution will produce wrong page number in some cases! Use with caution!

## Solution

The real problem is that \write (which is used by \addcontentsline) gets lost if it's after all material in the document. If we make it \immediate\write, it gets into the ToC, but possibly with a wrong page number, the one where it was expanded, instead of the one where the surrounding text was typeset.

\documentclass{article}

\usepackage{graphicx}

\makeatletter
\let\orig@write\write
% xacl = extra add contents line
% USE WITH CAUTION!!!
\def\xacl{\def\write{\immediate\orig@write}}
\makeatother

\begin{document}
\tableofcontents

blub
\newpage
%empty page

\begin{figure}[t]
\centering
\includegraphics[width=0.9\textwidth]{pic.jpg}
\caption{pic A}
\label{picA}
\end{figure}

\begingroup
\xacl % locally make \write -> \immediate@write

@ktran9.lbs That is because I added [demo] option to graphicx package. If you made your example self-contained, I would not have to do so. Just remove that option. – yo' Feb 13 '13 at 15:09