Apology at first if this question has been asked or the sample is not reproducible. I'm totally new to LaTex and happy to take any advice.
I am working on my thesis on a .tex
LaTex template given by the university, and I just saved it as a .rnw
file since I would like to include some R results. Before I added code, the file can knitr well and generate a pdf.
\documentclass[master,tocprelim]{cornell}
\usepackage{graphicx,pstricks}
\usepackage{graphics}
\usepackage{moreverb}
\usepackage{subfigure}
\usepackage{epsfig}
\usepackage{hangcaption}
\usepackage{txfonts}
\usepackage{palatino}
\graphicspath{ {figures/} }
\begin{document}
He said it is cool\cite{tkh1998}.
<<code, echo=FALSE, message=FALSE, warning=FALSE>>=
library(kableExtra)
library(tidyverse)
library(dplyr)
data = data.frame(matrix(rnorm(20, 10, 2), ncol=4))
kable(data, 'latex', caption = 'Time series Errors',
longtable = T, booktabs = T) %>%
kable_styling(latex_options = c("striped", "hold_position", "repeat_header"))
@
\appendix
%\bibliography{LR}
\end{document}
(I don't know how to include reference in the .rnw
file, but my .bib
file include this citation:
@ARTICLE{tkh1998,
author={ {Tin Kam Ho}},
journal={IEEE Transactions on Pattern Analysis and Machine Intelligence},
title={The random subspace method for constructing decision forests},
year={1998},
volume={20},
number={8},
pages={832-844},
keywords={pattern classification;trees (mathematics);decision theory;random processes;learning (artificial intelligence);random subspace method;decision forests;decision trees;overfitting;maximum accuracy;decision tree based classifier;generalization accuracy;feature vector;classification accuracy;Decision trees;Classification tree analysis;Training data;Clustering algorithms;Tin;Stochastic systems;Binary trees;Support vector machines;Support vector machine classification},
doi={10.1109/34.709601},
ISSN={0162-8828},
month={Aug},}
However, after I added:
<<code, echo=FALSE, message=FALSE, warning=FALSE>>=
library(kableExtra)
library(tidyverse)
library(dplyr)
data = data.frame(matrix(rnorm(20, 10, 2), ncol=4))
kable(data, 'latex', caption = 'Time series Errors',
longtable = T, booktabs = T) %>%
kable_styling(latex_options = c("striped", "hold_position", "repeat_header"))
@
This file cannot be knitr anymore. It produced errors including "citation tkh1998
undefined on input line 86" (I don't even have line 86!!), "Incompatible color definition on input line 89", "Undefined control sequence".
I appreciate any suggestion about integrating LaTeX output and R results together (especially KableExtra outputs), shall I mainly write in .tex
or .rnw
, etc.
Best Answer
Welcome to TeX.SE. This answer suggests that you use the
biblatex
package andbiber
to produce your citations and bibliography. The answer then assumes you are usingRStudio
as your editor and to compile your.Rnw
file.Executing
biber
from within RStudio was answered here: How to insert references and bibliography into a .Rnw file produce with RStudio R Sweave and knitr?You need to add this code chunk:
This will call
biber
to produce the.bbl
file required for producing a bibliography. Note thatRStudio
automatically deletes this file at the end of the compilation so you cannot see it.Compile the
.Rnw
file twice to produce the citations and bibliography.I removed packages not required for this MWE. However, since you have the
kable
optionbooktabs = T
you will need to load thebooktabs
package with\usepackage{booktabs}
. Anotherkable
option used in the MWE isstriped
. This uses\rowcolor
from thexcolor
package, so you also need to load that as well. Finally, the table is not being set using the longtable environment, so writelongtable = F
. You needlongtable = T
when the table will span more than one page.This is output:
This is the MWE;