# attachfile does not work inside captions

In the caption of some figures I want to link to files using the attachfile2 package. But when I try to compile the following example with XeTeX, MiKTeX and TeXstudio I get the warning ** WARNING ** Unresolved object reference "atfi_obj_6" found!!! for the first one and some.pdf isn't included. another.pdf is included in the resulting document, but twice.

Does anyone have an idea how to fix this?

\documentclass{article}
\usepackage{caption}
\usepackage{attachfile2}
\usepackage{hyperref}

\begin{document}
\listoffigures
\begin{figure}
\end{figure}

\begin{figure}
\end{figure}
\end{document}


The same problem appears for both figures if there is no \listoffigures at the beginning.

EDIT I forgot to mention that I had a look at the *.atfi which contains checksums of the attached files or their paths as far as I understand. The checksums when using attachfile inside a caption (doesn't work) and when using outside a caption (works fine) are the same, so maybe there is some kind of timing problem?

EDIT 2

Based on Herberts answer I made up an command that leads to captions which look more like the default ones and entries in the list of figures that look like the default ones.

\documentclass{article}
\usepackage{caption}
\usepackage{attachfile2}

\def\mycaption#1#2#3{{\par\centering\refstepcounter{figure}%
\figurename\ \thefigure: #1 \textattachfile{#2}{#3}\par}%

\begin{document}
\listoffigures

\begin{figure}
\end{figure}
\begin{figure}
\caption[Some Figure]{Some Figure}
\end{figure}

\end{document}


This results in the following entries in the *.lof file, so changes to the style of the list of figures should also apply to the entry produced by this command (I think so because they nearly look the same):

\contentsline {figure}{\numberline {1}{\ignorespaces Some Figure}}{1}{figure.1}
\contentsline {figure}{\numberline {2}{\ignorespaces Some Figure}}{1}{figure.caption.3}


The downside of this solution is that I have to define my custom caption style in two places (I didn't mention that because it wasn't necessary to reproduce my problem). So I would appreciate a solution which uses the default \caption-command.

But for now this one does the job.

• It works fine with pdflatex, with xelatex I noticed also that the red box around the link in the list of figures is also reaching down to the bottom of the document. There is also another attachment (HTML?) which is not present if I compile using pdflatex. – Uwe Ziegenhagen Aug 18 '14 at 5:41
• It works fine for me with pdflatex, too. I think the other attachment is the link to the file which couldn't be resolved. If you comment out the \listoffigures there will be two such attachments. – kriim Aug 18 '14 at 8:53

use always the optional argument when attaching a file:

\PassOptionsToPackage{colorlinks}{hyperref}
\documentclass{article}
\usepackage{caption}
\usepackage{attachfile2}

\begin{document}
\listoffigures
\begin{figure}
\end{figure}

\begin{figure}
\end{figure}

\end{document}


As an alternative create your own list of figures:

\documentclass{article}
\usepackage[color={1 0 0}]{attachfile2}
\usepackage{caption}
\def\mycaption#1#2#3{{\par\centering\refstepcounter{figure}%
\label{fig-\thefigure}\figurename: \thefigure\ #1 \textattachfile{#2}{#3}\par}%
\figurename:\ \thefigure\ #1\hfill\pageref{fig-\thefigure}\par}}

\begin{document}
\listoffigures
\begin{figure}
\end{figure}
\clearpage
\begin{figure}
\end{figure}

\end{document}

• I wanted to do so, but there won't be any files attached in this case. The "Another Figure" example should show, that there will be attachments if the link appears in the list of figures. – kriim Aug 18 '14 at 10:05
• sorry, I meant the other way round: optional argument with \textattachfile. See edited answer. – user2478 Aug 18 '14 at 10:11
• This one worked for me, but I don't want the links to appear in the list of figures. Instead they should appear in the caption only. For some reason attachfile can handle the reference if it's inside the *.lof file. I also tried using fully qualified paths, but that leads to the same behaviour. – kriim Aug 18 '14 at 10:23
• see edited answer. It is only an idea how it can be done – user2478 Aug 18 '14 at 11:01
• Thank you for your great hint. I adapted the command to fix some appearance issues. Do you have an idea how to change the command to add an option for a custom list entry (like the one from the \caption-command)? I read about default values once, but never got it working. Mostly I'm only able to collect code snippets and adapt them to my needs because I'm new to latex ;) – kriim Aug 18 '14 at 12:58

As far as I can read in the documentaton of packages attachfile and attachfile2, they work only when the typesetting engine is pdftex; XeLaTeX does not use that typesetting engine and does not natively produce pdf files; it rather produces and extended 9to UNICODE) version of a DVI file, and when you run it it automatically calls an extended version of the dvitopdfm program that converts dvi files to pdf. Dvi files do not support annotations, so it is a pure mireclae che runnining yor test file with XeLaTeX does not produce more serious errors. May be it can wiork with LuaLaTeX, since the underlying typesetting engine luatex is an extesion of an advanced version of pdftex.

• Have a look at the attachfile2 documentation at section 1.3 and 2.2.3, the developer added support for xetex. It works like a charm except for the behaviour I descriped in my question. – kriim Aug 18 '14 at 9:07

I wanted to let you know about my final solution. As it uses another approach than before, I didn't edit the question.

Now I'm using the package marginnote which makes one able to define the equivalent to \marginpar inside of floats etc. In my opinion this one looks also better than having the link in the caption.

\documentclass{article}
\usepackage[color={0 0 0}]{attachfile2} % use black as link color
\usepackage[demo]{graphicx} % show placeholder
\usepackage{marginnote} % like \marginpar but inside of floats etc.

\begin{document}
\begin{figure}

By the way, you can use a Unicode paperclip as link text to get a much nicer paperclip than the default one: \symbol{"1F4CE} -> 📎. In this case you must use xetex and a font that supports this symbol, at least for the text of the link.