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}

2 Answers 2

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .