修补 \printbibliography 以将内容显示到文档和日志中

修补 \printbibliography 以将内容显示到文档和日志中

朋友们,我正在使用一个聪明的想法约瑟夫大卫埃格尔为了使参考书目内容显示在文档和日志中(恩里克我正在尝试创建一个半自动biblatex样式分析器)。到目前为止,这是我正在使用的代码:

\begin{filecontents}{\jobname.bib}
@BOOK{foo:2012a,
  title = {My Title One},
  publisher = {My Publisher One},
  year = {2012},
  editor = {My Editor One},
  author = {Author One}
}

@BOOK{foo:2012b,
  title = {My Title Two},
  publisher = {My Publisher Two},
  year = {2012},
  editor = {My Editor Two},
  author = {Author Two}
}

@BOOK{foo:2012c,
  title = {My Title Three},
  publisher = {My Publisher Three},
  year = {2012},
  editor = {My Editor Three},
  author = {Author Three}
}
\end{filecontents}

\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[backend=biber,
            bibstyle=numeric-comp,
            sorting=none]{biblatex}

\addbibresource{\jobname.bib}

\begin{document}

\nocite{*}

\newsavebox\bibbox
\savebox\bibbox{\parbox{\textwidth}{\printbibliography}}
\showboxbreadth\maxdimen\showboxdepth\maxdimen\errorstopmode
\wlog{BEGIN BIBLIOGRAPHY}
\showbox\bibbox
\wlog{END BIBLIOGRAPHY}
\printbibliography

\end{document}

它运行良好,包括.log显示:

BEGIN BIBLIOGRAPHY
> \box26=
\hbox(65.92384+60.92383)x345.0
.\mathon
.\vbox(65.92384+60.92383)x345.0
..\penalty -300
..\glue 15.06577 plus 4.3045 minus 0.86089
..\glue(\parskip) 0.0
..\hbox(9.93758+0.0)x345.0, glue set 271.08008fil
...\hbox(0.0+0.0)x0.0
....\glue 0.0
...\T1/cmr/bx/n/14.4 R
...\T1/cmr/bx/n/14.4 e
...\T1/cmr/bx/n/14.4 f
...\T1/cmr/bx/n/14.4 e
...\T1/cmr/bx/n/14.4 r
...\T1/cmr/bx/n/14.4 e
...\T1/cmr/bx/n/14.4 n
...\T1/cmr/bx/n/14.4 c
...\T1/cmr/bx/n/14.4 e
...\T1/cmr/bx/n/14.4 s

[...]

我试图修补\printbibliography以“隐藏”这些宏,以便我们轻松调试其他文档。我的计划是创建一个调试包并将其包含在主文档中,例如mydebug.sty

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{mydebug}[2013/01/09 Debugging biblatex styles]

\let\origprintbibliography\printbibliography
\renewcommand\printbibliography[1][]{
\newsavebox\bibbox
\savebox\bibbox{\parbox{\textwidth}{\oldprintbibliography[#1]}}
\showboxbreadth\maxdimen\showboxdepth\maxdimen\errorstopmode
\wlog{BEGIN BIBLIOGRAPHY}
\showbox\bibbox
\wlog{END BIBLIOGRAPHY}
\oldprintbibliography[#1]
}

但遗憾的是,我未能修补\printbibliography。有人能指引我的路吗?:)

答案1

这是常见的问题:你需要letltxmacro,因为\printbibliography有一个可选参数,请参阅何时使用 \LetLtxMacro?。以下是我的选择:

\usepackage{letltxmacro}
\LetLtxMacro\biblatexprintbibliography\printbibliography
\newsavebox\bibbox

\renewcommand\printbibliography[1][]{
  \chardef\currentmode=\interactionmode % save the current interaction mode
  \batchmode % set batch mode so we won't be interrupted
  \begingroup % the changes to the parameters are local
    \showboxbreadth\maxdimen
    \showboxdepth\maxdimen
    \savebox\bibbox{\parbox{\textwidth}{\biblatexprintbibliography[#1]}}
    \wlog{BEGIN BIBLIOGRAPHY}
    \showbox\bibbox
    \wlog{END BIBLIOGRAPHY}
  \endgroup
  \interactionmode=\currentmode % restore the interaction mode
  \biblatexprintbibliography[#1]
}

交互模式的改变始终是全局的,这就是为什么需要明确地恢复它。


另一种方法,简单修补\blx@printbibliography

\usepackage{xpatch}
\makeatletter
\def\biblio@inlog#1{%
  \begingroup
  \let\biblio@inlog\@gobble
  \chardef\current@mode\interactionmode
  \showboxdepth=\maxdimen
  \showboxbreadth=\maxdimen
  \sbox\z@{\vbox{\printbibliography[#1]}}
  \batchmode
  \wlog{BEGIN BIBLIOGRAPHY}
  \showbox\z@
  \wlog{END BIBLIOGRAPHY}
  \interactionmode=\current@mode
  \endgroup}
\xapptocmd{\blx@printbibliography}{\biblio@inlog{#1}}{}{}
\makeatother

\biblio@inlog命令在展开时会重新定义自身,因此在构建要显示的框时它不会执行任何操作。

相关内容