I am using knitr to produce pdf reports. One thing I am trying to do is to add multicloumns to a table. I am referting to this link. The question is how can I put the code showed in the post in my Rnw file. This is what I've tried in my Rnw file:
\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}
and I use knit('test.Rnw') to convert it to tex file. And this is how the tex file looks like:
\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}
and when I run tools::texi2pdf('test.tex'), I got some error message:
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)
Anyone knows how this happened?
thanks!