Is there a possibility to create an internal link to a table in a rmarkdown-article?
I would like to click on this link and then I automatically come to my table.
I want to set the internal link in the text in the Empirical Results-part for table_1.
I've already tried [#table_1] but it doesn't work.
can someone help me?
Here is my example code:
---
title: "title"
subtitle: "subtitle"
author: "me"
date: "`r format(Sys.time(), '%B %d, %Y')`"
keywords: "keywords"
output:
pdf_document:
fig_cap: yes
keep_tex: yes
documentclass: article
capsize: normalsize
fontsize: 11pt
geometry: margin=1in
spacing: doublespacing
footerdate: yes
abstract: 'Insert abstract here'
---
\newcommand*{\keywords}[1]{\textbf{\textit{Keywords---}} #1}
\keywords{keywords}
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
##required packages
library(tidyverse)
library(knitr)
library(kableExtra)
```
# Introduction
# Literature review
# Data and Methodology
# Empirical Results
In table_1 you can see something....
[#table_1]
# Conclusions
\newpage
# References
\newpage
# Appendix
```{r echo=F, warning=F, message=F}
df1 <- tibble(column1= 1:10, column2=11:20)
table_1 <-df1%>%kable("latex",
booktabs=T,
caption = "example table")%>%
kable_styling(latex_options = "hold_position")
table_1
```
Related
Somehow my RMarkdown document is not crossreferencing tables or figures. Here is a stripped down version of my document.
---
title: "Test"
author: "Me"
date: "01/04/2022"
output: bookdown::pdf_document2
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
var1<-sample(LETTERS)
tab1<-table(var1)
My table is in Table \#ref{tab:tab1}
library(knitr)
kable(tab1, caption="my table")
AS we see in Figure \#ref{fig:plot1}
plot(seq(1,10,1))
You should call your tab1 in the code chunk like this {r tab1}. And use () instead of {} for your #ref. In that case it reference to your figures and tables. You can use the following code:
---
title: "Test"
author: "Me"
date: "01/04/2022"
output: bookdown::pdf_document2
---
My table is in Table \#ref(tab:tab1)
```{r tab1, echo =FALSE}
var1<-sample(LETTERS)
tab1<-table(var1)
library(knitr)
kable(tab1, caption="my table")
```
\newpage
AS we see in Figure \#ref(fig:plot1)
```{r plot1, fig.cap ="plot", echo=FALSE}
par(mar = c(4, 4, .2, .1))
plot(seq(1,10,1))
```
Output:
As you can see on the image, when you click on 1 in will go to your table.
I have monthly report generator that works fine with gt table inside a chunk, but not when the code for the chunk has a external source like the example below.
the main script
rmarkdown::render('report.Rmd', output_file = paste0('report_', i, '.html'))
this way the report.Rmd works fine and prints the gt table
---
title: "Report"
author: "Me"
date: "`r format(Sys.time(), '%d de %B de %Y')`"
output:
html_document
---
## Test
```{r first, echo=FALSE, message=FALSE, results='asis'}
library(tidyverse)
library(gt)
```
```{r second, results='asis', echo=FALSE, message=FALSE}
#source("mtcars_gt.R")
mtcars %>% gt()
```
but this way not
---
title: "Report"
author: "Me"
date: "`r format(Sys.time(), '%d de %B de %Y')`"
output:
html_document
---
## Test
```{r first, echo=FALSE, message=FALSE, results='asis'}
library(tidyverse)
library(gt)
```
```{r second, results='asis', echo=FALSE, message=FALSE}
source("mtcars_gt.R")
```
mtcars_gt.R is just the gt
mtcars %>% gt()
We can use readLines
```{r code = readLines('mtcars_gt.R')}
```
I want to knit an article in RMarkdown but the figures (tables) don't appear at the correct position in the document (pdf).
Here I can show you my problem in the example data. The table should appear after the appendix: Can someone show me how I can solve this problem?
And here is y example code:
---
title: "title"
subtitle: "subtitle"
author: "me"
date: "`r format(Sys.time(), '%B %d, %Y')`"
keywords: "keywords"
output:
pdf_document:
fig_cap: yes
keep_tex: yes
documentclass: article
capsize: normalsize
fontsize: 11pt
geometry: margin=1in
spacing: doublespacing
footerdate: yes
abstract: 'Insert abstract here'
---
\newcommand*{\keywords}[1]{\textbf{\textit{Keywords---}} #1}
\keywords{keywords}
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, fig.pos= "h", out.extra = '')
##required packages
library(tidyverse)
library(knitr)
library(kableExtra)
```
# Introduction
# Literature review
# Data and Methodology
# Empirical Results
# Conclusions
\newpage
# References {-}
<div id="refs"></div>
\newpage
# Appendix
```{r echo=F, warning=F, message=F}
df1 <- tibble(column1= 1:10, column2=11:20)
df1%>%kable("latex",
booktabs=T,
caption = "Dataframe 1")%>%
kable_styling()
```
Just add latex_options=c("hold_position") inside kable_stylying:
df1 <- tibble(column1= 1:10, column2=11:20)
df1 %>%
kable("latex", booktabs=T, caption = "Dataframe 1") %>%
kable_styling(latex_options = c("hold_position"))
I am working in a book, using R and the Tufte library.
I am using the following YAML for each .Rmd Chapter:
---
title: "Chapter 2: A First Linear Program"
header-includes:
\usepackage{longtable}
\usepackage{caption}
output:
tufte::tufte_handout:
citation_package: natbib
latex_engine: xelatex
highlight: monochrome
tufte::tufte_html: default
tufte::tufte_book:
citation_package: natbib
latex_engine: xelatex
---
\pagestyle{headings}
```{r setup, include=FALSE}
library(tufte)
library(tint)
library(knitr)
library(gridExtra)
knitr::opts_chunk$set(echo = TRUE)
knitr::opts_chunk$set(tidy = F)
knitr::opts_chunk$set(cache = TRUE)
knitr::opts_chunk$set(width = 30)
```
And the following one is the YAML for the index .Rmd file:
---
title: "Operations Research Using R<br />"
author: "Timothy R. Anderson"
date: "`r Sys.Date()`"
site: bookdown::bookdown_site
documentclass: book
bibliography: ["Master4Bookdowns.bib"]
---
# Preface {-}
I have tried different options like adding the sansfont and mainfont in YAML:
output:
tufte::tufte_handout:
citation_package: natbib
latex_engine: xelatex
highlight: monochrome
sansfont: Calibri Light
mainfont: Calibri Light
But it didn't change the font inside the chunk, only the text outside.
Here a simpler example document showing my attempt to change monofont as suggested in the comments:
---
title: "Chapter 2: A First Linear Program"
header-includes:
\usepackage{longtable}
\usepackage{caption}
output:
tufte::tufte_handout:
citation_package: natbib
latex_engine: xelatex
toc: TRUE
number_sections: true
highlight: monochrome
monofont: Times New Roman
tufte::tufte_html: default
tufte::tufte_book:
citation_package: natbib
latex_engine: xelatex
---
\pagestyle{headings}
```{r setup, include=FALSE}
library(tufte)
library(tint)
library(knitr)
library(gridExtra)
knitr::opts_chunk$set(echo = TRUE)
knitr::opts_chunk$set(tidy = F)
knitr::opts_chunk$set(cache = TRUE)
knitr::opts_chunk$set(width = 30)
```
This is an example, where we can see that text in the book is affected by the use of command
monofont: Times New Roman in the YAML. However, we can see that next chunk, the oucome when generating the tuffte_handout pdf is using a different font and also using multiple colors (no only black as in the main body).
```{r base_case_no_pipes_step_4}
model0d <- c("example", 6 + 2 <= 2000)
#fabrication
model0e <- c("example", 8 + 6 <= 2000)
#assembly
```
You have to bring the YAML headers to the right level:
monofont et al. are top-level headers
highlight is below the output format, not below output
The following example uses "Times New Roman" for the code block and XeLaTeX's default font (Latin Modern) for the main text. You can change that with mainfont. In addition, there is no syntax highlighting:
---
title: "Chapter 2: A First Linear Program"
header-includes:
- \usepackage{longtable}
- \usepackage{caption}
monofont: Times New Roman
output:
tufte::tufte_handout:
citation_package: natbib
latex_engine: xelatex
toc: TRUE
number_sections: true
highlight: monochrome
tufte::tufte_html: default
tufte::tufte_book:
citation_package: natbib
latex_engine: xelatex
---
\pagestyle{headings}
```{r setup, include=FALSE}
library(tufte)
library(tint)
library(knitr)
library(gridExtra)
knitr::opts_chunk$set(echo = TRUE)
knitr::opts_chunk$set(tidy = F)
knitr::opts_chunk$set(cache = TRUE)
knitr::opts_chunk$set(width = 30)
```
This is an example, where we can see that text in the book is affected by the use of command
monofont: Times New Roman in the YAML. However, we can see that next chunk, the oucome when generating the tuffte_handout pdf is using a different font and also using multiple colors (no only black as in the main body).
```{r base_case_no_pipes_step_4}
model0d <- c("example", 6 + 2 <= 2000)
#fabrication
model0e <- c("example", 8 + 6 <= 2000)
#assembly
```
Result:
Using bookdown to output a .pdf document the YAML within the index.Rmd looks like this currently:
---
title: "My title"
author:
- 'me'
output:
bookdown::pdf_document2:
includes:
in_header: latex/preamble.tex
keep_tex: yes
site: bookdown::bookdown_site
documentclass: book
geometry: "left=3.5cm, right=2.5cm, top=2.5cm, bottom=2.5cm"
fontsize: 12pt
linestretch: 1.5
bibliography: [packages.bib, referencias.bib]
linkcolor: NavyBlue
biblio-style: apalike
link-citations: yes
toc-depth: 2
lof: True
lot: True
---
How can I control fontsize and linestretch of code chunks independently from the main text? This answer provides a solution to control font size, but not line spacing.
It's is the same idea as here but now we just alter the source hook:
```{r setup, include=FALSE}
def.source.hook <- knitr::knit_hooks$get("source")
knitr::knit_hooks$set(source = function(x, options) {
x <- def.source.hook(x, options) # apply default source hook
ifelse(!is.null(options$linestretch), # if linestretch is not NULL, apply linestretch
paste0("\\linespread{", options$linestretch,"}\n", x, "\n\n\\linespread{1}"), # reset linestretch after the chunk!
x)
})
```
Now you can copy and paste the ifelse statement from the other answer into this hook as well and you can control both.
Full example:
---
title: "Linestretch"
date: "20 December 2018"
header-includes:
- \usepackage{lipsum}
output:
bookdown::pdf_document2:
keep_tex: true
linestretch: "`r (lstr <- 1.5)`"
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(eval = F)
def.source.hook <- knitr::knit_hooks$get("source")
knitr::knit_hooks$set(source = function(x, options) {
x <- def.source.hook(x, options)
x <- ifelse(!is.null(options$linestretch),
paste0("\\linespread{", options$linestretch,"}\n", x, "\n\n\\linespread{", lstr,"}"),
x)
ifelse(!is.null(options$size),
paste0("\\", options$size,"\n\n", x, "\n\n \\normalsize"),
x)
})
```
## R Markdown
\lipsum[30]
```{r, linestretch = 1, size="Large"}
head(mtcars)
head(mtcars)
```
\lipsum[30]