如果已经提出这个问题或者样本无法重现,首先要道歉。我对 LaTex 完全陌生,很乐意接受任何建议。
我正在使用.tex
大学提供的 LaTex 模板撰写论文,.rnw
由于我想包含一些 R 结果,所以我将其保存为文件。在我添加代码之前,该文件可以很好地编织并生成 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}
(我不知道如何在.rnw
文件中包含参考,但我的.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},}
然而,在我添加之后:
<<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"))
@
该文件无法再被 knitr 了。它产生的错误包括“tkh1998
输入行 86 上的引用未定义”(我甚至没有行 86!!)、“输入行 89 上的颜色定义不兼容”、“未定义的控制序列”。
我很感激任何关于将 LaTeX 输出和 R 结果整合在一起的建议(特别是 KableExtra 输出),我应该主要写在.tex
还是.rnw
等等。
答案1
欢迎使用 TeX.SE。此答案建议您使用biblatex
软件包并biber
生成引文和参考书目。然后,答案假定您正在使用RStudio
编辑器并编译.Rnw
文件。
biber
从 RStudio 内部执行的答案如下:如何将参考文献和参考书目插入使用 RStudio R Sweave 和 knitr 生成的 .Rnw 文件中?
您需要添加此代码块:
<<biber, eval= TRUE, include= FALSE, cache= FALSE, echo= FALSE>>=
system (paste ("biber", sub ("\\.Rnw$", "", current_input())))
@
这将调用biber
生成.bbl
参考书目所需的文件。请注意,RStudio
编译结束时会自动删除此文件,因此您看不到它。
对文件进行两次编译.Rnw
以生成引文和参考书目。
我删除了此 MWE 不需要的包。但是,由于您有选项,因此kable
您booktabs = T
需要使用 加载booktabs
包\usepackage{booktabs}
。MWE 中使用的另一个kable
选项是striped
。这使用\rowcolor
来自xcolor
包,因此您还需要加载它。最后,表格不是使用 longtable 环境设置的,因此请写入。当表格跨越多页时,longtable = F
您需要这样做。longtable = T
这是输出:
这是 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}