我正在尝试手动处理作者列表。该逻辑在文档中有效,但当我将其传递给 hypersetup 时似乎不起作用。
\documentclass{article}
\usepackage{authblk}
\usepackage{etoolbox}
\usepackage{hyperref}
%%%%%%%%%%%%%% Logic to process author list, e.g., part of some package %%%%%%%%%%%%%
%Define lists
\newcommand{\authlist}{}
\newcounter{nMYauthors}
\newcommand\MYauthor[2][]{\author[#1]{#2}\listadd{\authlist}{#2}\stepcounter{nMYauthors}}
%Define processing of list
\newcommand{\bodyforloop}[1]{\stepcounter{iMYauthor}\ifnumequal{\value{iMYauthor}}{1}{}{\ifnumequal{\value{nMYauthors}}{\value{iMYauthor}}{, and }{, }}#1}
\newcounter{iMYauthor}
\newcommand{\displayAuthorList}{\setcounter{iMYauthor}{0}\forlistloop{\bodyforloop}{\authlist}}
%%%%%%%%%%%%%%%% Define authors in preamble %%%%%%%%%%%%%
\MYauthor[*]{Ms. F. First}
\MYauthor[*]{Mr. S. Second}
\affil[*]{\LaTeX amateurs}
%This generates errors
\hypersetup{pdfauthor = {\displayAuthorList}}
\begin{document}
The new author list works in the document: \displayAuthorList
\end{document}
我收到的错误是:将命令传递给 hypersetup 时缺少 endcsname
有人对如何解决这个问题有什么想法吗?
答案1
您需要使用可扩展的命令。
对于此应用程序来说,重新定义是有意义的\author
。
我设置了一个(全局)列表管理,可以重复用于其他目的。该\delivergloballist
命令将列表名称、仅两个元素之间的分隔符、两个以上元素之间的分隔符以及最后两个元素的分隔符作为参数。
\documentclass{article}
\usepackage{authblk}
\usepackage{hyperref}
% Manage lists
\ExplSyntaxOn
\NewDocumentCommand{\definegloballist}{m}
{
\seq_new:c { g_dikdirk_list_#1_seq }
}
\NewDocumentCommand{\addtogloballist}{mm}
{
\seq_gput_right:cn { g_dikdirk_list_#1_seq } { #2 }
}
\NewExpandableDocumentCommand{\deliverlist}{mmmm}
{
\seq_use:cnnn { g_dikdirk_list_#1_seq } { #2 } { #3 } { #4 }
}
\ExplSyntaxOff
\definegloballist{authors}
\NewCommandCopy\authblkauthor\author
\RenewDocumentCommand{\author}{om}{%
\IfNoValueTF{#1}{\authblkauthor{#2}}{\authblkauthor[#1]{#2}}%
\addtogloballist{authors}{#2}%
}
%Define processing of list
\newcommand{\displayAuthorList}{%
\deliverlist{authors}{ and }{, }{, and }%
}
%%%%%%%%%%%%%%%% Define authors in preamble %%%%%%%%%%%%%
\author[*]{Mrs. F. First}
\author[*]{Mr. S. Second}
\affil[*]{\LaTeX amateurs}
\hypersetup{pdfauthor = {\displayAuthorList}}
\begin{document}
\title{Example paper}
\maketitle
The new author list works also in the document:
\displayAuthorList
\end{document}
的输出pdfinfo
是
Title:
Subject:
Keywords:
Author: Mrs. F. First and Mr. S. Second
Creator: LaTeX with hyperref
Producer: pdfTeX-1.40.25
CreationDate: Thu May 4 16:05:47 2023 CEST
ModDate: Thu May 4 16:05:47 2023 CEST
Custom Metadata: yes
Metadata Stream: no
Tagged: no
UserProperties: no
Suspects: no
Form: none
JavaScript: no
Pages: 1
Encrypted: no
Page size: 612 x 792 pts (letter)
Page rot: 0
File size: 46742 bytes
Optimized: no
PDF version: 1.5