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}
Related
I am working towards writing a textbook using R Bookdown, and I want to include pictures in the margins of the text. However, after trying multiple methods none of them seem to actually populate the image in the correct place. I have tried all the options in r bookdown manual and the instructions for using tufte style https://bookdown.org/yihui/rmarkdown/tufte-figures.html.
To make sure I am not having problems with conflicting packages I have been creating a fresh project then pasting the following code into the end of the index.Rmd file.
{r fig-margin, fig.margin=TRUE}
knitr::include_graphics('apple.png')
^![](C:\Users\n298l621\Documents\TESTING\apple.png)
{marginfigure, echo = TRUE}
knitr::include_graphics('apple.png')
The following image is the result of me either knitting the index.Rmd document or building the whole book using the build menu:
As you can see, the images are populating but certainly not in the margin. Any help or guidance would be greatly appreciated!
I do not know how to use r chunk inserting picture in the margin. Here is my document using latex in Rmarkdown insert the compamy logo in left-top margin.
---
documentclass: ctexart
fontsize: 12pt
geometry: a4paper,left=2cm,right=2cm,top=2cm,bottom=3cm
pagesize: a4paper
output:
rticles::ctex:
fig_caption: yes
number_sections: yes
toc: no
mainfont: 更纱黑体 UI SC #LiberationSerif
sansfont: 更纱黑体 UI SC Xlight #LiberationSans
monofont: 等距更纱黑体 SC Light #LiberationMono
header-includes:
- \usepackage{fancyhdr} #load fancyhdr for page header or foot
- \usepackage{booktabs}
# - \usepackage{longtable}
- \usepackage{array}
- \usepackage{multirow}
- \usepackage{wrapfig}
- \usepackage{float}
- \usepackage{colortbl}
- \usepackage{pdflscape}
- \usepackage{tabu}
- \usepackage{threeparttable}
- \usepackage{threeparttablex}
- \usepackage[normalem]{ulem}
- \usepackage{makecell}
- \usepackage{xeCJK}
---
\addtolength{\headheight}{1.5cm}
\pagestyle{fancyplain}
\fancyhead[L]{\includegraphics[height=1.2cm]{cdmlogo.png}} # put the logo at top-left
\fancyhead[C]{打印日期\today}
\fancyfoot[C]{\thepage}
\renewcommand{\headrulewidth}{0pt}
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r include=FALSE, label='oracle.connection'}
library(tidyverse)
library(ROracle)
library(kableExtra)
Could you include an example of the yaml front matter which you are using in your .Rmd?
A document produced with the following code:
---
title: "An MWE of tufte"
author: "chefnate119"
output:
tufte::tufte_html: default
---
```{r fig-margin, fig.margin=TRUE}
plot(cars)
```
Produces the following document
and to a pdf
---
title: "An MWE of tufte"
author: "chefnate119"
output:
tufte::tufte_handout: default
---
```{r fig-margin, fig.margin=TRUE}
plot(cars)
```
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
```
I want to create tables with the huxtable package in documents created with the thesisdow package. Knitting to html works well with HTML. But knitting but to PDF fails with the following error as soon as a table is included after loading huxtable:
! LaTeX Error: Environment threeparttable undefined.
There seems to be a clash with LaTex packages in the background.
Below a MWE for the thesisdown index.file.
Interestingly, in a simple markdown file knitting both to html and PDF works without problems.
Since thesisdown is built on bookdown it is possible that the error also occurs when knitting bookdown files to PDF.
index.Rmd
---
knit: "bookdown::render_book"
site: bookdown::bookdown_site
output:
thesisdown::thesis_pdf: default
# thesisdown::thesis_gitbook: default
---
```{r include_packages, include = FALSE}
# Ensure that the thesisdown package is installed and loaded
if(!require(devtools))
install.packages("devtools", repos = "http://cran.rstudio.com")
if(!require(thesisdown))
devtools::install_github("ismayc/thesisdown")
library(thesisdown)
```
# Random Test Output - *before* calling "library(huxtable)"
a table with pressure data
```{r pressure-table, echo=FALSE}
pressure
```
```{r echo=FALSE}
library(huxtable)
```
# Random Test Output - *after* calling "library(huxtable)"
a table with pressure data
```{r pressure-table2, echo=FALSE}
pressure
```
MWE summary:
knitting to HTML works always
knitting to PDF works only if there are no tables after "library(huxtable)", i.e. when the code chunk "pressure-table2" is not executed
UPDATE:
(1) If working with thesisdown, including the LaTex packages required by huxtable in the YAML-header as proposed by #dash2 works well.
(2) However, if working with huskydown, the issue unfortunately remains.
1. index.Rmd (thesisdown)
---
knit: "bookdown::render_book"
site: bookdown::bookdown_site
output:
thesisdown::thesis_pdf: default
# thesisdown::thesis_gitbook: default
header-includes:
- \usepackage{array}
- \usepackage{caption}
- \usepackage{graphicx}
- \usepackage{siunitx}
- \usepackage{colortbl}
- \usepackage{multirow}
- \usepackage{hhline}
- \usepackage{calc}
- \usepackage{tabularx}
- \usepackage{threeparttable}
- \usepackage{wrapfig}
---
...
2. index.Rmd (huskydown)
---
# UW thesis fields
title: "My thesis title - edit in index.Rmd"
author: "My Name"
year: "2017"
program: "My Department"
chair: "Name of my committee chair"
chairtitle: "Title of my chair"
signature1: "person 1"
signature2: "person 2"
signature3: "person 3"
abstract: |
"Here is my abstract"
acknowledgments: |
"My acknowledgments"
dedication: |
"My dedication"
knit: "bookdown::render_book"
site: bookdown::bookdown_site
output:
huskydown::thesis_pdf:
latex_engine: xelatex
bibliography: bib/thesis.bib
csl: csl/apa.csl
lot: true
lof: true
header-includes:
- \usepackage{tikz}
- \usepackage{array}
- \usepackage{caption}
- \usepackage{graphicx}
- \usepackage{siunitx}
- \usepackage{colortbl}
- \usepackage{multirow}
- \usepackage{hhline}
- \usepackage{calc}
- \usepackage{tabularx}
- \usepackage{threeparttable}
- \usepackage{wrapfig}
---
```{r include_packages, include = FALSE}
# This chunk ensures that the huskydown package is
# installed and loaded. This huskydown package includes
# the template files for the thesis.
if(!require(devtools))
install.packages("devtools", repos = "http://cran.rstudio.com")
if(!require(huskydown))
devtools::install_github("benmarwick/huskydown")
library(huskydown)
```
# Random Test Output - *before* calling "library(huxtable)"
a table with pressure data
```{r pressure-table, echo=FALSE}
pressure
```
```{r echo=FALSE}
library(huxtable)
```
# Random Test Output - *after* calling "library(huxtable)"
a table with pressure data
```{r pressure-table2, echo=FALSE}
pressure
```
Despite loading LaTex packages, knitting the index.Rmd for huskydown again throws the error:
! LaTeX Error: Environment threeparttable undefined.
Are any LaTex packages loaded with header-includes potentially ignored by huskydown?
Solution as recommended by #dash2: include LaTex packages manually
"LaTex packages loaded with header-includes ignored by huskydown?" - it seems that this presumption is actually the case.
Hence, the LaTex packages can not be included in the YAML header of the index.Rmd file.
The huskydown thesis template includes the file "template.tex" which defines the documentclass and sources several LaTex packages.
Manually adding all LaTex packages required by huxtable to this file solves the issue:
%-------------------------------
% Huxtable
%-------------------------------
% load LaTex packages needed for huxtable
\usepackage{array}
\usepackage{caption}
\usepackage{graphicx}
\usepackage{siunitx}
\usepackage{colortbl}
\usepackage{multirow}
\usepackage{hhline}
\usepackage{calc}
\usepackage{tabularx}
\usepackage{tabulary}
\usepackage{threeparttable}
\usepackage{wrapfig}
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 working on an RMarkdown pdf document and am trying to convert a table formatted in html using the FlexTable object of the RepoRters package and want to convert it into latex so that I can ouput the table into a pdf.
Is there a way to do this?
Thanks!
Rafael
This is an example that works on my machine. Be sure to include the header-includes block in the YAML content.
---
title: "lazyWeave Example"
output: pdf_document
header-includes:
- \usepackage{xcolor}
- \usepackage{graphicx}
- \usepackage{colortbl}
---
After running `install.packages("lazyWeave")` the following should produce the desired table.
```{r}
library(lazyWeave)
options(lazyReportFormat = "latex")
```
```{r, results='asis'}
lazy.matrix(mtcars[1:10, ],
rcol = c(TRUE, FALSE),
usecol = "lightgray")
```