# In biblatex (and fnpct) the feature of moving around punctuation breaks if using color commands over it

Biblatex by default (autopunct = true) moves punctuations around for superscript citation. However, changing the color of the superscript by \textcolor or by \color (need extra grouping) breaks this feature. I noticed this even happens also with fnpct. As can be seen in the MWE all attempts to have a colored superscript after punctuation failed (lines 2 to 4). Any idea how to solve this?


\documentclass{article}
\usepackage{xparse}
\usepackage{xcolor}
\usepackage[
%autopunct = true,  %that is the default setting
autocite = superscript,
hyperref=true
]{biblatex}

\NewDocumentCommand\testA{m}{%
\begingroup\color{blue}\autocite{#1}\endgroup%
}%

\NewDocumentCommand\testB{m}{%
\color{blue}\autocite{#1}%
}%

\begin{document}

This is a normal test \autocite{loh}. Things after.

This is an inline with color test \textcolor{blue}{\autocite{loh}}. Things after.

This is a macro test \testA{loh}. Things after.

This is a macro test \testB{loh}. Things after.

\end{document}



EDIT: As an example for what I am trying to reproduce: https://www.nature.com/articles/s41699-019-0098-2.pdf

EDIT 2: To make it more clear: I need one pdf-a version (through pdfx) for archiving and one pdf-x version for printing. Many of hyperref features, including citecolor are disabled with pdf-x and using hyperref citecolor option is not possible anymore. Some more detailss are mentioned here: X-4 option of PDFX does not show hyperlink color with the PDF/X option (x-4)

• I'm sorry to say this is just how it is. On the implementational level the command needs to absorb the punctuation as an argument. If you wrap it in a pair of braces like in the example, then the punctuation can not be read as an argument to \autocite and is ignored. – moewe Apr 19 '19 at 5:19
• What exactly do you want to do? Do you want to colour all footnote markers? Only footnote markers for citations? Do you want to colour all of biblatex's citation outputs? Is hyperref involved? – moewe Apr 19 '19 at 5:20
• @moewe, thanks for the reply. Yes I want all biblatex citations (which should be in superscript) to be colored and correctly after punctuations. Examples might be find in many scientific journals, such as here: nature.com/articles/s41699-019-0098-2.pdf And yes, hyperref is involved in the real case (not in MWE). It refs to the entry in the bibliography. – Shahryar Apr 19 '19 at 6:26
• I'm asking about hyperref because a cheap solution might be to just colour all citation links in blue \usepackage[colorlinks,citecolor=blue]{hyperref}. Technically you would not be colouring citations, you would be colouring all linked parts of citations. But in your case this probably coincides. – moewe Apr 19 '19 at 6:50
• @moewe Well, that leads to another caveat which I mentioned here: tex.stackexchange.com/questions/481893/… In other words, trying to have press-ready pdf one should ignore hyperref citecolor option. – Shahryar Apr 19 '19 at 6:54

The way \autocite's autopunct punctuation detection and moving feature works requires that the punctuation follow \autocite directly so that it can be absorbed as a final argument. If \autocite itself is wrapped in a command, then it can not absorb the punctuation after the wrapper. In theory it would be possible to make the wrapper absorb the punctuation and pass it on to \autocite, but I consider it bad practice to write wrapper commands like \testA and \testB for biblatex \...cite commands anyway. Instead we can use the wrapper argument to \DeclareCiteCommand. The original definition of \supercite can be found in numeric.cbx and we only change the wrapper

\documentclass[12pt]{article}
\usepackage{xcolor}
\usepackage[
autocite = superscript,
]{biblatex}

\newrobustcmd*{\mkbibcolouredsuperscript}[1]{%
\textcolor{blue}{%
\mkbibsuperscript{#1}}}

\DeclareCiteCommand{\supercite}[\mkbibcolouredsuperscript]
{\iffieldundef{prenote}
{}
{\BibliographyWarning{Ignoring prenote argument}}%
\iffieldundef{postnote}
{}
{\BibliographyWarning{Ignoring postnote argument}}}
{\usebibmacro{citeindex}%
\usebibmacro{cite}}
{\supercitedelim}
{}

\begin{document}
This is a normal test \autocite{loh}. Things after.

This is an inline with color test \textcolor{blue}{\autocite{loh}}. Things after.
\end{document}


As I mentioned in the comments, if you use hyperref and only want to colour the citation links and not the citation itself

\usepackage[colorlinks,citecolor=blue]{hyperref}


might be a nicer solution. But in this case that is out of the question because of pdfx features.

Addressing the comment. The same general approach works for all styles. But the exact code required may vary since the answer needs the correct definition of teh \...cite command. For numeric-comp we would need

\DeclareCiteCommand{\supercite}[\mkbibcolouredsuperscript]
{\usebibmacro{cite:init}%
\let\multicitedelim=\supercitedelim
\iffieldundef{prenote}
{}
{\BibliographyWarning{Ignoring prenote argument}}%
\iffieldundef{postnote}
{}
{\BibliographyWarning{Ignoring postnote argument}}}
{\usebibmacro{citeindex}%
\usebibmacro{cite:comp}}
{}
{\usebibmacro{cite:dump}}


I absolutely can not recommend this, but since you asked about it in the comments, here is a solution that shows how you could absorb the punctuation command from after the wrapper and typeset it before \autocite.

\documentclass[12pt]{article}
\usepackage{xcolor}
\usepackage[
autocite = superscript,
]{biblatex}

\makeatletter
\newcommand*{\testaAautocite}[2]{%
#2\unspace
\textcolor{blue}{\autocite{#1}}}
\newcommand{\testA}[1]{%
\blx@thecheckpunct{\testaAautocite{#1}}}
\makeatother

\begin{document}
This is a normal test \autocite{loh}. Things after.

This is an inline with color test \textcolor{blue}{\autocite{loh}}. Things after.

This is a normal test \testA{loh}. Things after.

This is a normal test \testA{loh} and non-punctuation afterwards.

This is a normal test \testA{loh}and non-punctuation afterwards.
\end{document}

• Thanks a lot for the answer. It works nice. However, If I user style=numeric-comp it throws Package biblatex Error: Bibliography macro 'cite' undefined.. It works fine with style=numeric. Also, while I understand your concern "... possible to make the wrapper absorb the punctuation and pass it on to \autocite, but I consider it bad practice to write wrapper commands ..", in my case it gives other possibilities. I convert Asciidoc to Latex and cite keys need some preprocessing before passing to autocite. So, I appreciate if you give some clue how to pass punc to autocite. – Shahryar Apr 19 '19 at 9:23
• @Shahryar See the edit, please. In case you have to preprocess the cite keys I strongly suggest to do this at an earlier (bit definitely a different) stage than in the document right before passing them to \autocite. – moewe Apr 19 '19 at 11:04
• thanks a lot for you complete and perfect answer! – Shahryar Apr 19 '19 at 11:12