Biblatex:将 \thefield{...} 的输出写入文件

Biblatex:将 \thefield{...} 的输出写入文件
  1. 简短的问题:在 Biblatex 中,如何将(字符串)字段内容打印到文件中?处理条目时,我想做这样的事情:

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

我想将最终文本写入文件,如 \typeout{} 打印出来的一样:

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

对于我的条目,字段“userc”包含逗号分隔的“REF1,REF2,...”类型的列表。\write 的结果是错误的,我不知道如何正确展开宏。

我希望文件中有“REF1 REF2”来生成诸如 \addtocategory{REF1}{KEY}、\addtocategory{REF2}{KEY} 之类的命令,然后在需要时输入该文件。

  1. 解释尝试的内容和原因。我正在尝试生成我作品的引用列表。目前,我已将所有引用书目条目都配备了以下字段
@<entrytype>{KEY,
            RELATED={REF1,REF2}, 
            RELATEDTYPE={set}
            }

其中 REF1、REF2 是工作 KEY 中引用的项目的键。添加合适的宏后即可实现

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

(这个想法是在网上找到的)并生成参考书目列表,后面跟着引用它们的项目。但现在我需要以相反的方式组织它(作品列表,后面跟着它们的引用),并尽可能保留 BiB 文件的当前结构。我不是专家;欢迎任何帮助。

答案1

\immediate\write

写入的一个选项\thefield是写入\immediate。如果你说\write,参数将在页面发送出去后稍晚一点写出,而不是在你所说的时间点\write(如果你需要正确获取页面引用,这非常方便,请参阅https://tex.stackexchange.com/a/103944/35864)。不幸的是,\thefield稍后将不再可用,并且写入操作不会产生预期的结果。如果您使用,则\immediate\write在数据仍然可用时会立即写出数据。

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

\addbibresource{biblatex-examples.bib}

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

\protected@write

或者,你可以\protected@write按照建议使用在评论中经过菲利佩·奥莱尼克,这可能更安全一些。\protected@write数据将在稍后页面发送出去时写入,但会预先展开,以便我们仍然打印正确的内容。

\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

\addbibresource{biblatex-examples.bib}

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

在这两种情况下,.myf文件都包含

19

相关内容