Search code examples
rlatexr-markdown

How to change the font for hyperlinks in RMarkdown?


For a report I am writing recently in R Markdown, I would like to change the font for the links I put on the References section. The desired output I would like to have is like the below image;

enter image description here

My YAML front I am using to produce the R markdown document as like the following;

---
title: ""
author:
  - AAA
output:
  pdf_document:
    latex_engine: xelatex
    number_sections: true
geometry: "left = 2.5cm, right = 2.5cm, top = 2.5cm, bottom = 2.5cm"
fontsize: 12 pt
header-includes:
    - \usepackage {ragged2e}
    - \usepackage {titling}
    - \usepackage[nottoc]{tocbibind} 
    - \usepackage{float}
    - \usepackage{sectsty}
    - \usepackage{paralist}
    - \usepackage{setspace}
    - \usepackage{fancyhdr}
    - \usepackage{lastpage}
    - \usepackage{dcolumn}
    - \usepackage{natbib}
    - \usepackage{subcaption}
    - \usepackage{rotating}
    - \usepackage{placeins}
    - \usepackage{float}
    - \floatplacement{figure}{H}
link-citations: true
link-color: red
urlcolor: blue
biblio-style: apsr
linestretch: 2
ident: True
bibliography: ["aaa.bib"]
---

Therefore, I appreciate any comment or suggestion in changing the font type for the hyperlinks in the references section. Thank you for your attention beforehand.


Solution

  • Option 1

    One way could be using \urlstyle from url package. From the manual of the url package - section 3,

    You can switch the style of printing using “\urlstyle{xx}”, where “xx” can be any defined style. The pre-defined styles are “tt”, “rm”, “sf” and “same” which all allow the same linebreaks but use different fonts — the first three select a specific font and the “same” style uses the current text font.

    So you can specify the font that you want to use in monofont yaml key and put this \urlstyle{tt} just before the # References.

    ---
    title: "Changing the fonts for hyperlink/url"
    date: "2022-09-12"
    output:
      pdf_document:
        latex_engine: xelatex
    link-citations: true
    link-color: red
    urlcolor: blue
    biblio-style: apsr
    linestretch: 2
    ident: True
    bibliography: pkg.bib
    monofont: "Comic Sans MS"
    ---
    
    
    ## Packages
    
    - purrr [@R-purrr]
    - tibble [@R-tibble]
    
    
    \urlstyle{tt}
    
    # References
    

    Contents of the bib file I have used here,

    pkg.bib

    @Manual{R-purrr,
      title = {purrr: Functional Programming Tools},
      author = {Lionel Henry and Hadley Wickham},
      year = {2020},
      note = {R package version 0.3.4},
      url = {https://CRAN.R-project.org/package=purrr},
    }
    
    @Manual{R-tibble,
      title = {tibble: Simple Data Frames},
      author = {Kirill MC<ller and Hadley Wickham},
      year = {2020},
      note = {R package version 3.0.3},
      url = {https://CRAN.R-project.org/package=tibble},
    }
    
    

    changed font for urls


    Option 2

    You can also change the fonts for url links, by defining a new command like \urlfont for loading a particular font family with \newfontfamily (read the fontsepc manual, p-13 to know the details) and then use that command in \def\UrlFont{} to define the url font.

    ---
    title: "Changing the fonts for hyperlink/url"
    date: "2022-09-12"
    output:
      pdf_document:
        latex_engine: xelatex
    link-citations: true
    link-color: red
    urlcolor: blue
    biblio-style: apsr
    linestretch: 2
    ident: True
    bibliography: pkg.bib
    header-includes:
      - \usepackage{fontspec}
      - \newfontfamily\urlfont{Roboto Mono}
    ---
    
    
    ## Packages
    
    - purrr [@R-purrr]
    - tibble [@R-tibble]
    
    
    \def\UrlFont{\urlfont}
    
    # References
    
    

    changed url font - option 2


    Do not put the commands \urlstyle{tt} or \def\UrlFont{\urlfont} in header-includes, because if you do that these commands will be overridden by \urlstyle{same} (default style set by r-markdown). So It's best to set these commands just before the reference section.