7

I would like to write the result of "\cite{xyz}" to a file. The following MWE gives me

Forbidden control sequence found while scanning text of \write.

So I do not know how to do it.

\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}
4

You can't use \cite because this has several instruction for printing the citation key.

You need to use \b@<key>, for instance like this:

\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}

Contents after the first run

[<Ab>]
[<Cd>]

Contents after the second run

[1]
[2]
1

You can use \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}
  • Sorry I did not explain enough. The solution writes "\cite{Cd}" in the file. I want "[2]" in the file instead. – user117994 Nov 5 '16 at 13:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.