3

I want only the year of the citation in blue, NOT everything like it its now.

LaTeX

\documentclass[a4paper,10pt]{article}

\usepackage{natbib} \usepackage{apalike}

\usepackage{hyperref} \hypersetup{ hidelinks=false,
colorlinks=true, citecolor=blue }

\begin{document}

This is an example of ... \citep{author1}.

\bibliographystyle{apalike}
\bibliography{Bibliography}

\end{document}

Bibliography.bib

@book{author1,
  title={Book Title},
  author={Author},
  year={2014},
  publisher={Publisher}
}
Mico
  • 506,678
LeoFr
  • 59

1 Answers1

3

It took some time to find the right call to \NAT@hyper@ to patch.

\begin{filecontents*}{\jobname.bib}
@book{author1,
  title={Book Title},
  author={Author},
  year={2014},
  publisher={Publisher}
}
\end{filecontents*}

\documentclass[a4paper,10pt]{article}

\usepackage{natbib} \usepackage{apalike} \usepackage{xpatch}

\usepackage{hyperref} \hypersetup{ %hidelinks, colorlinks=true, citecolor=blue }

\makeatletter \xpatchcmd\NAT@citex {% @citea\NAT@hyper@{% \NAT@nmfmt{\NAT@nm}% \hyper@natlinkbreak{\NAT@aysep\NAT@spacechar}{@citeb@extra@b@citeb}% \NAT@date }% } {% @citea \NAT@nmfmt{\NAT@nm}% \NAT@aysep\NAT@spacechar \NAT@hyper@{\NAT@date}% } {}{} \xpatchcmd\NAT@citex {% @citea\NAT@hyper@{% \NAT@nmfmt{\NAT@nm}% \hyper@natlinkbreak{\NAT@spacechar\NAT@@open\if#1\else#1\NAT@spacechar\fi}% {@citeb@extra@b@citeb}% \NAT@date }% } { @citea \NAT@nmfmt{\NAT@nm}% \NAT@spacechar\NAT@@open\if#1\else#1\NAT@spacechar\fi \NAT@hyper@{\NAT@date}% } {}{} \makeatother

\begin{document}

This is an example of ... \citep{author1}.

This is an example of ... \citet{author1}.

\bibliographystyle{apalike} \bibliography{\jobname}

\end{document}

enter image description here

egreg
  • 1,121,712
  • Your solution works correctly with \citep, but does not appear to work as it should with \citet. – Mico Jun 01 '21 at 11:31
  • thanks! if you can make that work for \citet too then I will select your answer. Unfortunately I have no clue what you did there... – LeoFr Jun 01 '21 at 11:38
  • @LeoFr I added also the patch for \citet – egreg Jun 01 '21 at 11:58
  • You may want to ask the moderator team to move your answer, which employs the xpatch package, to the earlier query, which so far has only one answer which, in turn, is based on the etoolbox package. That way, readers can compare and contrast the two solution methods more easily. Just a thought. – Mico Jun 02 '21 at 05:42