0

I've written a simple (automatized) report which includes many graphics by simple \includegraphics. Now I'd like to generate a table of contents link for each graphics which I can click due to package hyperref. So I wrote

\documentclass{article}
\usepackage{graphicx}
\usepackage{hyperref}
\begin{document}
\tableofcontents
\newpage
\section{Start}
\begin{center}
\includegraphics{mwe/example-image-a.pdf}
\end{center}
\addcontentsline{toc}{subsection}{A}

\begin{center}
\includegraphics{mwe/example-image-a.pdf}
\end{center}
\addcontentsline{toc}{subsection}{A}

\begin{center}
\includegraphics{mwe/example-image-a.pdf}
\end{center}
\addcontentsline{toc}{subsection}{A}

\begin{center}
\includegraphics{mwe/example-image-b.pdf}
\end{center}
\addcontentsline{toc}{subsection}{B}

\begin{center}
\includegraphics{mwe/example-image-a.pdf}
\end{center}
\addcontentsline{toc}{subsection}{A}
\end{document}

(not sure how MWE works :/). However, in the generated table of contents in the PDF, the links in the TOC get me to the wrong pages not showing the correct graphics. In the example clicking on TOC entry "B" does not get me to graphics "B".

Is there a way to get correct references to my images?

  • For the sake of the community, could you provide us with a minimal working example (MWE) that shows your setup? Don't use myplot.pdf, but rather example-image and such from mwe. – Werner Aug 5 '14 at 13:50
3

You have to set an anchor using \phantomsection. And I would use a minipage or \parbox to ensure that both \phantomsection and \includegraphics are on the same page:

\documentclass{article}
\usepackage{graphicx}
\usepackage{hyperref}
\begin{document}
\tableofcontents
\newpage
\section{Start}
\begin{center}
  \parbox{\linewidth}{
    \centering
    \phantomsection
    \includegraphics{mwe/example-image-a.pdf}
    \addcontentsline{toc}{subsection}{A}
  }
\end{center}
\begin{center}
  \parbox{\linewidth}{
    \centering
    \phantomsection
    \includegraphics{mwe/example-image-a.pdf}
    \addcontentsline{toc}{subsection}{A}
  }
\end{center}
\begin{center}
  \parbox{\linewidth}{
    \centering
    \phantomsection
    \includegraphics{mwe/example-image-a.pdf}
    \addcontentsline{toc}{subsection}{A}
  }
\end{center}
\begin{center}
  \parbox{\linewidth}{
    \centering\phantomsection
    \includegraphics{mwe/example-image-b.pdf}
    \addcontentsline{toc}{subsection}{B}
  }
\end{center}
\begin{center}
  \parbox{\linewidth}{
    \centering
    \phantomsection
    \includegraphics{mwe/example-image-a.pdf}
    \addcontentsline{toc}{subsection}{A}
  }
\end{center}
\end{document}
  • Thanks a lot! Do I need both \centering? How would \minipage be different and is it better? – Gerenuk Aug 6 '14 at 13:52
  • The outer center environment inserts additional vertical space and prevents a possible parindent. The inner \centering command centers your graphics horizontally. minipage is an Environment while \parbox is a command. The result would be the same. – esdd Aug 6 '14 at 20:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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