Beamer: Strange spacing in references with biblatex apa-style

I stumbled upon these two spacing errors (marked with red numbers):
enter image description here

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}
\addbibresource{literature.bib}

\begin{document}

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

\end{document}

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

enter image description here

Solutions Collecting From Web of "Beamer: Strange spacing in references with biblatex apa-style"

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{%
  \@ifpackageloaded{biblatex}
    {\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}.

...
\addbibresource{biblatex-examples.bib}

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

enter image description here

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.

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}%
  \printfield{titleaddon}}

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}%
       \setunit{\addspace}%
       \printtext[brackets]{%
        \printfield[apacase]{title}%
        \setunit{\subtitlepunct}%
        \printfield[apacase]{subtitle}}}
     \setunit{\addspace}%
     \printfield{titleaddon}%
     \iffieldequalstr{entrytype}{book}%
       {\setunit{\addspace}\usebibmacro{bookaddinfo}}%
       {}%
     \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.