如何将完全展开的宏写入文件(使用 LaTeX 内核)

如何将完全展开的宏写入文件(使用 LaTeX 内核)

我想将宏的值写入\@authortofile文件。宏内部还有另一个宏,它被定义为一些单词之间的分隔符:

\def\@separator{\def\@separator{, }}

当我尝试这样做时,我在.dat文件中得到一个字符串:

\thispaperauthor{\@separator One\@separator Two\@separator Three}

未展开 \@separator,但我需要如下文本:

\thispaperauthor{One, Two, Three}

PDF输出同时具有所需的格式: 在此处输入图片描述

这是MWE

\documentclass[]{article}

\makeatletter
\def\@separator{\def\@separator{, }} %---def separator

\def\@authortofile{\@separator One\@separator Two\@separator Three} %def \@authortofile

%---definition of newwrite as \writedatatofile
\newwrite\titleauthorfile
\newcommand\writedatatofile[1]{%
  \immediate\openout\titleauthorfile=#1.dat
  \immediate\write\titleauthorfile{%
     \string\thispapertitle{\unexpanded\expandafter{\@title}}}%
  \immediate\write\titleauthorfile{%
     \string\thispaperauthor{\unexpanded\expandafter{\@authortofile}}}%
  \immediate\write\titleauthorfile{\string\finishauthors}
  \immediate\closeout\titleauthorfile
}
\makeatother
\title{Title}
\begin{document}
\writedatatofile{\jobname}

\makeatletter
\@authortofile
\makeatother

\end{document}

答案1

\thispapertitle{Title}
\thispaperauthor{One , Two , Three}
\finishauthors

\documentclass[]{article}

\makeatletter
\def\@separator{\def\@separator{, }} %---def separator

\def\@authortofile{\@separator One \@separator Two \@separator Three} %def \@authortofile

%---definition of newwrite as \writedatatofile
\newwrite\titleauthorfile
\newcommand\writedatatofile[1]{%
  \immediate\openout\titleauthorfile=#1.dat
  \immediate\write\titleauthorfile{%
     \string\thispapertitle{\unexpanded\expandafter{\@title}}}%
{%
\def\@separator##1\@separator##2{%
\unexpanded{##1}%
\ifx!##2%
\else
, \expandafter\@separator\expandafter##2%
\fi
}%
  \immediate\write\titleauthorfile{%
     \string\thispaperauthor{\@authortofile\@separator!}}%
}%
  \immediate\write\titleauthorfile{\string\finishauthors}%%%
  \immediate\closeout\titleauthorfile
}
\makeatother
\title{Title}
\begin{document}
\writedatatofile{\jobname}

\makeatletter
\@authortofile
\makeatother

\end{document}

相关内容