1

I would like to display DOI with \DeclareCiteCommand. I'm using \documentclass{report}.

I tried this:

\documentclass{article}

\usepackage[backend=biber, sorting=none]{biblatex} \addbibresource{references.bib}

\DeclareCiteCommand{\mycite}% { \usebibmacro{prenote}\addspace }% {% \mkbibquote{\thefield{title}}% \addcomma\addspace by \printnames[][-\value{listtotal}]{author}% \addcomma\addspace \thefield{year}% \addcomma\addspace \mkbibitalic{\thefield{journaltitle}}% \addcomma\addspace \mkbibitalic{\thefield{volume}}% \mkbibparens{\thefield{number}}% \addcomma\addspace% } {} { \usebibmacro{postnote}% \addspace% \mkbibparens{\url{\thefield{doi}}}% \adddot% }

\begin{document}

\mycite{sampleentry}

\end{document}

In the bibliography, I put this entry found in "Publication Manual of the American Psychological Association, Seventh Edition (2020)":

@article{sampleentry,
    author      =   {Wang, Xiaoye and Lind, Mats and Bingham, Geoffrey},
    year        =   {2018},
    month       =   {06},
    pages       =   {1513},
    title       =   {Large Continuous Perspective Change With Noncoplanar Points Enables Accurate Slant Perception},
    volume      =   {44},
    number      =   {10},
    journal     =   {Journal of Experimental Psychology: Human Perception and Performance},
    doi         =   {https://doi.org/10.1037/xhp0000553},
}

However, I only get \thefield{doi}. Is there another command to print the DOI?

Dunno
  • 329
  • 1
    Please note that it is extremely bad style to have \addcomma\addspace without a surrounding \setunit in bibmacros, drivers or citation definitions. \thefield should almost never be used for printing. If you want to print fields use \printfield. Formatting commands should not be added manually around \printfield (or \thefield for that matter), they should be added in field format definitions. – moewe May 21 '23 at 12:11
  • Why is it bad practice? – Dunno May 22 '23 at 17:55
  • Because "naked" punctuation (outside of \setunit/the punctuation buffer) is liable to produce clashing/double punctuation if fields are missing. biblatex has some emergency measures to prevent this (which is why in your examples the issue will probably not arise). The punctuation buffer provides a predictable framework to deal with punctuation that does not require TeX magic to avoid punctuation clashes. ... – moewe May 22 '23 at 19:24
  • ... Similarly applying formatting outside of \printunit (or even worse \thefield) will usually yield bad results for missing fields (try \mkbibparens{\thefield{number}} on something with a missing number field). – moewe May 22 '23 at 19:24

2 Answers2

4

You shouldn't have to use \thefield for printing. Instead use \printfield and friends or even better pre-defined bibmacros that print whole chunks of the data you want to show. You should also not have bare \addcomma\addspaces flying around. Those should be inside \setunits to make use of biblatex's punctuation buffer.

As shown the macro also risks introducing massive amount of undesirable space.

I suggest the following approach, where \printfield{doi} to print the DOI works perfectly fine.

Logically, however, it is a bit unusual and possibly risky to print entry data in the "postnote" code: If you cite multiple entries you end up with the data from the last key. The others are ignored. It might be more logical to move all printing into the loopcode and possibly warn if more than one entry is cited.

\documentclass[british]{article}
\usepackage[T1]{fontenc}
\usepackage{babel}
\usepackage{csquotes}

\usepackage[backend=biber, style=authoryear]{biblatex}

\DeclareFieldFormat{ccdoi}{% \mkbibparens{% \ifhyperref {\href{https://doi.org/#1}{\nolinkurl{https://doi.org/#1}}} {\nolinkurl{https://doi.org/#1}}}}

\DeclareCiteCommand{\CustomCite} {\usebibmacro{prenote}} {\printfield{title}% \setunit{\addcomma\space}% \bibstring{byauthor}% \setunit{\addspace}% \printnames[default]{author}% \setunit{\addcomma\space}% \printdate \setunit{\addcomma\space}% \usebibmacro{journal+issuetitle}} {\multicitedelim} {\usebibmacro{postnote}% \setunit{\addspace}% \printfield[ccdoi]{doi}}

\addbibresource{biblatex-examples.bib}

\begin{document} Lorem \autocite{sigfridsson}

\CustomCite{sigfridsson}

\CustomCite[381]{sigfridsson}

\printbibliography \end{document}

‘Comparison of methods for deriving atomic charges from the electrostatic potential and moments’, by Emma Sigfridsson and Ulf Ryde, 1998, Journal of Computational Chemistry 19.4 (https://doi.org/10.1002/(SICI)1096-987X(199803)19:4<377::AID-JCC1>3.0.CO;2-P)


The problem in the updated code was

\url{\thefield{doi}}

Depending on the definition \url reads and prints its argument almost verbatim. In this case this means that the output that it produces is \thefield{doi}, which is not what you want. (\mkbibparens{\thefield{doi}} instead of \mkbibparens{\url{\thefield{doi}}} would have produced output more to your liking. But you would have gotten into trouble with hyphenation and linking, so this is definitely not recommended.) Again, you want to use \printfield{doi} instead, because that uses \url (and friends) in the correct way.

moewe
  • 175,683
  • How can I know where the error came from? to make use of biblatex's punctuation buffer. What do you mean by that? – Dunno May 21 '23 at 15:52
  • @Dunno The punctuation buffer helps you deal with punctuation in case of missing fields. See https://tex.stackexchange.com/q/409148/35864 for an introduction to the concept. – moewe May 21 '23 at 20:22
0

It seems that no command can be used in the postnote part.

\DeclareCiteCommand{\CustomCite}
{
    \usebibmacro{prenote}\addspace
}
{%
    \mkbibquote{\thefield{title}}%
    \addcomma\addspace by \printnames[][-\value{listtotal}]{author} %
    \addcomma\addspace \thefield{year} %
    \addcomma\addspace \mkbibitalic{\thefield{journaltitle}} %
    \addcomma\addspace \mkbibitalic{\thefield{volume}} %
    \mkbibparens{\thefield{number}} %
    \addcomma \addspace %
    \mkbibparens{\thefield{doi}} %
}
{}
{
    \usebibmacro{postnote} %
    \adddot %
}
Dunno
  • 329
  • 1
    I don't think that is the case. If I take the code from the question and embed it into a working document I get the output I (and I guess also you) expected. Please post a useful example document that shows how things go wrong. – moewe May 21 '23 at 12:09
  • Why should I provide something else since I solved my own problem? I wouldn't have come here in the first place on a Sunday. Anyway I edited my post. – Dunno May 21 '23 at 16:01