plk / biblatex

biblatex is a sophisticated bibliography system for LaTeX users. It has considerably more features than traditional bibtex and supports UTF-8
518 stars 118 forks source link

Incorrect formatting of citations within reference #988

Open dominikswild opened 4 years ago

dominikswild commented 4 years ago

Numeric citations within a reference are not correctly compressed (not saying one should be citing within a reference). Consider the following .bib file:

@misc{Comment,
  note = {{This comment cites \cite{Article1,Article2,Article3}.}}
}
@article{Article1,
  author = {One, Author},
  title = {Title One},
  year = {2001}
}
@article{Article2,
  author = {Two, Author},
  title = {Title Two},
  year = {2002}
}
@article{Article3,
  author = {Three, Author},
  title = {Title Three},
  year = {2003}
}

Here is a simple example that uses the above bibliography:

\documentclass{article}

\usepackage[style=numeric,citestyle=numeric-comp]{biblatex}
\addbibresource{bibliography.bib}

\begin{document}
  Reference to comment \cite{Comment}.
  \printbibliography
\end{document}

After compiling a spurious period and space show up in reference [1]: image

Everything works fine when the citestyle is not compressed. The issue is present for both Biber and BibTeX as a backend. BibTeX on its own works correctly on this example.

moewew commented 4 years ago

This is essentially the same issue as https://tex.stackexchange.com/q/397975/35864 (and https://tex.stackexchange.com/q/435162/35864) on TeX.SX.

One way around this is to make sure that \blx@citecmdinit always executes \blx@initunit with

\protected\def\blx@citecmdinit{%
  \blx@leavevmode@cite
  \blx@initunit}

By default it only does that when it's not in the bibliography https://github.com/plk/biblatex/blob/23f6a15eef21789da19068f62be85dffa494e623/tex/latex/biblatex/biblatex.sty#L11604-L11608

\documentclass{article}
\usepackage[style=numeric-comp]{biblatex}

\makeatletter
\protected\def\blx@citecmdinit{%
  \blx@leavevmode@cite
  \blx@initunit}
\makeatother

\begin{filecontents}{\jobname.bib}
@misc{Comment,
  note = {This comment cites \cite{Article1,Article2,Article3}.}
}
@article{Article1,
  author = {One, Author},
  title  = {Title One},
  year   = {2001},
}
@article{Article2,
  author = {Two, Author},
  title  = {Title Two},
  year   = {2002},
}
@article{Article3,
  author = {Three, Author},
  title  = {Title Three},
  year   = {2003},
}
\end{filecontents}
\addbibresource{\jobname.bib}

\begin{document}
  Reference to comment \cite{Comment}.

  \printbibliography
\end{document}

An alternative is to 'manually' reset the punctuation tracker before the problematic citation

\documentclass{article}
\usepackage[style=numeric-comp]{biblatex}

\makeatletter
\newcommand*{\mkbibcitesafe}{\blx@initunit}
\makeatother

\begin{filecontents}{\jobname.bib}
@misc{Comment,
  note = {This comment cites \mkbibcitesafe\cite{Article1,Article2,Article3}.}
}
@article{Article1,
  author = {One, Author},
  title  = {Title One},
  year   = {2001},
}
@article{Article2,
  author = {Two, Author},
  title  = {Title Two},
  year   = {2002},
}
@article{Article3,
  author = {Three, Author},
  title  = {Title Three},
  year   = {2003},
}
\end{filecontents}
\addbibresource{\jobname.bib}

\begin{document}
  Reference to comment \cite{Comment}.

  \printbibliography
\end{document}

Even though I think it would be more logical to redefine \blx@citecmdinit as

\protected\def\blx@citecmdinit{%
  \blx@leavevmode@cite
  \blx@initunit}

in biblatex.sty, I don't think we can do that because it may break current uses of "integrated" cite commands in the bibliography (I'm thinking of biblatex-chicago, I don't think using \cite commands in the bibliography the way biblatex-chicago does is a good idea nowadays [it might have been at the time the style was initially written], but we'll probably have to accept that biblatex-chicago is not going to change and it is still quite popular, so nothing we want to break).

If at all possible I strongly recommend you avoid \cite commands in the bibliography. See also https://github.com/plk/biblatex/issues/904.

moewew commented 4 years ago

Mhhh ... given that my own style apparently uses \cite commands in the bibliography, I probably shouldn't just point to biblatex-chicago here. biblatex-dw also uses cite commands in the bibliography and Audrey also appears to recommend it in https://tex.stackexchange.com/q/18820/35864, though things are more subtle there.

Not that this matters for what I wrote above, but I want to avoid the impression I'm unjustly singling out a particular style here.

moewew commented 4 years ago

It turns out that we can do something about this particular example by being more consistent in using the punctuation buffer (\setunit and friends) in numeric-comp. Since we are already reworking it heavily for the next release (#900, #963) we might as well add this change.

This does not resolve the underlying problem with the punctuation buffer and \cite has in bibliographies, though.

moewew commented 4 years ago

MWE from #1046 showing the same issue

\documentclass{article}
\usepackage[style = authoryear-comp]{biblatex}

\begin{filecontents}{\jobname.bib}
@book{test01, author = {Author1}, title = {Title1}, year = {2001}}
@book{test02, author = {Author2}, title = {Title2}, year = {2002}}
@book{test03,
    author = {Author3}, title = {Title3}, year = {2003},
    addendum = {\parencite{test01}, \parencite{test02},
        \parencite{test01}, \parencite{test02}}}
\end{filecontents}
\addbibresource{\jobname.bib}

\begin{document}
\printbibliography
\nocite{*}
\end{document}