# How to add urldate to first citation in online references

I want to add the date when I visited my online references in the very first mentioning of these. I use biblatex with the authoryear-comp style. Here is a MWE:

\documentclass{scrreprt}
\usepackage[style=authoryear-comp]{biblatex}
\usepackage{filecontents}

\begin{filecontents}{\jobname.bib}
@online{key,
title = {Very nice online reference},
url = {https://www.example.com/},
urldate = {2019-11-25},
author = {Nobody},
}
\end{filecontents}

% Start of the document.
\begin{document}

I want to cite here with the urldate in the citation.
\parencite{key} should look like:
(Author 2001, visited on 25.11.2019) but just when I first reference it.
In every later cication it shall just look (Author 2001).

% End of the document.
\end{document}


Can anybody help me with that problem?

Remark by user https://tex.stackexchange.com/users/14649: This is a follow question up on How to access urldate in text.

You can try something like this. The option citetracker is needed for the \ifciteseen test. The definition of cite is the original definition from authoryear-comp.cbx with the following two lines

  \setunit{\addcomma\space}%
\usebibmacro{cite:urlinfo}%


The same can be done for textcite, but it should be noted that \textcite has a few special cases (compressed citations, citations without name or year, shorthands) that might give slightly unexpected output with this configuration. I couldn't think of a better general solution, so I recommend to scrutinise the output carefully.

\documentclass{scrreprt}
\usepackage[style=authoryear-comp, citetracker]{biblatex}

\newbibmacro*{cite:urlinfo}{%
\ifciteseen
{}
{\DeclareFieldFormat{urldate}{\bibstring{urlseen}\space##1}%
\printurldate}}

\makeatletter
\renewbibmacro*{cite}{%
\iffieldundef{shorthand}
{\ifthenelse{\ifnameundef{labelname}\OR\iffieldundef{labelyear}}
{\usebibmacro{cite:label}%
\setunit{\printdelim{nonameyeardelim}}%
\usebibmacro{cite:reinit}}
{\iffieldequals{namehash}{\cbx@lasthash}
{\ifthenelse{\iffieldequals{labelyear}{\cbx@lastyear}\AND
$$\value{multicitecount}=0\OR\iffieldundef{postnote}$$}
{\setunit{\compcitedelim}%
\savefield{labelyear}{\cbx@lastyear}}}
{\printnames{labelname}%
\setunit{\printdelim{nameyeardelim}}%
\savefield{namehash}{\cbx@lasthash}%
\savefield{labelyear}{\cbx@lastyear}}}}
{\usebibmacro{cite:shorthand}%
\usebibmacro{cite:reinit}}%
\usebibmacro{cite:urlinfo}%
\setunit{\multicitedelim}}

\renewbibmacro*{textcite}{%
\iffieldequals{namehash}{\cbx@lasthash}
{\iffieldundef{shorthand}
{\ifthenelse{\iffieldequals{labelyear}{\cbx@lastyear}\AND
$$\value{multicitecount}=0\OR\iffieldundef{postnote}$$}
{\setunit{\compcitedelim}%
\savefield{labelyear}{\cbx@lastyear}}}
{\setunit{\compcitedelim}%
\usebibmacro{cite:shorthand}%
\global\undef\cbx@lastyear}}
{\ifnameundef{labelname}
{\iffieldundef{shorthand}
{\usebibmacro{cite:label}%
\setunit{%
\global\booltrue{cbx:parens}%
\printdelim{nonameyeardelim}\bibopenparen}%
\ifnumequal{\value{citecount}}{1}
{\usebibmacro{prenote}}
{}%
{\usebibmacro{cite:shorthand}}}
{\printnames{labelname}%
\setunit{%
\global\booltrue{cbx:parens}%
\printdelim{nameyeardelim}\bibopenparen}%
\ifnumequal{\value{citecount}}{1}
{\usebibmacro{prenote}}
{}%
\iffieldundef{shorthand}
{\iffieldundef{labelyear}
{\usebibmacro{cite:label}}
\savefield{labelyear}{\cbx@lastyear}}
{\usebibmacro{cite:shorthand}%
\global\undef\cbx@lastyear}}%
\stepcounter{textcitecount}%
\savefield{namehash}{\cbx@lasthash}}%
\usebibmacro{cite:urlinfo}%
\setunit{%
\ifbool{cbx:parens}
{\bibcloseparen\global\boolfalse{cbx:parens}}
{}%
\textcitedelim}}
\makeatother

\begin{filecontents}{\jobname.bib}
@online{key,
title   = {Very nice online reference},
url     = {https://www.example.com/},
urldate = {2019-11-25},
author  = {Nobody},
}
\end{filecontents}

• @mathse I added something for textcite as well, but since the code there is more complex I strongly recommend you check the output carefully, one might have to work around a few oddities. – moewe Dec 21 '19 at 7:07