# Beamer: Strange spacing in references with biblatex apa-style

I stumbled upon these two spacing errors (marked with red numbers):

I guess the first space is not really an error, but simply English spacing (\nonfrenchspacing). Can it be turned off (without changing the language)? \frenchspacing does not seem to have an effect on the references.

But I don't understand where the second spacing error is coming from. Has anyone got a clue?

Here is the code:

\documentclass{beamer}

\usepackage[american]{babel}
\usepackage[utf8]{inputenc}
\usepackage{csquotes}
\usepackage[style=apa, backend=biber,doi,url]{biblatex}
\DeclareLanguageMapping{american}{american-apa}

\begin{filecontents}{literature.bib}
@article {Thomas1975,
author = {Thomas, Ewart and Weaver, Wanda},
title = {Cognitive processing and time perception},
journal = {Attention, Perception, \& Psychophysics},
issn = {1943-3921},
pages = {363--367},
volume = {17},
doi = {10.3758/BF03199347},
year = {1975}
}
\end{filecontents}

\begin{document}

\frame{\textcite{Thomas1975}}
\frame{\printbibliography}

\end{document}


Without beamer (\documentclass{article}) everything looks fine:

-
There is no foobar key in your literature.bib –  Seamus May 24 '12 at 14:28
The second space is definitely a spurious space. But I'm not sure about the first. That might not be an error. (I can't select only half the space…) –  Seamus May 24 '12 at 14:33
Does the second space appear without beamer? –  PLK May 24 '12 at 14:44
Without beamer everything is fine. –  deboerk May 24 '12 at 14:56
@JosephWright The spurious space after the title goes away if you remove \newblock\usebeamercolor[fg]{bibliography entry note} in \apptocmd{\abx@macro@title} from beamerbaselocalstructure.sty. The standard styles don't appear to have this problem, though. –  Audrey May 24 '12 at 15:17

The spacing and punctuation issues are associated with patches beamer applies to biblatex bibliography macros for changing colours within bibliography items. These can be found in beamerbaselocalstructure.sty.

Extra whitespace in the first problem is associated with \usebeamercolor. This can be resolved by issuing biblatex's \unspace.

To solve the second problem we can set punctuation before changing the colour. This will suppress any spurious punctuation generated by \newunit or \setunit in the existing drivers and bibliography macros.

Here are some revised patches demonstrating these ideas. A new patch for the labeltitle bibliography macro handles the case where author-year styles use label or labeltitle as a fallback for labelname.

\AtBeginDocument{%
{\apptocmd{\blx@env@bibliography}
{\let\makelabel\beamer@biblabeltemplate}{}{}
\apptocmd{\abx@macro@begentry}
{\let\bbx@tempa\@empty%
\usebeamercolor[fg]{bibliography entry author}}{}{}
\pretocmd{\abx@macro@labeltitle}
{\ifboolexpr{ test {\ifcsundef{abx@field@label}}
and test {\ifcsundef{abx@field@labeltitle}} }{}{\let\bbx@tempa\labelnamepunct}}{}{}
\pretocmd{\abx@macro@title}
{\ifcsundef{abx@name@labelname}{}{\let\bbx@tempa\labelnamepunct}%
\bbx@tempa\newblock\unspace\usebeamercolor[fg]{bibliography entry title}}{}{}
\apptocmd{\abx@macro@title}
{\ifcsundef{abx@field@title}{}{\newunitpunct}%
\newblock\unspace\usebeamercolor[fg]{bibliography entry note}}{}{}}
{}}


Instead of editing the beamer style file, you can modify the existing patches just after \begin{document}.

...

\begin{document}
\makeatletter
\pretocmd{\abx@macro@begentry}{\let\bbx@tempa\@empty}{}{}
\pretocmd{\abx@macro@labeltitle}
{\ifboolexpr{ test {\ifcsundef{abx@field@label}}
and test {\ifcsundef{abx@field@labeltitle}} }{}{\let\bbx@tempa\labelnamepunct}}{}{}
\patchcmd{\abx@macro@title}
{\ifcsundef{abx@name@labelname}{}{\blx@unitpunct\blx@postpunct}}
{\ifcsundef{abx@name@labelname}{}{\let\bbx@tempa\labelnamepunct}}{}{}
\patchcmd{\abx@macro@title}
{\newblock\usebeamercolor[fg]{bibliography entry title}}
{\bbx@tempa\newblock\unspace\usebeamercolor[fg]{bibliography entry title}}{}{}
\patchcmd{\abx@macro@title}
{\ifcsundef{abx@field@title}{}{\blx@unitpunct\blx@postpunct}}
{\ifcsundef{abx@field@title}{}{\newunitpunct}}{}{}
\patchcmd{\abx@macro@title}
{\newblock\usebeamercolor[fg]{bibliography entry note}}
{\newblock\unspace\usebeamercolor[fg]{bibliography entry note}}{}{}
\makeatother
\nocite{Thomas1975,ctan,cms,companion,britannica,kant:ku}
\frame[allowframebreaks]{\printbibliography}
\end{document}


Here's your entry printed with some different bibliography margins.

The patches appear to give the desired effect for standard biblatex styles, but thorough testing is needed. The solution doesn't resolve issues for a few (hopefully edge) cases in biblatex-apa. These include entries that don't have any data to form a label (i.e. labelname, label, labeltitle and labelyear all missing) and biblatex-apa's overriding of useeditor and friends per-entry option settings (e.g. britannica in the above document).

Unresolved issues are probably worse for some other contributed styles. Surely beamer can't account for all of them. Perhaps the best approach is to have style authors tailor the patches as needed.

-
I'll take a look at this. Note that \usebeamercolor does not add any space per se: it's the whatsit that is the issue. –  Joseph Wright May 25 '12 at 9:29
@Audrey: I tried to apply your patch, but I couldn't make it work. I looked at the related question you linked and removed the first two lines with their corresponding curly brackets. Then I put the code right behind \begin{document} between \makeatletter and \makeatother. The code caused no error, but made no change either. Could you perhaps give an example of how to apply your patch on my example given above? Thank you! –  deboerk May 25 '12 at 9:31
@JosephWright OK. I corrected the sentence, but feel free to edit it further to clarify. –  Audrey May 25 '12 at 13:46
@deboerk Inside the document, you have to work with the edits made from the existing patches. I added an example. –  Audrey May 25 '12 at 13:47

The problem here is one that I suspected could occur: adding beamer colours to bibliographies is something of a hack, and is more risky with biblatex than with traditional BibTeX styles.

When adding to biblatex styles, we have to target the drivers 'during' the formatting: with traditional BibTeX, it's essentially done 'after' the formatting. That's an issue because the formatting produced by biblatex depends on the output, but the colour-control inserts affect that. For example, the standard biblatex set up is

\newbibmacro*{title}{%
\ifboolexpr{
test {\iffieldundef{title}}
and
test {\iffieldundef{subtitle}}
}
{}
{\printtext[title]{%
\printfield[titlecase]{title}%
\setunit{\subtitlepunct}%
\printfield[titlecase]{subtitle}}%
\newunit}%


whereas for the apa style it's

\renewbibmacro*{title}{%
\ifthenelse{\iffieldundef{title}\AND\iffieldundef{subtitle}}
{}
{\iffieldundef{origtitle}
{\printtext[title]{%
\printfield[apacase]{title}%
\setunit{\subtitlepunct}%
\printfield[apacase]{subtitle}}}%
{\printfield{origtitle}%
\printtext[brackets]{%
\printfield[apacase]{title}%
\setunit{\subtitlepunct}%
\printfield[apacase]{subtitle}}}
\iffieldequalstr{entrytype}{book}%
{}%
\ifthenelse{%
\ifnameundef{author}\AND%
$$\ifnameundef{editor}\AND\NOT\boolean{bbx:editorinauthpos}$$\AND%
\ifnameundef{namea}\AND%
\ifnameundef{nameb}}
{\newunit\newblock
\usebibmacro{labelyear+extrayear}}
{}}}


The important line here is \setunit{\addspace}: it will insert a space 'after' the current material. Unfortunately, the colour code is seen as 'output' by biblatex (there is a whatsit in the output stream, which prevents biblatex 'seeing' what is going on).

Now, the question is what to do about this. I'm not sure there is a general fix, as the appropriate place to insert the colour code depends on the biblatex driver in use. So perhaps the best thing that can be done is to offer an option not to add in any colours.

-
I would appreciate an option to turn off colors for the references. At the moment I always have to adjust the beamer theme and color everything black. –  deboerk May 25 '12 at 9:20