Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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 
\addcontentsline{toc}{section}{section 1}
\newpage
%empty page

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

\addcontentsline{toc}{section}{section 2} %lost
\end{document}
share|improve this question
    
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

1 Answer 1

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 
\addcontentsline{toc}{section}{section 1}
\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`
\addcontentsline{toc}{section}{section 2} %lost
\endgroup

\end{document}
share|improve this answer
    
Thanks for your help. However, it put a black rectangle box in the pdf instead of the pic.jpg –  ktran9.lbs Feb 13 '13 at 14:36
    
@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. –  tohecz Feb 13 '13 at 15:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.