1

I was implementing a custom version of Guido's answer when I ran into a snag. His code runs as follows:

\DeclareAutoCiteCommand{inline}{\mycite}{\cites}
\DeclareCiteCommand{\mycite}
  {}
  {\ifentrytype{personalcommunication}
  {\printnames{labelname} \mkbibparens{personal communication, \printdate}}
  {\mkbibparens{\usebibmacro{cite}}}%
  }
 {}
 {}

I wanted to customise this so it would print (Elliott, pers. comm., 20th Jul. 2017) for

@misc{elliott2017,
    author = {Robin Elliott},
    date = "2017-07-20",
    howpublished = "personal communication"
}

The snag for me came when I tried to include the date as the hyperlink to the bibliography entry (which, unlike Guide's answer above I want to keep).

Looking into the BibLaTeX manual I found \bibhyperref and \bibhypertarget, but \bibhyperref caused a lot of errors when I tried referring to \currentfield. Likewise, I am not sure if that is a logically sound process when I think about it. Anyways, this is how I modified the above code along with how I declare BibLaTeX and hyperref:

\documentclass[12pt,oneside]{article}

\usepackage[backend=bibtex, style=ieee, natbib=true, backref=true, backrefstyle=two, sortcites=true, autocite=inline]{biblatex}

\addbibresource{ref_pers_comm.bib}
\usepackage[plainpages=false,bookmarks=true,bookmarksnumbered=true,colorlinks=true,,breaklinks,pdftitle={},pdfauthor={},pdfdisplaydoctitle=true,pdfpagelayout={SinglePage}]{hyperref}

\DeclareAutoCiteCommand{inline}{\mycite}{\cites}
\DeclareCiteCommand{\mycite}
{}
{\ifentrytype{misc}{%
        \IfStrEq{\thefield{howpublished}}{personal communication}
        {\mkbibparens{\printnames{labelname}, {\emph{pers. comm.}, 
\bibhyperref{\currentfield}{\printdate}}}
        {\mkbibparens{\usebibmacro{cite}}}%
    }
    {\mkbibparens{\usebibmacro{cite}}}%
}
{}
{} 

\begin{document}

\ldots considerably more useful \autocite{elliott2017}\ldots

\printbibliography

\end{document}

Clearly, however, the \currentfield at the very least is wrong as I get an Undefined control sequence. error when I compile with the above.

So:

  1. Is it \bibhyperref or \bibhypertarget that I should be using (or something third entirely)?
  2. What is the handle that I can use for the above command to take me to the correct reference list printout?

EDIT: Added the info requested (hopefully) but the formatting has gone insane. Random newlines are added every time I try a space.

  • It would really help us if you could show us a full MWE/MWEB. The solution will depend on the style you use as well as on other modifications you might have made to the style. – moewe Aug 2 '17 at 13:16
  • I don't think \currentfield is defined. What do you want it to do? Instead of \bibhyperref or \bibhypertarget you probably need to use \printtext[bibhyperlink]{\printdate}. And you don't need to pass a handle or label to it, that will be done automatically. – moewe Aug 2 '17 at 13:28
  • @moewe: Done. Should have thought of it beforehand... The reference file contains that reference as brought up. – gktscrk Aug 2 '17 at 14:13
  • @moewe: I was thinking based on its description in the reference that \currentfieldrefers to the printed entry in the bibliography, hence used it as a handle like I would if I wanted to go for \ref{section:introduction} having previously defined \section{Introduction}\label{section:introduction}. I'll try your recommendation now ==> No luck. Gives the error: File ended while scanning use of \blx@defcitecmd@iv. – gktscrk Aug 2 '17 at 14:15
  • Ahh, OK \currentfield is only available within a \printfield. So you can't use it here. – moewe Aug 2 '17 at 17:43
2

Try

\makeatletter
\DeclareAutoCiteCommand{inline}{\mycite}{\mycites}
\DeclareMultiCiteCommand{\mycites}{\mycite}{\multicitedelim}
\DeclareCiteCommand{\mycite}
  {\usebibmacro{cite:init}%
   \bibopenbracket
   \usebibmacro{prenote}}
  {\ifboolexpr{test {\ifentrytype{misc}} and test {\iffieldequalstr{howpublished}{personal communication}}}
     {\addtocounter{cbx@tempcntb}{1}%
      \printnames{labelname}%
      \setunit{\addcomma\space}%
      \printtext[emph]{pers. comm\addperiod}%
      \setunit{\addcomma\space}%
      \printtext[bibhyperref]{\printdate}%
      \ifnumgreater{\value{cbx@tempcntb}}{-1}
        {\multicitedelim}
        {}}
     {\usebibmacro{cite:comp}}}
  {}
  {\usebibmacro{cite:dump}%
   \usebibmacro{postnote}%
   \bibclosebracket}
\makeatother

Unfortunately, the code became quite complicated since you use biblatex-ieee, which is based on nuemric-comp. numeric-comp needs quite some helper macros and tricks to obtain the compressed output.


Because it is hard to explain what happens in the code above due to the intricate changes needed for numeric-comp, let me explain it assuming you used a different base style that is easier to modify. Let's say style=authoryear.

From your original code I would have then made

\DeclareAutoCiteCommand{inline}{\mycite}{\mycites}
\DeclareMultiCiteCommand{\mycites}{\mycite}{\multicitedelim}
\DeclareCiteCommand{\mycite}[\mkbibparens]
  {}
  {\ifboolexpr{test {\ifentrytype{misc}} and
               test {\iffieldequalstr{howpublished}{personal communication}}}
     {\printnames{labelname}%
      \setunit{\addcomma\space}%
      \printtext[emph]{pers. comm\addperiod}%
      \setunit{\addcomma\space}%
      \printtext[bibhyperref]{\printdate}}
     {\usebibmacro{cite}}}
  {}
  {}

Here it is easier to compare and contrast my solution to your attempt.

To your questions

  1. You should be using \printtext[bibhyperref]{...}
  2. \printtext[bibhyperref]{...} already takes care of everything, the correct label is automatically determined, you only need to put the text you want linked in the curly braces.

Please note how I made use of more biblatex-y commands in your command definition.

  • Do not use literal punctuation, use biblatex's punctuation tracker and the correct macros. Instead of , write \setunit{\addcomma\space}. See §4.11.7 Using the Punctuation Tracker of the documentation.
  • Do not print text directly, use \printtext or even better \bibstrings
  • Use \iffieldequalstr instead of \IfStrEq{\thefield{howpublished}}{...}

It might have been even nicer to modify the bibmacro cite directly or at least the cite command \cite instead of defining a new \mycite.

  • Thanks; this is a very good and detailed answer. I especially appreciate your effort in explaining it through a simpler usage. I'll investigate the punctuation thing as well, thanks for noting it. What's the benefit of the \iffieldequalstr format though? The ifstatement previously used looks a bit more readable (opinion?)... – gktscrk Aug 2 '17 at 20:27
  • @gktscrk I prefer \iffieldequalstr{howpublished}{personal communication} to \IfStrEq{\thefield{howpublished}}{personal communication} because I like to use the commands defined by biblatex for this task and because it is slightly shorter. I used \ifboolexpr to consolidate the two \ifs in your code into one. This is at first a bit harder to parse and maybe longer, but I avoid to call \usebibmacro{cite} twice. But that is again more a matter of taste than anything else. – moewe Aug 2 '17 at 21:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.