# Conflict between hyperref and attachfile2 package

I have a problem with creating pdf document that contains an attachment. This code works fine:

% <xetex — Unicode-based TeX engine>
\documentclass[12pt, a4paper, czech, twoside]{book}
\usepackage{hyperref}
\usepackage{attachfile2}
\begin{document}
My attached file is \attachfile{my.m}
\end{document}


but this code

\documentclass[12pt, a4paper, czech, twoside]{book}
\usepackage[xetex,
bookmarks={true},    %  A set of Acrobat bookmarks are written
anchorcolor={black}, %  Color for anchor text.
filecolor={cyan},    %  Color for URLs which open local files.
runcolor={blue},     %  Color for run links (launch annotations).
urlcolor={magenta},  %  Color for linked URLs.
unicode={true},
pdfauthor={Jaroslav Fait},
pdftitle={Wiking},
pdfsubject={study notes},
pdfkeywords={linear algebra, math, electronics},
pdfproducer={XeLateX with hyperref},
pdfcreator={Xelatex}]{hyperref}

\usepackage{attachfile2}
\begin{document}
My attached file is \attachfile{my.m}
\end{document}


no!!!. Notes (my.m is Matlab m-file).

The generated pdf document contain an attachment but could not be opened or saved, and the file name of the attachment is corrupted. The result of my investigation is such that the hyperref package shall not have any options. I use Miktex 2.9 (xelatex engine, last update) and Adobe reader X.

• Welcome to TeX.SX! Have you tried loading the packages in reverse order? – bodo Aug 3 '12 at 13:31

It works for me by using \hypersetup instead of directly supplying the package options:

\documentclass[12pt, a4paper, czech, twoside]{book}
\usepackage[xetex,
bookmarks={true},    %  A set of Acrobat bookmarks are written
anchorcolor={black}, %  Color for anchor text.
filecolor={cyan},    %  Color for URLs which open local files.
runcolor={blue},     %  Color for run links (launch annotations).
urlcolor={magenta},  %  Color for linked URLs.
]{hyperref}
\hypersetup{pdfauthor={Jaroslav Fait},
pdftitle={Wiking},
pdfsubject={study notes},
pdfkeywords={linear algebra, math, electronics},
pdfproducer={XeLateX with hyperref},
pdfcreator={Xelatex}}
\usepackage{attachfile2}
\begin{document}
My attached file is \attachfile{my.m}
\end{document}


(By the way, the pdfproducer option won't work as xdvipdfmx, the program used by XeTeX to produce the PDF, always overwrites it with it own entry).

Edit: Some more investigation reveals that the problem is induced by using the hyperref options unicode=true/pdfencoding=unicode, which is activated automatically if you use pdfauthor and friends as an argument when loading the package. So the solution is to not use one of the options unicode or pdfencoding and to set pdfauthor etc. with \hypersetup, which is recommended practice anyway.

• attachfile2 implicitly loads hyperref (due to the need for \pdfstringdef), so I think you can simply use hypersetup. Also, it is worth mentioning that some options need to be passed to hyperref directly at load time. I always get burned on the hyperfootnotes one, e.g. – jon Aug 3 '12 at 17:10
• @jon Good point! I've had another look into the code, and it seems that the only problematic options are unicode=true/pdfencoding=unicode, and pdfauthor and friends (which set these two automatically if used at load time). So one can use options like pdfa, which have to be present when loading the package, without harm, and only supply the document information with \hypersetup. – diabonas Aug 4 '12 at 10:57
• Thank you very much to all for the help. The solutions works, and in addition, I learned something new. :-) – JardaFait Aug 6 '12 at 19:54