Thesisdown: knitting to PDF fails when loading huxtable package and creating tables - r

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}

Related

How to add logo to top right of PDF document

I am making a pdf document in which I want two logos in the top right of the page. I found great examples of using the latex 'fancy' package. When I replicate the example I receive this error ! LaTeX Error: File 'fancyhdr.sty' not found. I am new to latex and have only used in Rstudio when knitting documents. How do I add this file or how do I install the latex package 'fancy'. I am using a 2017 macbook pro and making this document in R Studio.
---
title: "Test"
date: "Updated: `r format(Sys.time(), '%Y-%m-%d')`"
output: pdf_document
urlcolor: blue
indent: true
header-includes:
- \usepackage{indentfirst}
- \usepackage{graphicx}
- \usepackage{fancyhdr}
- \pagestyle{fancy}
- \fancyhead[R]{\includegraphics[width=2cm]{ribbitr.png}}
- \fancyhead[L]{\includegraphics[width=2cm]{nsf_logo.png}}
- \renewcommand{\headrulewidth}{0.2pt}
- \renewcommand{\footrulewidth}{0.2pt}
---

I am getting error "command \bibname unspecified" while compiling a latex document in R markdown

I am trying to create an R Markdown Template on the basis of a LaTeX template I found on Overleaf. The template preamble uses many packages. I am trying to compile this latex file in RStudio using tinytex latex compiler, and during this process, I am getting error "Latex Error: Command \bibname unspecified. I am attaching both, the preamble from overleaf, and the YAML specification from R Markdown.
Overleaf Preamble
\documentclass[twoside]{report} %[twoside, 14pt]
% ==== MY BUNCH OF PACKAGES ====
\usepackage[a4paper,top=3cm,bottom=3cm,left=2.5cm,right=2.5cm,bindingoffset=5mm]{geometry}
\usepackage[english]{babel}
\usepackage{textcomp}
\usepackage{graphicx}
\usepackage{wrapfig}
\usepackage{multirow}
\usepackage{float}
\usepackage{amsmath}
\usepackage{latexsym}
\usepackage{amssymb}
\usepackage{caption}
\captionsetup{font=footnotesize}
\usepackage{subcaption}
\usepackage{tabularx}
%\usepackage{mathabx}
\usepackage{subfiles}
\newcommand\quoteref[1]{\csname#1\endcsname}%
\usepackage{glossaries}
\usepackage[toc,page]{appendix}
\usepackage{rotating}
\usepackage{listings}
\usepackage{xcolor}
\usepackage{changepage} % for the adjust width environment
\usepackage{setspace}
% ----------- code environment settings
\definecolor{codegreen}{rgb}{0,0.6,0}
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\definecolor{codepurple}{rgb}{0.58,0,0.82}
\definecolor{backcolour}{rgb}{0.95,0.95,0.92}
\lstdefinestyle{mystyle}{
backgroundcolor=\color{backcolour},
commentstyle=\color{codegreen},
keywordstyle=\color{magenta},
numberstyle=\tiny\color{codegray},
stringstyle=\color{codepurple},
basicstyle=\ttfamily\footnotesize,
breakatwhitespace=false,
breaklines=true,
captionpos=b,
keepspaces=true,
numbers=left,
numbersep=5pt,
showspaces=false,
showstringspaces=false,
showtabs=false,
tabsize=2
}
\lstset{style=mystyle}
% ---------------- chapter abstracts
\newenvironment{chapabstract}
{
\begin{center}
\noindent\makebox[0.8\linewidth]{\rule{0.8\paperwidth}{0.4pt}}
\end{center}
\vspace{2mm}
\list{}{
\setlength{\leftmargin}{1cm}
\setlength{\rightmargin}{\leftmargin}
}
\item\relax
}
{\par}
\makeatother
% ------------- hyperref
%\biboptions{authoryear,square,sort&compress}
\usepackage[colorlinks=true,linkcolor=blue,citecolor=blue]{hyperref}
% -------------- journals shortcuts
\newcommand{\aaa}{Anonymous Ambiguous Abstracts}
\newcommand{\bbb}{Boring Bibliographic Book}
% ----------- glossary entries for summary of acronyms
\makenoidxglossaries
\newglossaryentry{AAA}{name=AAA, description={Ambitious Amicable Ants}}
\newglossaryentry{BBB}{name=BBBB, description={Babbling Bumbling Band of Baboons}}
\newglossaryentry{CCC}{name=CCC, description={Condescending Critical Cucumber}}
\newglossaryentry{BFG}{name=BFG, description={Big Friendly Giant}}
% ==== TEMPLATE PACKAGES ====
\usepackage[utf8]{inputenc} % allow utf-8 input
\usepackage[T1]{fontenc} % use 8-bit T1 fonts
\usepackage{lmodern}
\usepackage{hyperref} % hyperlinks
\usepackage{url} % simple URL typesetting
\usepackage{booktabs} % professional-quality tables
\usepackage{amsfonts} % blackboard math symbols
\usepackage{nicefrac} % compact symbols for 1/2, etc.
\usepackage{microtype} % microtypography
\usepackage{lipsum} % fill with latin nonsense
YAML Specification in R Markdown
title: "Ambra2"
author: "Maulik Bhatt"
date: "`r Sys.Date()`"
output:
bookdown::pdf_document2:
template: Ambra.tex
keep_tex: true
Bibliography: references.bib
What am I doing wrong here?

Is there a proper way to include images in the margins a an R Bookdown project?

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)
```

In RStudio, `knit` always works, but `rmarkdown::render` fails on second run (but not first!)

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')
)

R markdown with Beamer: table too wide

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}

Resources