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:
- Using non ASCII characters in author names and titles within hyperref pdfinfo, but I already use
\hypersetup- I also tried creating a new variable without line breaks, but the error just seems to propagate throughout.
- Using a code for special characters (e.g.
\040should be a space) does not produce any difference.
- hyperref: pdftitle and wrong character encoding, same argument.
- pdfinfo doesn't appear to be working, same argument again.
- hyperref : break long lines in pdftitle and other fields.
\textLFand\textCRdo not solve my problem. Foxit Reader does not recognize those characters while Adobe Acrobat and Acrobat Reader display only the first line. My issue lies withhyperref. - Non-ascii characters are not displayed in hyperref's pdftitle: bug in the 'unicode' option?. In my case,
\usepackage[pdfencoding=unicode]{hyperref}does not change anything. - Make hyperref take pdfinfo from \title and \author. Admittedly, if I use
\usepackage[pdfusetitle]{hyperref}the\hypersetupis obsolete and the line breaks are automatically converted to space (for pdftitle) and to comma (for pdfauthor). However, as noted before, I want a combination of static text and\thetitle, so\hypersetupis unavoidable.- The
\@titleapproach would not help as it is already used to define\thetitlein the first place.
- The
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.

