# Hyperref pdftitle manages line breaks in a strange way

I use XeLaTeX with fontspec, polyglossia, biblatex and hyperref packages (etc.) to typeset my documents as \documentclass[11pt,a4paper]{article} . The hyperref package is just awesome, however, I have found a really annoying bug.

I want the pdftitle to use a combination of some static text and a previously defined variable \thetitle. So far so good. The problem arises when this variable contains line breaks (\\). hyperref seems to simply omit them automatically but I want to be able to control this manually.

To get rid of the line breaks I use this macro \replacelinebreaks{}{}:

% A macro to remove line breaks from any text #1 and replace them
% with #2 (can be void). E.g.: \replacelinebreaks{\thetitle}{\ }.
\newcommand{\replacelinebreaks}[2]{%
\begingroup\def\\{#2}#1\endgroup}


(The credit for it goes mostly to Easy way to remove formatting (e.g. linebreaks).) This macro works fine everywhere throughout the document - except in \hypersetup{pdftitle= ...}.

What am I missing here? There are quite a lot of very similar questions on this site, but none seems to solve my particular problem:

Here is my MWE:

% !BIB TS-program = biber
% !TeX program = xelatex
% !TeX encoding = UTF-8
% !TeX spellcheck = en_GB

\documentclass[11pt,a4paper]{article}
\usepackage{polyglossia}
\usepackage{hyperref}

\newcommand{\replacelinebreaks}[2]{\begingroup\def\\{#2}#1\endgroup}

\def\thetitle{Type the\\Minimum Working Example\\Title Here\\}
\def\firstauthor{Abra}      % Only the first author
\def\theauthor{\firstauthor %  Add all other authors (no spaces! Use "\\" and "%")
\\Ca%
\\Dabra%
}

\begin{document}
\hypersetup{
pdftitle    = MWE No.1 \space -- \replacelinebreaks{\thetitle}{\ },
pdfauthor   = \replacelinebreaks{\theauthor}{; },
}

\begin{center}
{\Huge\thetitle}
\end{center}
\end{document}


It results in:

Please help me understand and correct this behaviour, so I could either use my \replacelinebreaks macro or achieve my goal in some other way. Your help is much appreciated.

• Why don't you provide some code for tests? Feb 3, 2017 at 12:06
• @Ulrike Fischer: MWE is now provided. I am running some tests for your proposed solution. Feb 3, 2017 at 13:06

You always can redefine the way hyperref handles such tokens:

\documentclass{article}
\usepackage{hyperref}

\newcommand\myvar{abc\\cde}

\pdfstringdefDisableCommands{\def\\{XXX}}
\hypersetup{pdftitle= abc\\ cde\myvar}

\begin{document}
blub

\end{document}


Or you can define your variable so that it uses in sensible places \texorpdfstring:

\documentclass{article}
\usepackage{hyperref}

\newcommand\myvar{abc\texorpdfstring{\\}{XXX}cde}

\hypersetup{pdftitle= abcXXXcde\myvar}

\begin{document}
\myvar

\end{document}

• Do these commands affect only hyperref or the whole document? In my case, the \pdfstringdefDisableCommands{\def\\{ }}works fine for \thetitle, but not for \theauthor, where line breaks have to be replaced not with space, but with "; ". It seems that I cannot have two such exceptions at once ... Feb 3, 2017 at 13:37
• You can add another set: \pdfstringdefDisableCommands{\def\\{YYY}}\hypersetup{pdfauthor= abc\\ cde\myvar} and later set the definition to someelse, e.g. for the bookmarks. Feb 3, 2017 at 13:57