[Tex/LaTex] poisonous interaction between memoir, hyperref, and hyperxmp

copyrighthyperrefincompatibilitymemoirxetex

In another, now rather confused, question, I'm struggling to get copyright information into a PDF produced by memoir.

The investigations so far have led to this problematical MWE:

% !TEX TS-program = XeLaTeX
% !TEX encoding = UTF-8 Unicode
\documentclass{article}
\usepackage{lipsum}
\usepackage{hyperref}
\usepackage{hyperxmp}
\hypersetup{
    pdfauthor={Brent Longborough},
    pdftitle={O Hai},
    pdfcopyright={Copyright © 2012 by Brent Longborough. All rights reserved.},
    }
\begin{document}
\lipsum[1]
\end{document}

which produces a PDF with the copyright notice successfully included in the metadata, and with the "PDF Producer" set to "XeTeX 0.9997"

However, if I change the document class to:

\documentclass{memoir}

then I get no copyright notice, and the PDF Producer is now set to "xdvipdfmx (0.7.8)".

After an hour or so ploughing through memoir.sty and memhfixc.sty, I'm flummoxed. Does anyone have any idea where to look next?

Best Answer

This is actually a timing problem: The metadata has to be inserted before the final \clearpage command, otherwise, it isn't written to the PDF document any more. However, memoir issues a \clearpage with

\AtBeginDocument{\AtEndDocument{\clearpage\dol@stsheet\dol@stpage}}

so the code used by hyperxmp

\AtBeginDocument{%
   % [...]
      \AtEndDocument{%
        \hyxmp@find@metadata
        \hyxmp@embed@packet
      }%
   % [...]
}

is too late as \hyxmp@find@metadata and \hyxmp@embed@packet are executed after \clearpage.

To solve this, you can manually issue

\makeatletter
  \AtEndDocument{\hyxmp@find@metadata\hyxmp@embed@packet}
\makeatother

in your document's preamble.

Minimal test code:

% !TEX TS-program = XeLaTeX
% !TEX encoding = UTF-8 Unicode
\documentclass{article}
\usepackage{lipsum}
\usepackage{hyperref}
\usepackage{hyperxmp}
\makeatletter
  \AtEndDocument{\hyxmp@find@metadata\hyxmp@embed@packet}
\makeatother
\hypersetup{
    pdfauthor={Brent Longborough},
    pdftitle={O Hai},
    pdfcopyright=Copyright © 2012 by Brent Longborough. All rights reserved.}
\begin{document}
\lipsum[1]
\end{document}
Related Question