I tried to put the plot objects in r markdown. The fig.cap worked well in the first r chunk. However problems occurred when I tried to put the second figure. Please see the code below.
The YAML looks like this
output:
bookdown::pdf_document2:
toc: no
header-includes:
- \usepackage{setspace}\doublespacing
- \usepackage{float}
- \usepackage{booktabs}
- \usepackage{longtable}
- \usepackage{amsmath}
- \usepackage{multicol}
- \usepackage{threeparttable}
- \usepackage{caption}
The code in r chunk is
{r trend_1, echo=F, fig.cap='The air pollution trend of different groups',out.width='50%',message=F, fig.show='hold'}
The code for plotting is (these are two plot objects)
state.con.oz
state.con.pm
I got an error ! Package caption Error: \caption outside float. Any ideas?
BTW, I wonder how to create a figure footnote in r markdown?
Thanks in advance.
Try to name your R code chunk without underlines
```{r trend1, echo=F, fig.cap='The air pollution trend of different groups',out.width='50%',message=F, fig.show='hold'}
your_plot
```
Related
I'm trying to do something quite simple: generate reports in PDF format. Finally found a way that reproduces my issue. I need to use rmarkdown::render to create reports based on data in GlobalEnv. I am using the tinytex package. Here is test.Rmd:
---
title: "Untitled"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(kableExtra)
library(tidyverse)
```
## R Markdown
```{r cars}
mtcars %>%
kable(booktabs = TRUE) %>%
kable_styling(latex_options = "striped")
```
Works:
"Knit" in RStudio seems to always work on this file, producing, as expected, the mtcars dataframe, nicely formatted with kable()
Doesn't work (but should?):
Running rmarkdown::render("test.Rmd") works on THE FIRST RUN, but NOT the second. It throws the error:
! LaTeX Error: Unknown float option `H'.
After this, "Knit" in RStudio produces the PDF, but R/knitr prints any warning/error messages from the rmarkdown::render("test.Rmd") command.
Additional Information
Running rmarkdown::render("test.Rmd") does not produce errors if the above code chunk is changed to
```{r cars}
mtcars %>%
kable()
```
I've elected to answer my own question because I've found a work-around that hopefully will not be necessary if someone finds the reason for the errors.
It seems that the PDF rendering engine does not recognize anything but the most basic LaTeX installation of tinytex. I tried tinytex::tlmgr_install to manually install the necessary LaTeX packages but all of them returned a "package already present" message.
Solution
I've added the following to my YAML in my Rmd:
header-includes:
- \usepackage{booktabs}
- \usepackage{float}
- \usepackage{colortbl}
- \usepackage[table]{xcolor}
I essentially added each \usepackage line until I received no errors with the formatting I was looking for.
Working Rmd code
Both rmarkdown::render() and knit (Rstudio) work (and on my OWN code as well!):
---
title: "Untitled"
output: pdf_document
header-includes:
- \usepackage{booktabs}
- \usepackage{float}
- \usepackage{colortbl}
- \usepackage[table]{xcolor}
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(kableExtra)
library(tidyverse)
```
## R Markdown
```{r cars}
mtcars %>%
kable(booktabs = TRUE) %>%
kable_styling(latex_options = "striped")
```
That didn't work for me. I was getting a clash in xcolor option. The solution as pointed out in page 3 : https://haozhu233.github.io/kableExtra/awesome_table_in_pdf.pdf
is to add :
options(kableExtra.latex.load_packages = FALSE)
and in the YAML :
header-includes:
- \usepackage{booktabs}
- \usepackage{longtable}
- \usepackage{array}
- \usepackage{multirow}
- \usepackage{xcolor}
- \usepackage{wrapfig}
- \usepackage{float}
- \usepackage{colortbl}
- \usepackage{pdflscape}
- \usepackage{tabu}
- \usepackage{threeparttable}
- \usepackage{threeparttablex}
- \usepackage[normalem]{ulem}
- \usepackage{makecell}
Note that you can remove any packages that you don't need.
Using Rscript_call solved the problem for me in such cases.
library(rmarkdown)
# doesn't work on second run
render(input = "K:/file.Rmd")
# works on second run
xfun::Rscript_call(
rmarkdown::render,
list(input = 'K:/file.Rmd')
)
I'm currently having an issue with latex in Rmarkdown (using fancyhdr package). I am trying to make a data report with a certain template style. But as of right now, my heading1 for each page is not aligned properly. I assume it has something to do with my \headheight, but I am not sure how to resolve it.
This is currently done in R-markdown, and I am quite confident that the issue has something to do with my YAML. I would be very appreciated if anyone could tell me how to fix my YAML coding for \headheight or overall improving my coding for YAML to fix the overlapping issue.
There are no errors when I run this code, but this is the warnings that they provide me (but I have no idea how to resolve the warning)
Package Fancyhdr Warning: \headheight is too small (12.0pt):
Make it at least 32.08571pt.
We now make it that large for the rest of the document.
This may cause the page layout to be inconsistent, however.
This is my YAML:
---
output:
pdf_document:
latex_engine: pdflatex
header-includes:|
\usepackage{graphicx}
\usepackage{fancyhdr}
\pagestyle{fancy}
\newcommand{\helv}{%
\fontfamily{phv}\fontseries{b}\fontsize{9}{11}\selectfont}
\fancyhead[R]{\includegraphics[width=3cm]{logo.png}}
\fancyhead[L]{\textbf\selectfont\sffamily{Daily Report}}
\fancyfoot[L]{\textbf\selectfont\sffamily{NOT INTENDED FOR FORWARDING}}
\renewcommand{\headrulewidth}{0.4pt}
\renewcommand{\footrulewidth}{0.4pt}
classoption: a4paper
---
And these are in the rest of the codes:
```r-markdown
## \selectfont\sffamily{EQUITY 1}
# content page 1
\newpage
## \selectfont\sffamily{EQUITY 2}
# content page 2
I posted an image of the pdf output on TeXStackExchange: https://tex.stackexchange.com/questions/482525/having-problems-with-latex-coding-in-rmarkdown-fancyhdr-headheight
I would like all of them to be aligned with each other. I do not know if I have done my coding in YAML correctly.
The warning is pretty explicit that the headheight needs to be larger than 32.08571pt. This requirement can be fulfilled by adding e.g. \setlength{\headheight}{32.09pt} to your header-includes.
---
output:
pdf_document:
latex_engine: pdflatex
header-includes:|
\usepackage{graphicx}
\usepackage{fancyhdr}
\pagestyle{fancy}
\newcommand{\helv}{%
\fontfamily{phv}\fontseries{b}\fontsize{9}{11}\selectfont}
\fancyhead[R]{\includegraphics[width=3cm]{logo.png}}
\fancyhead[L]{\textbf\selectfont\sffamily{Daily Report}}
\fancyfoot[L]{\textbf\selectfont\sffamily{NOT INTENDED FOR FORWARDING}}
\renewcommand{\headrulewidth}{0.4pt}
\renewcommand{\footrulewidth}{0.4pt}
\setlength{\headheight}{32.09pt}
classoption: a4paper
---
I have been searching to solve this issue for quite some time, but I cannot get it right. I use R markdown for creating a beamer presentation and I would like to include a footer by using the fancyhdr LaTeX package. The problem that I encounter is that the footer is not completely on each slide. I assume that it has to do something with the margins used by beamer, but I do not know how to change this.
Here is a MWE. This is the .rmd file:
---
title: "Untitled"
author: ""
date: "10 januari 2019"
output:
beamer_presentation:
includes:
in_header: preamble.tex
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
## R Markdown
SOME NICE TEXT
This is in the file "preamble.tex" that is specified in the YAML:
\usepackage{fancyhdr}
\pagestyle{fancy}
\lhead{}
\chead{}
\rhead{}
\lfoot{Text in footer}
\cfoot{}
\rfoot{\thepage}
\renewcommand{\headrulewidth}{0.4pt}
\renewcommand{\footrulewidth}{0.4pt}
This is how it looks. The footer is not entirely on the slide:
Thank you in advance for your help!
When using beamer, it's much more natural to set the footline template than try to use fancyhdr like you'd use with, for example, and article class document (or a pdf_document in R Markdown world). Changing your preamble.tex to the following:
\setbeamertemplate{footline}
{
\leavevmode%
\hbox{%
\begin{beamercolorbox}[wd = .5\paperwidth, ht = 1ex, dp = 1ex, center]{author in head/foot}%
Text in footer
\end{beamercolorbox}%
\begin{beamercolorbox}[wd = .5\paperwidth, ht = 1ex, dp = 1ex, center]{date in head/foot}%
\insertframenumber{}
\end{beamercolorbox}
}%
\vskip3pt%
}
\addtobeamertemplate{footline}{\begin{center}\rule{0.6\paperwidth}{0.4pt}\end{center}\vspace*{-1ex}}{}
results in this output:
which I believe is pretty close to what you want (the black bar there in the middle is just space between the slides in the PDF viewer I was using); you can tweak the footline definition until you get it precisely there.
I am putting together an R Markdown document in HTML, and I have the following YAML:
---
title: "R Markdown Example"
author: "Me"
date: "October 30, 2017"
output:
html_document:
fig_width: 7
fig_height: 6
fig_caption: true
code_folding: "show"
---
I am trying to insert a figure with auto-numbered caption with the following code:
```{r, fig.cap="Figure caption \\label{fig_1}"}
plot(mtcars$hp, mtcars$mpg)
```
But the caption number won't show up. When I try to reference the image in the text
(Figure \ref{fig_1})
I just get:
(Figure )
I have the same problem. This only happens for Word and HTML outputs, so I think I may be using LaTeX documentation instead of the proper input for these types. I've tried a lot of the different recommendations for figure captions, but I can't seem to get any of it to work.
I needed to output to Word to adhere to a journal that only accepted Word... So I ended up using the captioner package. This should also work with html and pdf.
library(captioner)
fig_nums <- captioner()
In my r chunk I put my plot code and then the caption comes directly after the chunk in ticks, using the fig_nums function ``:
```{r}
plot(pressure)
```
`r fig_nums("pressure-plot", "pressure against temperature")`
It looks like this in the Word output:
Then I can refer to it in text like this:
As you can see from the nice visualisation in r fig_nums("pressure-plot", display = "cite") the pressure rises as
temperature rises.
This will show as:
As you can see from the nice visualisation in Figure 1 the pressure rises as
temperature rises
Be sure to run the first time you use, and check-out for more info:
install.packages("captioner")
vignette("using_captioner")
You can use the bookdown::html_document2 format.
---
title: "Untitled"
output: bookdown::html_document2
---
```{r pressure, echo=FALSE, fig.cap='test plot'}
plot(pressure)
```
\#ref(fig:pressure)
I've been trying for ages to find a way to get my table to shrink enough to fit on a slide. The base code looks like:
---
title: "Untitled"
output: beamer_presentation
---
```{r setup, include = FALSE}
library(knitr)
library(kableExtra)
```
## Table
The table below is too long.
```{r}
kable(summary(mtcars), format = "latex")
```
If I try adding %>% kable_styling(full_width = TRUE) or anything like that, I get this error:
! LaTeX Error: Environment tabu undefined.. But I don't get it when I change the output to pdf_document.
Is there anyway to make tables that are too wide fit into a beamer presentation? Or will I always be limited to about 5/6 columns?
For your specific task, I think you should use `kable_styling(latex_options = "scale_down").
===
If you want to use other features in kableExtra, put the following things in your yaml header. You can find some extra explanation at the very beginning (on Page 2 right now) of the package manual(http://haozhu233.github.io/kableExtra/awesome_table_in_pdf.pdf).
header-includes:
- \usepackage{booktabs}
- \usepackage{longtable}
- \usepackage{array}
- \usepackage{multirow}
- \usepackage{wrapfig}
- \usepackage{float}
- \usepackage{colortbl}
- \usepackage{pdflscape}
- \usepackage{tabu}
- \usepackage{threeparttable}