2

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.

1

Welcome to TeX.SE. This answer suggests that you use the biblatex package and biber to produce your citations and bibliography. The answer then assumes you are using RStudio 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:

<<biber, eval= TRUE, include= FALSE, cache= FALSE, echo= FALSE>>=
system (paste ("biber", sub ("\\.Rnw$", "", current_input())))
@

This will call biber to produce the .bbl file required for producing a bibliography. Note that RStudio 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 option booktabs = T you will need to load the booktabs package with \usepackage{booktabs}. Another kable option used in the MWE is striped. This uses \rowcolor from the xcolor package, so you also need to load that as well. Finally, the table is not being set using the longtable environment, so write longtable = F. You need longtable = T when the table will span more than one page.

This is output:

enter image description here

This is the MWE;

\begin{filecontents}{\jobname.bib}
@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},}
}
\end{filecontents}

\documentclass{article}
\usepackage{booktabs}         % reqired for booktabs=T
\usepackage[backend= biber]{biblatex}
\usepackage[table]{xcolor}    % required for "striped"
\addbibresource{\jobname.bib}

\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 = F, booktabs = T) %>%
  kable_styling(latex_options = c("striped", "hold_position", "repeat_header"))
@

\appendix

<<biber, eval= TRUE, include= FALSE, cache= FALSE, echo= FALSE>>=
system (paste ("biber", sub ("\\.Rnw$", "", current_input())))
@

\printbibliography

\end{document}
  • Thanks so much, Ross! Would it be possible to use an external .bib file for bibliography? For instance, \bibliography{job} where job.bib is located under the same folder as .rnw file. (When I made above changes on your basis, it generated an error saying undefined control sequence... – Rachel Zhang Jun 29 at 14:41
  • Rachel. This setup for the MWE uses \begin{filecontents} \end{filecontents} to make the example complete and portable. You can remove this part since you already have your .bib file. You can have any *.bib file in your working directory and just reference that file using addbibresource{bibfilename.bib}. The undefined control sequence indicates a command is not recognized. Was there additional information with that error to provide a clue about what command was not recognized? Can you compile the example exactly as posted, with just this one bibentry? – Ross Jun 29 at 16:04
  • 1
    Hi Ross, never mind, I have a few packages out of date and now it worked out perfectly. Thanks so much! – Rachel Zhang Jun 30 at 2:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.