2

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:

enter image description here

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.

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

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}

enter image description here

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 ... – Vid Merljak Feb 3 '17 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. – Ulrike Fischer Feb 3 '17 at 13:57

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.