# How to outsource \hypersetup to an external file?

I use the hyperref package and defined custom settings as follows:

\usepackage{color}
\usepackage[dvipsnames]{xcolor}
\usepackage{url}
\usepackage{hyperref}

\hypersetup{
bookmarks=true,         % show bookmarks bar?
unicode=false,          % non-Latin characters in Acrobat’s bookmarks
pdftoolbar=true,        % show Acrobat’s toolbar?
pdffitwindow=false,     % window fit to page when opened
pdfstartview={FitH},    % fits the width of the page to the window
pdftitle={My title},    % title
pdfauthor={Author},     % author
pdfsubject={Subject},   % subject of the document
pdfcreator={Creator},   % creator of the document
pdfproducer={Producer}, % producer of the document
pdfkeywords={keyword1} {key2} {key3}, % list of keywords
pdfnewwindow=true,      % links in new window
citecolor=green,        % color of links to bibliography
filecolor=magenta,      % color of file links
urlcolor=OliveGreen     % color of external links
}


I saw here that I can somehow put these settings into an external file to slim down the preamble. I created a hyperref.sty and moved the \hypersetup{} part in there. I noticed that it is considered in the compilation process, however, it fails.

Question:

What is the proper way to externalize the options for the hyperref package?

-
If you load the package xcolor there is no need to load color. You can externalize the setup in an extra file which can be include by input –  Marco Daniel Sep 7 '12 at 17:32

just place the hypersetup command into myfile.tex then

\input{myfile}


don't create a file called hyperref.sty or you will prevent the original loading.

-

## hyperref.cfg

If the file hyperref.cfg exists, then package hyperref loads this file right before it processes its options. Therefore you can put many option settings via \hypersetup in this file. Options for PDF informations should be loaded after the package hyperref is loaded. In hyperref.cfg this can be done via:

\AtEndOfPackage{%
\hypersetup{pdfauthor={me}, ...}%
}


An extended example for hyperref.cfg:

\hypersetup{%
bookmarks,%
pdfencoding=auto,%
...
}%
\AtEndOfPackage{%
\hypersetup{%
pdfauthor={...},%
...
}%
\RequirePackage{bookmark}%
}%


## myhyperref.cfg

In your original approach you cannot use hyperref.sty as file name, because this name is already used by the package itself. But via \input a different file name can be loaded:

\input{myhyperref.cfg}


But after hyperref is loaded, some options cannot be changed anymore.

## Package myhyperref

A third approach is to write a small package:

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{myhyperref}[2012/09/07 v1.0]

\RequirePackage[dvipsnames]{xcolor}
\RequirePackage[
bookmarks,
pdfencoding=auto,
]{hyperref}
\RequirePackage{bookmark}
\bookmarksetup{
numbered,
open,
}
\hypersetup{
% ...
pdfauthor={me},
% ...
}

\endinput


Then the package is loaded via \usepackage in the main document:

\documentclass{...}
...
\usepackage{myhyperref}
...
\begin{document}
...
\end{document}

-
How do I know which options have to be loaded before or after the package hyperref is loaded? –  JJD Sep 7 '12 at 19:29
hyperref warns if an option is used too late. –  Heiko Oberdiek Sep 7 '12 at 19:33
When I use your myhyperref example I end up with this error: Option clash for package hyperref. at line 10. I load the package as the last one. –  JJD Sep 7 '12 at 19:43