Hyperref pdftitle 以奇怪的方式管理換行符

Hyperref pdftitle 以奇怪的方式管理換行符

我使用XeLaTeXwith fontspecpolyglossiabiblatexpackages hyperref(等)將我的文件排版為\documentclass[11pt,a4paper]{article}.這個hyperref包非常棒,但是,我發現了一個非常煩人的錯誤。

我希望pdftitle使用一些靜態文字和先前定義的變數的組合\thetitle。到目前為止,一切都很好。當變數包含換行符號 ( \\) 時,就會出現問題。hyperref似乎只是自動忽略它們,但我希望能夠手動控制它們。

為了擺脫換行符,我使用這個巨集\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}

(這主要歸功於刪除格式的簡單方法(例如換行符號).) 這個巨集在整個文件中的任何地方都可以正常工作 - 除了\hypersetup{pdftitle= ...}.

我在這裡缺少什麼?網站上有很多非常相似的問題,但似乎沒有一個能解決我的特定問題:

這是我的 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}

結果是:

在此輸入影像描述

請幫助我理解並糾正這種行為,以便我可以使用我的\replacelinebreaks巨集或以其他方式實現我的目標。非常感謝您的幫忙。

答案1

您始終可以重新定義 hyperref 處理此類標記的方式:

\documentclass{article}
\usepackage{hyperref}

\newcommand\myvar{abc\\cde}

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

\begin{document}
blub

\end{document}

在此輸入影像描述

或者您可以定義變量,以便它在合理的地方使用\texorpdfstring

\documentclass{article}
\usepackage{hyperref}

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

\hypersetup{pdftitle= abcXXXcde\myvar}

\begin{document}
\myvar

\end{document}

相關內容