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.


\usepackage[tileString=My title]{MyStyle}


\hypersetup{ pdftitle={\titleString} }

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.

1 Answer 1


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:



\RequirePackage{kvoptions}% load kvoptions

  family=mysty@,% define the family and prefix of the options

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

% make kvoptions read the options passed to the package and evaluate them.

% you need hyperref to use hypersetup

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




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





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:

% This is necessary for unicode characters in pdfTeX
% This patches LaTeX's package loading to allow unicode strings.

% you need braces to preserve spaces




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

  • 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
    Commented Jul 1, 2018 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 :) Commented Jul 1, 2018 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. Commented Jul 1, 2018 at 16:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .