Search code examples
latexr-markdownbibtex

How to change the fontcolor of reference(author) name from black to blue in rmarkdown document?


I am trying to write a paper using rmarkdown. The following is my rmarkdown code and I want make cited author's name in the text blue-colored. The following codes always generate black colored author's name. I use natbib for citation package with bibtex file. I just wonder what is missing here.

---
title: "Paper" 
output:
  pdf_document:
    keep_tex: yes
    number_sections: yes
    citation_package: natbib
    fig_caption: yes
    fig_height: 4.5
    fig_width: 8
  word_document: 
    fig_caption: yes
thanks: null
link-citations: yes
toc: no
bibliography: /Users/references02.bib
biblio-style: apalike
in_header: preamble.tex
keep_tex: yes
fontsize: 12pt
linestretch: 1.2
subparagraph: yes
number_sections: yes
geometry: margin=1in
header-includes:
- \usepackage{amsmath}
- \usepackage{amsthm}
- \numberwithin{equation}{section}
- \usepackage{makecell}
- \usepackage{geometry}
- \usepackage{graphicx}
- \usepackage{tikz, pgf}
- \usepackage{verbatim}
- \usepackage{chronosys}
- \usepackage{stackengine}
- \usepackage{booktabs, colortbl, xcolor}
- \usepackage{babel}
- \usetikzlibrary{arrows.meta,calc}
- \usepackage[font=small]{caption}
- \usepackage{adjustbox}
- \usepackage{tabularx}
- \usepackage{pgfplots}
- \usepackage[makeroom]{cancel}
- \usetikzlibrary{arrows,decorations.markings}
- \usetikzlibrary{intersections}
- \usepackage[nottoc]{tocbibind}
- \usepackage{filecontents}
- \newtheorem{prop}{Proposition}
- \newtheorem{definition}{Definition}
- \newtheorem{algorithem}{Algorithm}
- \usepackage[T1]{fontenc}
- \usepackage[toc,page]{appendix}
- \usepackage{tabularx,ragged2e,booktabs,caption}
- \newenvironment{rcases}
  {\left.\begin{aligned}}
  {\end{aligned}\right\rbrace}
- \renewcommand{\refname}{References}
- \UseRawInputEncoding
- \usepackage[utf8]{inputenc}
- \usepackage[colorlinks=true,citecolor=blue, urlcolor=blue,linkcolor=blue]{hyperref}
- \usepackage{subfig}
- \usepackage[font=small,labelfont=bf]{caption}
- \usepackage{float}
- \usepackage{flafter}
- \floatstyle{plaintop}
- \restylefloat{table}
- \setlength{\parindent}{2em}
- \setlength{\parskip}{0em}
- \usepackage{adjustbox}
- \usepackage[style=english]{csquotes} 
- \usepackage[nameinlink,capitalize,noabbrev]{cleveref}
- \creflabelformat{equation}{#2\textup{(\textcolor{red}{#1})}#3}
- \creflabelformat{figure}{#2\textcolor{red}{#1}#3}
- \creflabelformat{table}{#2\textcolor{red}{#1}#3}
- \creflabelformat{section}{#2\textcolor{red}{#1}#3}
- \usepackage{dcolumn}
- \usepackage{rotating}
- \usepackage{siunitx}
- \usepackage{arydshln} 
---

In fact, \cite{newton1665} wrote this piece...

I want "Newton(1665)" to be printed as blue color.


Solution

  • For a normal latex document your \usepackage[colorlinks=true,citecolor=blue]{hyperref} would be the correct solution. Unfortunately rmarkdown will forcibly undo your settings by inserting \hypersetup{hidelinks} right before the document starts. You can avoid this by using the citecolor: blue yaml option in your header:

    ---
    title: "Paper" 
    output:
      pdf_document:
        keep_tex: yes
        number_sections: yes
        citation_package: natbib
        fig_caption: yes
        fig_height: 4.5
        fig_width: 8
      word_document: 
        fig_caption: yes
    thanks: null
    link-citations: yes
    toc: no
    bibliography: /Users/references02.bib
    biblio-style: apalike
    in_header: preamble.tex
    keep_tex: yes
    fontsize: 12pt
    linestretch: 1.2
    subparagraph: yes
    number_sections: yes
    geometry: margin=1in
    header-includes:
    - \usepackage{amsmath}
    - \usepackage{amsthm}
    - \numberwithin{equation}{section}
    - \usepackage{makecell}
    - \usepackage{geometry}
    - \usepackage{graphicx}
    - \usepackage{tikz, pgf}
    - \usepackage{verbatim}
    - \usepackage{chronosys}
    - \usepackage{stackengine}
    - \usepackage{booktabs, colortbl, xcolor}
    - \usepackage{babel}
    - \usetikzlibrary{arrows.meta,calc}
    - \usepackage[font=small]{caption}
    - \usepackage{adjustbox}
    - \usepackage{tabularx}
    - \usepackage{pgfplots}
    - \usepackage[makeroom]{cancel}
    - \usetikzlibrary{arrows,decorations.markings}
    - \usetikzlibrary{intersections}
    - \usepackage[nottoc]{tocbibind}
    - \usepackage{filecontents}
    - \newtheorem{prop}{Proposition}
    - \newtheorem{definition}{Definition}
    - \newtheorem{algorithem}{Algorithm}
    - \usepackage[T1]{fontenc}
    - \usepackage[toc,page]{appendix}
    - \usepackage{tabularx,ragged2e,booktabs,caption}
    - \newenvironment{rcases}
      {\left.\begin{aligned}}
      {\end{aligned}\right\rbrace}
    - \renewcommand{\refname}{References}
    - \UseRawInputEncoding
    - \usepackage[utf8]{inputenc}
    - \usepackage[colorlinks=true,citecolor=blue, urlcolor=blue,linkcolor=blue]{hyperref}
    - \usepackage{subfig}
    - \usepackage[font=small,labelfont=bf]{caption}
    - \usepackage{float}
    - \usepackage{flafter}
    - \floatstyle{plaintop}
    - \restylefloat{table}
    - \setlength{\parindent}{2em}
    - \setlength{\parskip}{0em}
    - \usepackage{adjustbox}
    - \usepackage[style=english]{csquotes} 
    - \usepackage[nameinlink,capitalize,noabbrev]{cleveref}
    - \creflabelformat{equation}{#2\textup{(\textcolor{red}{#1})}#3}
    - \creflabelformat{figure}{#2\textcolor{red}{#1}#3}
    - \creflabelformat{table}{#2\textcolor{red}{#1}#3}
    - \creflabelformat{section}{#2\textcolor{red}{#1}#3}
    - \usepackage{dcolumn}
    - \usepackage{rotating}
    - \usepackage{siunitx}
    - \usepackage{arydshln} 
    citecolor: blue
    ---
    
    In fact, \cite{newton1665} wrote this piece...