我正在尝试破解一个书目包 ( amsrefs
),并遇到了以下问题。我试图将 amsrefs 格式转换回 .bib 文件。这不太管用,因为字符串会被修改;即,诸如 的名称Goode, Johnny~B.
会转换为Goode, Johnny\nobreakspace {}B.
,而当我尝试 \immediate\write 它时,输出流中会出现以下内容。
我如何强制\nobreakspace
和/或更复杂的\protect \unhbox \voidb@x \penalty \@M
宏进行评估~
?
这是我想要实现的简化版本,问题如下:将此文件保存到“test.tex”并进行编译,生成“test.bib”,其中包含以下行
%Output bibliography test.tex to test.bib
@Book{
title={Goode, Johhny\protect \unhbox \voidb@x \penalty \@M \ {}B.}
}
而不仅仅是“title={Goode, Johnny~B.}”
\documentclass{article}
\usepackage{amsrefs}
\makeatletter
\def\printtitle#1{\immediate\write\bibfile{ title={#1}}}
\BibSpec{book}{%
+{}{\immediate\write\bibfile{@Book\@charlb}}{transition}
+{}{\printtitle}{title}
+{}{\immediate\write\bibfile{\@charrb}}{transition}
}
\begin{document}
\immediate\newwrite\bibfile
\immediate\openout\bibfile=\jobname.bib
\immediate\write\bibfile{\@percentchar Output bibliography \jobname.tex to \jobname.bib}
\begin{bibdiv}
\begin{biblist}
\bib{test}{book}{
title={Goode, Johhny~B.}
}
\end{biblist}
\end{bibdiv}
\immediate\closeout\bibfile
\end{document}
答案1
您需要一个\immediate
版本\write
(见将 \\ 写入文件)。该\protected@iwrite
命令还有一个参数,用于提供写入时应用的设置。要写入文字,~
您需要使其(暂时)等同于\relax
:
\documentclass{article}
\usepackage{amsrefs}
\usepackage{xpatch}
\makeatletter
% get a copy of `\protected@write
\let\protected@iwrite\protected@write
% patch the copy to add \immediate
\xpatchcmd{\protected@iwrite}{\write}{\immediate\write}{}{}
\newcommand{\bib@write}[1]{%
\protected@iwrite\bibfile
{\let~\relax}%
{#1}%
}
\def\printtitle#1{\bib@write{ title={#1}}}
\BibSpec{book}{%
+{}{\bib@write{@Book\@charlb}}{transition}
+{}{\printtitle}{title}
+{}{\bib@write{\@charrb}}{transition}
}
\newwrite\bibfile
\immediate\openout\bibfile=\jobname.bib
\bib@write{\@percentchar Output bibliography \jobname.tex to \jobname.bib}
\makeatother
\begin{document}
\begin{bibdiv}
\begin{biblist}
\bib{test}{book}{
title={Goode, Johhny~B.}
}
\end{biblist}
\end{bibdiv}
\immediate\closeout\bibfile
\end{document}
以下是 的内容\jobname.bib
:
%Output bibliography bartholdi.tex to bartholdi.bib
@Book{
title={Goode, Johhny~B.}
}