我正在使用 knitr 生成 pdf 报告。我想做的一件事是将多列添加到表中。我指的是这个链接。问题是如何将帖子中显示的代码放入我的 Rnw 文件中。这是我在我的 Rnw 文件中尝试过的:
\documentclass[a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[english]{babel}
\usepackage{booktabs}
\usepackage{colortbl, xcolor}
\usepackage{tabularx}
\begin{document}
<<echo=FALSE, results = "asis">>=
mx <- matrix(1:6, ncol=3)
rownames(mx) <- LETTERS[1:NROW(mx)]
colnames(mx) <- sprintf("Col %s", LETTERS[1:NCOL(mx)])
latex(mx, n.rgroup=c(2), rgroup=c("Nice!"),
n.cgroup=c(2,1), cgroup=c("First", "Second"),
file="",
ctable=TRUE)
@
\end{document}
我使用 knit('test.Rnw') 将其转换为 tex 文件。这就是 tex 文件的样子:
\documentclass[a4paper]{article}\usepackage[]{graphicx}\usepackage[]{color}
%% maxwidth is the original width if it is less than linewidth
%% otherwise use linewidth (to make sure the graphics do not exceed the margin)
\makeatletter
\def\maxwidth{ %
\ifdim\Gin@nat@width>\linewidth
\linewidth
\else
\Gin@nat@width
\fi
}
\makeatother
\definecolor{fgcolor}{rgb}{0.345, 0.345, 0.345}
\newcommand{\hlnum}[1]{\textcolor[rgb]{0.686,0.059,0.569}{#1}}%
\newcommand{\hlstr}[1]{\textcolor[rgb]{0.192,0.494,0.8}{#1}}%
\newcommand{\hlcom}[1]{\textcolor[rgb]{0.678,0.584,0.686}{\textit{#1}}}%
\newcommand{\hlopt}[1]{\textcolor[rgb]{0,0,0}{#1}}%
\newcommand{\hlstd}[1]{\textcolor[rgb]{0.345,0.345,0.345}{#1}}%
\newcommand{\hlkwa}[1]{\textcolor[rgb]{0.161,0.373,0.58}{\textbf{#1}}}%
\newcommand{\hlkwb}[1]{\textcolor[rgb]{0.69,0.353,0.396}{#1}}%
\newcommand{\hlkwc}[1]{\textcolor[rgb]{0.333,0.667,0.333}{#1}}%
\newcommand{\hlkwd}[1]{\textcolor[rgb]{0.737,0.353,0.396}{\textbf{#1}}}%
\usepackage{framed}
\makeatletter
\newenvironment{kframe}{%
\def\at@end@of@kframe{}%
\ifinner\ifhmode%
\def\at@end@of@kframe{\end{minipage}}%
\begin{minipage}{\columnwidth}%
\fi\fi%
\def\FrameCommand##1{\hskip\@totalleftmargin \hskip-\fboxsep
\colorbox{shadecolor}{##1}\hskip-\fboxsep
% There is no \\@totalrightmargin, so:
\hskip-\linewidth \hskip-\@totalleftmargin \hskip\columnwidth}%
\MakeFramed {\advance\hsize-\width
\@totalleftmargin\z@ \linewidth\hsize
\@setminipage}}%
{\par\unskip\endMakeFramed%
\at@end@of@kframe}
\makeatother
\definecolor{shadecolor}{rgb}{.97, .97, .97}
\definecolor{messagecolor}{rgb}{0, 0, 0}
\definecolor{warningcolor}{rgb}{1, 0, 1}
\definecolor{errorcolor}{rgb}{1, 0, 0}
\newenvironment{knitrout}{}{} % an empty environment to be redefined in TeX
\usepackage{alltt}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[english]{babel}
\usepackage{booktabs}
\usepackage{colortbl, xcolor}
\usepackage{tabularx}
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
\begin{document}
% latex.default(mx, n.rgroup = c(2), rgroup = c("Nice!"), n.cgroup = c(2, 1), cgroup = c("First", "Second"), file = "", ctable = TRUE)
%
\ctable[label=mx,pos=!tbp,]{lrrcr}{}{\FL
\multicolumn{1}{l}{\bfseries mx}&\multicolumn{2}{c}{\bfseries First}&\multicolumn{1}{c}{\bfseries }&\multicolumn{1}{c}{\bfseries Second}\NN
\cline{2-3} \cline{5-5}
\multicolumn{1}{l}{}&\multicolumn{1}{c}{Col A}&\multicolumn{1}{c}{Col B}&\multicolumn{1}{c}{}&\multicolumn{1}{c}{Col C}\ML
{\bfseries Nice!}&&&&\NN
~~A&$1$&$3$&&$5$\NN
~~B&$2$&$4$&&$6$\LL
}
\end{document}
当我运行 tools::texi2pdf('test.tex') 时,我收到了一些错误消息:
texi2dvi(file = file, pdf = TRUE, clean = clean, quiet = quiet, :
'tester3.tex''texi2dvi'
Messages:
/usr/bin/texi2dvi: pdflatex exited with bad status, quitting.
/usr/bin/texi2dvi: see tester3.log for errors.
1: tools::texi2pdf("tester3.tex")
2: texi2dvi(file = file, pdf = TRUE, clean = clean, quiet = quiet, texi2dvi = texi2dvi, texinputs = texinputs, index = index)
3: stop(msg, domain = NA)
有谁知道这是怎么回事?
谢谢!