13

Friends, I'm using a clever idea from Joseph, David and egreg in order to get the bibliography content displayed in both document and log (Henrique and I are trying to create a semi-automatic analyzer for biblatex styles). So far, this is the code I'm using:

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

It works like a charm, including the .log display:

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

[...]

I was trying to patch \printbibliography in order to "hide" these macros and make it easy for us to debug other documents. My plan was to create a debug package and include it in the main document, say 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]
}

But sadly, I'm failing at patching \printbibliography. Could someone enlight my path? :)

Paulo Cereda
  • 44,220

1 Answers1

12

It's the usual problem: you need letltxmacro, because \printbibliography has an optional argument, see When to use \LetLtxMacro?. Here's my pick:

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

The changes to the interaction mode are always global, that's why one needs to restore it explicitly.


A different way, simply patching \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

The \biblio@inlog command redefines itself when expanded, so it will do nothing when the box to be shown is built.

egreg
  • 1,121,712