# Biblatex: Writing the output of \thefield{...} to a file

1. Short question: in Biblatex, how do I print the (string) field contents to a file? When processing an entry I would like to do something like this:

\write\file{\thefield{userc}}% the file contents is wrong (unexpanded macros here)

I would like to write to the file the final text, as printed out by \typeout{}:

\typeout{\thefield{userc}}% outputs "REF1,REF2", etc - I do see the text I need

For my entries the field "userc" contains comma separated lists of type "REF1,REF2,...". The result of \write is wrong and I am at a loss how to get the macro properly expanded.

I would like to have "REF1 REF2" in the file to generate there commands like \addtocategory{REF1}{KEY}, \addtocategory{REF2}{KEY}) and input the file later where needed.

1. Explanation of what and why is being tried. I am trying to produce the list of citations to my works. Currently I have all the citing bibliography entries equipped with the fields
@<entrytype>{KEY,
RELATED={REF1,REF2},
RELATEDTYPE={set}
}


where REF1,REF2 are the keys of the items which are cited in the work KEY. This works after adding a suitable macro

\newbibmacro{related:set}[1]{%
%...
\typeout{\thefield{userc}}% outputs "REF1, REF2", etc - OK
\write\f{\thefield{userc}}% fails
}


(the idea found online) and produces the list of bibliography items followed by items citing them. But now I need to organize this in the opposite way (list of works followed by their citations) and keep, if possible, the current structure of the BiB file. I am no expert; any help is welcome.

• Welcome to TeX.SX! It would be good if you provided a compilable example that we can just copy-paste. But try \protected@write instead of \write (just guessing). Jul 13 '20 at 13:38
• Many thanks Pheylipe, \protected@write worked well. Curiously enough, \immediate\write did not (this is what I tried from the beginning; maybe done something wrong). Jul 14 '20 at 21:14
• My guess for \protected@write is because you said \typeout works, and \typeout is a \write with (a kind of) protection, so \protected@write should work. But I don't really know BibLaTeX, so I don't what \thefield does. If moewe's answer solved your problem, please mark his answer as accepted Jul 14 '20 at 21:20

## \immediate\write

One option to write \thefield is to wrote \immediately. If you say \write, the argument will be written out at a slightly later point when the page is shipped out and not at the point you say \write (which is extremely convenient if you need to get page references right, see https://tex.stackexchange.com/a/103944/35864). Unfortunately, \thefield won't be available at that later point any more and the write operation will not result in the desired outcome. If you use \immediate\write the data is written out immediately when the data is still available.

\documentclass[british]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{babel}
\usepackage{csquotes}

\usepackage[style=authoryear, backend=biber]{biblatex}

% define and open file
\newwrite\myfile
\immediate\openout\myfile\jobname.myf\relax

% close file at end of document
\AtEndDocument{\immediate\closeout\myfile}

\AtEveryBibitem{\immediate\write\myfile{\thefield{volume}}}

\begin{document}
\cite{sigfridsson}
\printbibliography
\end{document}


## \protected@write

Alternatively, you can use \protected@write as suggested in the comments by Phelype Oleinik, which is probably a bit safer. With \protected@write the data will be written later, when the page is shipped out, but it will be expanded beforehand so that we still print the right thing.

\documentclass[british]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{babel}
\usepackage{csquotes}

\usepackage[style=authoryear, backend=biber]{biblatex}

% define and open file
\newwrite\myfile
\openout\myfile\jobname.myf\relax

% close file at end of document
\AtEndDocument{\closeout\myfile}

\makeatletter
\AtEveryBibitem{\protected@write\myfile{}{\thefield{volume}}}
\makeatother


In both cases, the .myf file contains
19