带有可选参数的宏似乎会将 PDF 字符串中不允许的标记注入到输出中。
下面的 MWE 应该能证明这种效果。
这个令牌到底来自哪里,它是什么样子的,我做错了什么,有没有已知的解决方法?
\documentclass[12pt]{article}
\usepackage[main=english]{babel}
\newcommand{\docTitle}{Works fine}
\renewcommand{\docTitle}[1]{Works fine, too (#1)}
%\renewcommand{\docTitle}[1][]{Does not work}%<- simplified/minimized example
\usepackage[pdfencoding=auto,
final=true,
]{hyperref}
%\usepackage{hyperxmp}
\hypersetup{%
pdfauthor = {John Doe},
pdftitle = {\docTitle{Hiho!}},
pdfsubject = {The Subtitle},
pdfkeywords = {Some keywords},
pdfdisplaydoctitle = true,
pdfstartpage = 1,
pdfstartview = FitH,
}
\setlength{\parindent}{0pt}
\begin{document}
Local tex-environment:
TexLive 2016 (latest updates, frozen), Win7-64bit, Texstudio (latest
stable release)
\bigskip
Step to reproduce:
After uncomenting line~7 the content of \textbackslash docTitle is
ignored (see screenshot of the resulting pdf's metadata).
\bigskip
The log says:
\begin{verbatim}
Package hyperref Warning: Token not allowed in a PDF string (PDFDocEncoding):
(hyperref) removing `\docTitle' on input line 21.
\end{verbatim}
\bigskip
Question[s]:
Where does this token exactly come from, how does it look like, is there
a known workaround?
\end{document}}
截屏:
答案1
由于宏具有可选参数,因此它不可扩展,因此PDF 元键中留下了\docTitle[]{}
“无用”的标记。\docTitle
一个可能的解决方案:使用\DeclareExpandableDocumentCommand
来自的宏xparse
。
\DeclareExpandableDocumentCommand{\docTitle}{O{}m}{Does not work or does it? See #1}
将定义一个可扩展的宏O
,该宏具有一个可选参数,没有默认值,因此只需使用即可{}
将其留空。这xparse
相当于\newcommand{\foo}[1][]{}
。
\DeclareExpandableDocumentCommand
定义的宏必须具有强制参数。
\documentclass[12pt]{article}
\usepackage[main=english]{babel}
\usepackage{xparse}
\newcommand{\docTitle}{Works fine}
\renewcommand{\docTitle}[1]{Works fine, too (#1)}
\DeclareExpandableDocumentCommand{\docTitle}{O{}}{Does not work or does it? See #1}%<- simplified/minimized example
\usepackage[pdfencoding=auto,
final=true,
]{hyperref}
%\usepackage{hyperxmp}
\hypersetup{%
pdfauthor={John Doe},
pdftitle={\docTitle[FooFoo]{Hiho}},
pdfsubject = {The Subtitle},
pdfkeywords = {Some keywords},
pdfdisplaydoctitle = true,
pdfstartpage = 1,
pdfstartview = FitH,
}
\setlength{\parindent}{0pt}
\begin{document}
Local tex-environment:
TexLive 2016 (latest updates, frozen), Win7-64bit, Texstudio (latest
stable release)
\bigskip
Step to reproduce:
After uncomenting line~7 the content of \textbackslash docTitle is
ignored (see screenshot of the resulting pdf's metadata).
\bigskip
The log says:
\bigskip
Question[s]:
Where does this token exactly come from, how does it look like, is there
a known workaround?
\end{document}}