[Tex/LaTex] use hypersetup in own package

hyperrefpackages

I'm trying to create my own style file (.sty-file) where more or less the whole preamble is defined. The reason behind is, that users of my stylefile just have to use my package and don't have to care about styling.

In my stylefile I create some new commmands like the following:

\newcommand{\theversion}{Version 1.0}
\newcommand{\thedocumenttitle}{My Basic Title}
\newcommand{\theauthor}{John Doe}
\newcommand{\thedate}{\today}

Users of my stylefile can now "override" these settings with renewcommand in their actual main file like so:

\renewcommand{\theversion}{Version 2.0}
\renewcommand{\thedocumenttitle}{My Overwritten Title}

Now my problem is the following: I use the hyperref package in the stylefile and also use the hypersetup command:

\hypersetup{
pdftitle={\thedocumenttitle{} - \theversion{}},
pdfauthor={\theauthor{}},
linkcolor={black},
urlcolor={black},
citecolor={black}}

Although I renew the commands in the main file, the old values are used in the hypersetup because the hypersetup is defined in the style and not in the main file. The renewing happens in the main file and therefore after the hypersetup command i guess.

So my questions are:

  1. In general: Is it a good aproach to define commands in the stylefile and let the users of the stylefile renew these commands if needed?
  2. How can I solve my hypersetup issue? Sure, I could put the hypersetup in the main file after the renewcommands. But I don't want the users of the stylefile care about such things. Is there any different (better) approach?

Best Answer

You can delay the execution of \hypersetup for the information entries. However, it must be set before hyperref uses it. Usually this time is \begin{document}, that means, \AtBeginDocument can be used before hyperref is loaded:

\AtBeginDocument{%
  \hypersetup{%
    pdftitle={\thedocumenttitle\space - \theversion},%
    pdfauthor={\theauthor},%
  }%
}
\RequirePackage{hyperref}

Remarks:

  • Do not use empty braces inside the entries (\theauthor{}). Macro \theauthor does not have an argument and the braces will remain in the entry.
Related Question