4

I try to add a variable title string to my own package 'MyStyle.sty'.

'MyStyle.sty' just contains some used packages, pdf settings and commands for a style guide / corporate identity.

Document.tex

\documentclass[fontsize=12pt,paper=a4,headings=normal,titlepage=true]{scrbook}
\usepackage[tileString=My title]{MyStyle}
...
\begin{document}
...
\end{document}

MyStyle.sty

\usepackage{scrhack}
...
\hypersetup{ pdftitle={\titleString} }
...
\endinput

How to add this titleString variable to my package? I need the solution to work with non-ASCII characters, like ä, ü, ö as well. I found something like \DeclareOptions but can't use it.

6

LaTeX's \DeclareOption doesn't work with key-value pairs. That is, options like 10pt, 11pt, and 12pt are allowed, but an option fontsize=<10 or 11 or 12> wouldn't work.

There are a few packages that implement this key-value scheme. Here's a solution using kvoptions:

MyStyle.sty

\RequirePackage{scrhack}

\RequirePackage{kvoptions}% load kvoptions

\SetupKeyvalOptions{%
  family=mysty@,% define the family and prefix of the options
  prefix=mysty@}

\DeclareStringOption{titleString}% declare a key-value option 'titleString'

% make kvoptions read the options passed to the package and evaluate them.
\ProcessKeyvalOptions{mysty@}

% you need hyperref to use hypersetup
\RequirePackage{hyperref}

% the value given to the option is stored in \mysty@titleString
\hypersetup{ pdftitle={\mysty@titleString} }

\endinput

document.tex

\documentclass[fontsize=12pt,paper=a4,headings=normal,titlepage=true]{scrbook}

% you need braces to preserve spaces
\usepackage[titleString={My title}]{MyStyle}

\begin{document}

hello

\end{document}

Update:

Using options with non-ASCII characters

When you try to use options with non-ASCII characters you'll get a lot of warnings from hyperref saying:

Package hyperref Warning: Token not allowed in a PDF string (PDFDocEncoding):
(hyperref)                removing `<something>' on input line <somewhere>.

then where you had the non-ASCII letters you'll have some numbers.

As explained by Heiko Oberdiek here, this happens when the \usepackage command passes the options to the package.

Quoting Oberdiek:

The 8-bit bytes of the input file have category code 12 (other) like digits. \usepackage[utf8]{inputenc} makes these bytes active. The active character bytes then decode the bytes to get the macros for the Unicode character representation.

LaTeX expands the options the hard way (\edef) assuming options are only simple ASCII letters and digits.

This forced expansion of the active Unicode characters generates the sequence of commands that hyperref rejects when adding the metadata to the pdf.

Package kvoptions-patch patches the LaTeX option parsing to support arbitrary macros in the values of key value settings without expanding them.

So you have to load kvoptions-patch prior to calling your MyStyle package:

\documentclass[fontsize=12pt,paper=a4,headings=normal,titlepage=true]{scrbook}
% This is necessary for unicode characters in pdfTeX
\usepackage[utf8]{inputenc}
% This patches LaTeX's package loading to allow unicode strings.
\usepackage{kvoptions-patch}

% you need braces to preserve spaces
\usepackage[titleString={größenordnungsmäßig}]{MyStyle}

\begin{document}

hello

\end{document}

Of course, this is needed only if you use pdfTeX. XeTeX and LuaTeX have unicode support by default and don't require this workaround.

|improve this answer|||||
  • Perfect solution for me! +1 But is there a possibility to add german mutated vowels like ä, ü, ö in my title string? I always get a 'Token not allowed in PDF string' warning – PascalS Jul 1 '18 at 15:09
  • @Passe Hmmm... Something wrong is not right here. When the diacritics are passed to the package something gets messed up. I'm trying to figure out where and why this is happening. As soon as I find out I'll ping you here :) – Phelype Oleinik Jul 1 '18 at 15:54
  • 1
    @Passe Done :) You have to load \usepackage[utf8]{inputenc} (which I believe you already have because of the warnings you get) and \usepackage{kvoptions-patch} to allow the unicode strings in the options. – Phelype Oleinik Jul 1 '18 at 16:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.