user2957945
user2957945

Reputation: 2413

How to reference sections in rmarkdown with pdf output

How do I use \label and \ref to link sections in rmarkdown when outputting to pdf please. I have tried various permutations from https://bookdown.org/yihui/bookdown/cross-references.html and https://bookdown.org/yihui/rmarkdown-cookbook/cross-ref.html with no success.

One attempt

---
title: "Untitled"
output: pdf_document
---
See Section \@ref(sec:label).

# Section One  (\#sec:label)

which gives

enter image description here

Upvotes: 3

Views: 2999

Answers (1)

Duck
Duck

Reputation: 39613

You can modify your document as follows:

---
title: "Untitled 1"
date: "28 de junio de 2020"
link-citations: yes
output:
  pdf_document:
    includes:
      keep_tex: yes
    number_sections: yes
    toc_depth: 2
---

\section{This is the first section} \label{section1}

You will be some in \ref{section1}

enter image description here

Then you can see how numbers appear, just add \label{} to your sections and use \ref{} to call. Also, I suggest using \section{}, and modify YAML as I included. Hoping this can help.

Upvotes: 2

Related Questions