我想将“\cite{xyz}”的结果写入文件。以下 MWE 给我
Forbidden control sequence found while scanning text of \write.
所以我不知道该怎么做。
\documentclass{article}
\begin{document}
\cite{Ab,Cd}
\newwrite\tempfile
\immediate\openout\tempfile=tmpfil.txt
\immediate\write\tempfile{2015} % this works fine
\immediate\write\tempfile{\cite{Cd}}
\immediate\closeout\tempfile
\begin{thebibliography}{1}
\bibitem{Ab} Author, title, 2016.
\bibitem{Cd} Common, Second, 2015.
\end{thebibliography}
\end{document}
答案1
您不能使用,\cite
因为这有几个用于打印引用键的指令。
您需要使用\b@<key>
,例如像这样:
\documentclass{article}
\newwrite\tempfile
\AtBeginDocument{\immediate\openout\tempfile=\jobname.txt}
\makeatletter
\newcommand{\writecite}[1]{%
\@for\next:=#1\do{%
\immediate\write\tempfile{%
\@ifundefined{b@\next}{[<\next>]}{[\csname b@\next\endcsname]}%
}%
}%
}
\makeatother
\begin{document}
\cite{Ab,Cd}
\writecite{Ab,Cd}
\begin{thebibliography}{1}
\bibitem{Ab} Author, title, 2016.
\bibitem{Cd} Common, Second, 2015.
\end{thebibliography}
\end{document}
首次运行后的内容
[<Ab>]
[<Cd>]
第二次运行后的内容
[1]
[2]
答案2
您可以使用\unexpanded
:
\documentclass{article}
\begin{document}
\cite{Ab,Cd}
\newwrite\tempfile
\immediate\openout\tempfile=tmpfil.txt
\immediate\write\tempfile{2015} % this works fine
\immediate\write\tempfile{\unexpanded{\cite{Cd}}}
\immediate\closeout\tempfile
\begin{thebibliography}{1}
\bibitem{Ab} Author, title, 2016.
\bibitem{Cd} Common, Second, 2015.
\end{thebibliography}
\end{document}