3

I have been experimenting with the MonteCarlo package in R for a project. After using the maketable function, I was given the following code to generate a LaTex table with the Monte Carlo simulation's results.

\begin{table}[h]
\centering
\resizebox{ 1 \textwidth}{!}{%
\begin{tabular}{ rrrrrrrrr }
\hline\hline\\\\
scale & n/loc &  & 0 & 0.2 & 0.4 & 0.6 & 0.8 & 1 \\ 
 &  &  &  &  &  &  &  &  \\ 
\multirow{ 4 }{*}{ 1 } & 50 &  & 0.06 & 0.31 & 0.80 & 0.99 & 1.00 & 1.00 \\ 
 & 100 &  & 0.06 & 0.54 & 0.98 & 1.00 & 1.00 & 1.00 \\ 
 & 250 &  & 0.05 & 0.89 & 1.00 & 1.00 & 1.00 & 1.00 \\ 
 & 500 &  & 0.04 & 0.99 & 1.00 & 1.00 & 1.00 & 1.00 \\ 
 &  &  &  &  &  &  &  &  \\ 
\multirow{ 4 }{*}{ 2 } & 50 &  & 0.05 & 0.11 & 0.33 & 0.59 & 0.81 & 0.93 \\ 
 & 100 &  & 0.05 & 0.14 & 0.52 & 0.83 & 0.97 & 1.00 \\ 
 & 250 &  & 0.05 & 0.36 & 0.89 & 1.00 & 1.00 & 1.00 \\ 
 & 500 &  & 0.06 & 0.66 & 0.99 & 1.00 & 1.00 & 1.00 \\ 
\\
\\
\hline\hline
\end{tabular}%
}
\caption{ decision  }
\end{table}

This code had numerous error messages when I try to compile it in Texstudio. Browsing around the forum, I found that by adding a \documentclass{article} and \begin{document}. command at the beginning with the appropriate \end commands at the end, a majority of the errors resolved themselves. However, I still have five errors that are popping up in the editor that are preventing compilation. Those errors are:

line 5 - Undefined control sequence. \resizebox
line 5 - Missing number, treated as zero. \resizebox { 1 \ textwidth}
line 5 - Illegal unit of measure (pt inserted). \resizebox { 1 \ textwidth}
line 10 - Undefined control sequence. \multirow
line 15 - Undefined control sequence. \multirow

Any ideas about how I can resolve these errors would be highly appreciated.

3

Welcome to TeX.SE. I encourage you to read a tutorial on RStudio, knitr, and Markdown, like this one: https://github.com/sahirbhatnagar/knitr-tutorial. The home of all things on knitr is here: https://yihui.name/knitr/.

First, in RStudio, you need to create a file with an .Rnw extension. This file includes your LaTeX code, and chunks of R code. Compile the file with pdfLaTeX or XeLaTeX. This will execute your code chunks, the output from which will be included in a .tex file together with any LaTeX formatting instructions you may have given. The product is a pdf file that includes the output of any LaTeX code you have written together with the output of executing the R code.

All you have at the moment is a code fragment for including in a .tex file that you need to write. That file will start with \documentclass{article} (or some other class like book or report) and end with \end{document}.

Here is an example, let's call it Fledgling.Rnw from one of @Fran's great answers:

\documentclass{article}
\begin{document}
<<mytable,echo=FALSE,results='asis'>>=
library(xtable)
a <- matrix(c(1,2,3,4,5,6,7,8,9),nrow=3)
xtable(a)
@
\end{document}

The R code chunk is placed between <<>>== and @. Here, it loads xtable. library is the equivalent of \usepackage in LaTeX. A matrix, a, is created, which is then formatted using LaTeX syntax by executing xtable. A file called Fledling.tex will be created at the conclusion of executing the R code. This is made possible using the R package, knitr. No user intervention is required. All you need is RStudio and a .Rnw file. With the .tex file created, pdfLaTeX or XeLaTeX take over and compile Fledgling.tex to produce Fledgling.pdf.

3

You need the graphicx package for \resizebox and the multirow package for \multirow, so this works:

\documentclass[a4paper]{article}
\usepackage{graphicx,multirow}
\begin{document}
\begin{table}[h]
\centering
\resizebox{ 1 \textwidth}{!}{%
\begin{tabular}{ rrrrrrrrr }
\hline\hline\\\\
scale & n/loc &  & 0 & 0.2 & 0.4 & 0.6 & 0.8 & 1 \\ 
 &  &  &  &  &  &  &  &  \\ 
\multirow{ 4 }{*}{ 1 } & 50 &  & 0.06 & 0.31 & 0.80 & 0.99 & 1.00 & 1.00 \\ 
 & 100 &  & 0.06 & 0.54 & 0.98 & 1.00 & 1.00 & 1.00 \\ 
 & 250 &  & 0.05 & 0.89 & 1.00 & 1.00 & 1.00 & 1.00 \\ 
 & 500 &  & 0.04 & 0.99 & 1.00 & 1.00 & 1.00 & 1.00 \\ 
 &  &  &  &  &  &  &  &  \\ 
\multirow{ 4 }{*}{ 2 } & 50 &  & 0.05 & 0.11 & 0.33 & 0.59 & 0.81 & 0.93 \\ 
 & 100 &  & 0.05 & 0.14 & 0.52 & 0.83 & 0.97 & 1.00 \\ 
 & 250 &  & 0.05 & 0.36 & 0.89 & 1.00 & 1.00 & 1.00 \\ 
 & 500 &  & 0.06 & 0.66 & 0.99 & 1.00 & 1.00 & 1.00 \\ 
\\
\\
\hline\hline
\end{tabular}%
}
\caption{ decision  }
\end{table}
\end{document}

That said, I think resizing that table is a bit pointless, so I would rather do something like this:

\documentclass[a4paper]{article}
\usepackage{booktabs,multirow}
\begin{document}
\begin{table}
\centering
\begin{tabular}{ rrrrrrrrr }
\toprule
scale & n/loc &  & 0 & 0.2 & 0.4 & 0.6 & 0.8 & 1 \\ \midrule
\multirow{ 4 }{*}{ 1 } & 50 &  & 0.06 & 0.31 & 0.80 & 0.99 & 1.00 & 1.00 \\ 
 & 100 &  & 0.06 & 0.54 & 0.98 & 1.00 & 1.00 & 1.00 \\ 
 & 250 &  & 0.05 & 0.89 & 1.00 & 1.00 & 1.00 & 1.00 \\ 
 & 500 &  & 0.04 & 0.99 & 1.00 & 1.00 & 1.00 & 1.00 \\ \addlinespace
\multirow{ 4 }{*}{ 2 } & 50 &  & 0.05 & 0.11 & 0.33 & 0.59 & 0.81 & 0.93 \\ 
 & 100 &  & 0.05 & 0.14 & 0.52 & 0.83 & 0.97 & 1.00 \\ 
 & 250 &  & 0.05 & 0.36 & 0.89 & 1.00 & 1.00 & 1.00 \\ 
 & 500 &  & 0.06 & 0.66 & 0.99 & 1.00 & 1.00 & 1.00 \\ 
\bottomrule
\end{tabular}%
\caption{ decision  }
\end{table}
\end{document}

The booktabs package provides the macros \toprule, \midrule, \bottomrule and \addlinespace.

  • 1
    Obviously, if the table changes from time to time, editing the code isn't ideal. – Torbjørn T. Oct 19 at 6:24

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.