我有一个宏\writetitletofile
,可以将文章标题写入文件.dat
。我怎样才能对文章的作者执行相同的操作。需要注意的是,作者列表是使用authblk
包创建的。
\documentclass{scrartcl}
\usepackage{fontspec, polyglossia}
\setmainlanguage{english}
\setmainfont{CMU Serif}
\setsansfont{CMU Sans Serif}
\setmonofont{CMU Typewriter Text}
\usepackage{authblk}
\author[1]{First Author}
\affil[1]{Affiliation of the 1st author}
\author[2]{Second Author}
\affil[2]{Affiliation of the 2nd author}
\title{Title of the article}
\makeatletter
\def\writetitletofile{%
\newwrite\file
\immediate\openout\file=titleauthors.dat
\immediate\write\file{\@title}%
\immediate\closeout\file%
}%
\makeatother
\begin{document}
\writetitletofile
\end{document}
答案1
我们可以利用以明确定义的格式保存作者和所属机构列表的事实authblk
:在示例中,宏\AB@authors
扩展为
\protect \Authfont \protect \AB@setsep First Author\protect \\[\affilsep ]%
\protect \Affilfont Affiliation of the 1st author\protect \and \protect
\Authfont \protect \AB@setsep Second Author\protect \\[\affilsep ]%
\protect \Affilfont Affiliation of the 2nd author.
(已重新格式化,以便更加清晰)。
\AB@setsep
因此我们可以使用递归宏来提取和之间的数据\protect
。
\documentclass{scrartcl}
\usepackage{fontspec, polyglossia}
\usepackage{authblk}
\setmainlanguage{english}
\setmainfont{CMU Serif}
\setsansfont{CMU Sans Serif}
\setmonofont{CMU Typewriter Text}
\makeatletter
\newwrite\titleauthorfile
\newcommand\writedatatofile{%
\immediate\openout\titleauthorfile=\jobname-titleauthors.dat
\immediate\write\titleauthorfile{\@title}%
\processauthors
\immediate\closeout\titleauthorfile
}
\newcommand\processauthors{%
\expandafter\process@authors\AB@authors\AB@setsep\protect\@nil
}
\def\process@authors#1\AB@setsep#2\protect#3\@nil{%
\ifx\relax#2\relax
\expandafter\@gobble
\else
\immediate\write\titleauthorfile{\unexpanded{#2}}%
\expandafter\@firstofone
\fi
{\process@authors#3\@nil}%
}
\makeatother
\author[1]{First Author}
\affil[1]{Affiliation of the 1st author}
\author[2]{Second Author}
\affil[2]{Affiliation of the 2nd author}
\title{Title of the article}
\begin{document}
\writedatatofile
\end{document}
该文件的内容\jobname-titleauthors.dat
(\jobname
是主文件的名称,最好使用依赖于它的名称)将是
Title of the article
First Author
Second Author
为了连续读取创建的文件,最好添加一些结构:
\documentclass{scrartcl}
\usepackage{fontspec, polyglossia}
\usepackage{authblk}
\setmainlanguage{english}
\setmainfont{CMU Serif}
\setsansfont{CMU Sans Serif}
\setmonofont{CMU Typewriter Text}
\makeatletter
\newwrite\titleauthorfile
\newcommand\writedatatofile{%
\immediate\openout\titleauthorfile=\jobname-titleauthors.dat
\immediate\write\titleauthorfile{%
\string\thispapertitle{\unexpanded\expandafter{\@title}}}%
\processauthors
\immediate\write\titleauthorfile{\string\finishauthors}
\immediate\closeout\titleauthorfile
}
\newcommand\processauthors{%
\expandafter\process@authors\AB@authors\AB@setsep\protect\@nil
}
\def\process@authors#1\AB@setsep#2\protect#3\@nil{%
\ifx\relax#2\relax
\expandafter\@gobble
\else
\immediate\write\titleauthorfile{%
\string\thispaperauthor{\unexpanded{#2}}%
}
\expandafter\@firstofone
\fi
{\process@authors#3\@nil}%
}
\makeatother
\author[1]{First Author}
\affil[1]{Affiliation of the 1st author}
\author[2]{Second Author}
\affil[2]{Affiliation of the 2nd author}
\title{Title of the article}
\begin{document}
\writedatatofile
\end{document}
现在输出文件将是
\thispapertitle{Title of the article}
\thispaperauthor{First Author}
\thispaperauthor{Second Author}
\finishauthors
在对和\input
给出合理的定义后,您就可以得到这个文件。\thispapertitle
\thispaperauthor