Table caption misplaced and no list of tables generated - latex

I am writing an APA-style document in RMarkdown using Papaya package, which compiles into LaTeX, and then to pdf, by pandoc. I know a small amout of LaTeX, and I am not familiar with RMarkdown, and was asked to use it. So I have two problems here, and thank you in advance for any help. The problems might be related to Rmarkdown/Papaya, but they could also be purely LaTeX related, so even if you don't know about RMarkdown, you may still know something.
The first problem. I have the following table in my RMarkdown file,
\begin{center}
\begin{tabular}{| c | c | c | c | c | c | c |}
\hline
Model & Composability & Unboundness & Comm. & Sync. & Distribution & Mobility\\ \hline
$\pi$ & explicit & explicit & shared-mem & sync & NA & NA \\ \hline
Join & explicit & implicit & shared-mem & sync & implicit & implicit\\ \hline
Ambient & explicit & explicit & shared-mem & sync & explicit & explicit\\ \hline
Actor & NA & implicit & message-pass & async & implicit & implicit \\ \hline
\end{tabular}
\captionof{table}{Summary of Properties of Concurrency Models}\label{summary}
\end{center}
I was wondering how I can make the caption one-line and centered in the generated pdf file? It now spans two lines and is not centered (see this screenshot).
The second problem. I also would like to generate a list of tables after tables of contents. When I add \listoftables to the beginning part of the RMarkdown file together with other LaTeX command, \listoftables is missing from the generated .tex file, but the other commands shown below such as \newcommand are preserved in the .tex file.
```{r analysis-preferences}
# Seed for random number generation
set.seed(42)
knitr::opts_chunk$set(cache.extra = knitr::rand_seed)
```
\listoftables
\newcommand{\defeq}{\vcentcolon=}
\newcounter{equationset}
\newcommand{\equationset}[1]{% \equationset{<caption>}
\refstepcounter{equationset}% Step counter
\noindent\makebox[\linewidth]{Equation set~\theequationset: #1}}
\lstset{
basicstyle=\ttfamily,
mathescape
}
Then I notice there is an option at the beginning of the RMarkdown file
tablelist : no
and I change it to
tablelist : yes
but I get the following new compilation warning:
Warning message:
Package tocloft Warning: \#starttoc has already been redefined; tocloft bailing
out. on input line 1147.
The LaTeX packages that I included in the RMarkdown file are:
header-includes:
- \usepackage{amsmath}
- \usepackage{mathtools}
- \usepackage{listings}
- \usepackage{caption}

Related

Import table from another latex file

Is it possible to have a tables.tex file and then import specific tables into other files?
Yes it is!
Copy the following example code into a file called main.tex (or any other name):
\documentclass{article}
\begin{document}
Some text.
\input{tables.tex}
Some other text.
\end{document}
Then copy the following code for one table into a file called tables.tex:
\begin{table}[ht]
\centering
\begin{tabular}{|l|c|r|}
\hline
a & b & c\\
\hline
d & e & f\\
\hline
\end{tabular}
\end{table}
Save them both in the same folder and compile the first one.
If your tables.tex file is in a subfolder or different folder, edit the path in the code accordingly like \input{subfolder/tables.tex} or \input{../other_folder/tables.tex}.
If you need two or more different tables in different sections of the same document, then you need to input more files, e.g. table1.tex, table2.tex, ecc.
Also very important: input command vs. include command.

knitr file ended while scanning use of \#xverbatim

I have the following .Rnw file:
\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{multicol}
\begin{document}
\begin{multicols}{2}
\hskip-3.5cm\begin{tabular}{|l|}
\hline
\cellcolor[RGB]{0,0,140}{\large\textbf{\textcolor{white}{Bill To: }}}\\
\hline
\textbf{
"asdf"
}\\
\\[-1em]
\textbf{asdf#asdf.com} \\
\hline
\end{tabular}
\hskip6cm\begin{tabular}{|l|l|}
\hline
Date: & 05/31/2018 \\
\hline
Invoice \#: & 1234asdf \\
\hline
\end{tabular}
\end{multicols}
\end{document}
which gives me the expected pdf:
However, when I replace the "asdf" with R code:
\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{multicol}
\begin{document}
\begin{multicols}{2}
\hskip-3.5cm\begin{tabular}{|l|}
\hline
\cellcolor[RGB]{0,0,140}{\large\textbf{\textcolor{white}{Bill To: }}}\\
\hline
\textbf{
<<asdf>>=
cat("asdf")
#
}\\
\\[-1em]
\textbf{asdf#asdf.com} \\
\hline
\end{tabular}
\hskip6cm\begin{tabular}{|l|l|}
\hline
Date: & 05/31/2018 \\
\hline
Invoice \#: & 1234asdf \\
\hline
\end{tabular}
\end{multicols}
\end{document}
I get the following error:
File ended while scanning use of \#xverbatim
Looking at the generated .tex file, this is the relevant part:
\textbf{
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{cat}\hlstd{(}\hlstr{"asdf"}\hlstd{)}
\end{alltt}
\begin{verbatim}
## asdf
\end{verbatim}
\end{kframe}
\end{knitrout}
}\\
and this is what the .log file says:
Runaway argument?
#### asdf \end {verbatim} \end {kframe} \end {knitrout} \check#icr \expandafte
r \ETC.
! File ended while scanning use of \#xverbatim.
<inserted text>
\par
<*> test2.tex
I suspect you have forgotten a `}', causing me
to read past where you wanted me to stop.
I'll try to recover; but if the error is serious,
you'd better type `E' or `X' now and fix your file.
! Emergency stop.
<*> test2.tex
What am I doing wrong?
By default, R output is wrapped in a LaTeX verbatim environment, and you can't put one of those inside \textbf. There are a couple of different approaches to fix this.
The simplest is just to use the chunk option results='asis', i.e.
\textbf{
<<asdf,results='asis',echo=FALSE>>=
cat("asdf")
#
}
This will prevent knitr from adding the environment around the output; the LaTeX code will just be
\textbf{
asdf
}
which should be fine.
If you want the default formatting but just want to change the font or style of text, things are harder. You need to tell knitr to use a different environment instead of verbatim, e.g. the Verbatim environment provided by the fancyvrb package. You can do this by changing the output hook. For example, this should work
% in the preamble:
\usepackage{fancyvrb}
<<include=FALSE>>=
oldhook <- knitr::knit_hooks$get("output")
bold <- function(x, options)
paste0("\\begin{Verbatim}[fontseries=b]\n", x, "\\end{Verbatim}")
#
% in the body:
<<asdf,echo=FALSE>>=
knitr::knit_hooks$set(output = bold)
cat("asdf")
#
% Optionally restore the old hook...
<<include=FALSE>>=
knitr::knit_hooks$set(output = oldhook)
#
However, it doesn't always work, because some options (like fontseries=b) conflict with settings that knitr makes. You can change to italic (using fontshape=it), but not to bold. So stick with the first suggestion.

Using the booktabs \midrule and with brackets

I am having trouble using \midrule in a latex longtable along with brackets. For example, here is my latex document (test.tex):
\documentclass[a4paper]{article}\usepackage[]{graphicx}\usepackage[]{color}
\usepackage{longtable}
\usepackage{booktabs}
\begin{document}
\begin{longtable}{|l|l|}
\caption{} \\
\toprule
test & estimate\\
\midrule
(Intercept) & 10.000 \\
test & 20.000 \\
\bottomrule
\end{longtable}
\end{document}
When running pdflatex on this file:
pdflatex test.tex
I run into these errors:
! Undefined control sequence.
<argument> ...al \expandafter \let \cmrsideswitch
\#tempa \fi \fi
l.12 (Intercept)
& 10.000 \\
Removing the brackets fixes the issue. And interestingly switching the order of the 2 rows works too [i.e. the (Intercept) row as the second row). I can't figure out what is wrong. Has anyone encountered this?
OK, so I had the same problem with code generated from Pandoc (with bracket after \toprule), I fixed it by using \toprule{} instead, it seems that toprule eats the bracket otherwise. Maybe this will help you.
Another possibilty is to put empty \hbox{} before the opening bracket, which I used, since I could not modify tex produced by pandoc (but pandoc is capable of parsing latex snippets in markdown).

How do I compile a LaTeX table exported from R?

I'm completely new to LaTeX and have the MacTeX 2009 package installed, with the intent of having tables from R output into LaTeX and formatted as PDF.
I get the following LaTeX code (below) when I run an example in R (it renders ok in R, but I´d like to use TeXshop). However, when I paste this into a TeXshop window, I get the following error:
./Untitled.tex:2: LaTeX Error: Environment table undefined
I´m sure there is something very basic I´m missing here.
% latex.default(cstats, title = title, caption = caption, rowlabel = rowlabel,
% col.just = col.just, numeric.dollar = FALSE, insert.bottom = legend,
% rowname = lab, dcolumn = dcolumn, extracolheads = extracolheads,
% extracolsize = Nsize, ...)
%
\begin{table}[!tbp]
\caption{Descriptive Statistics by treatment\label{f}}
\begin{center}
\begin{tabular}{lccc}\hline\hline
\multicolumn{1}{l}{}&\multicolumn{1}{c}{Drug}&\multicolumn{1}{c}{Placebo}&\multicolumn{1}{c}{Test Statistic}\tabularnewline
&\multicolumn{1}{c}{{\scriptsize $N=263$}}&\multicolumn{1}{c}{{\scriptsize $N=237$}}&\tabularnewline
\hline
age&{\scriptsize 46.5~}{49.9 }{\scriptsize 53.2} &{\scriptsize 46.7~}{50.0 }{\scriptsize 53.4} &$ F_{1,498}=0.1 ,~ P=0.754 ^{1} $\tabularnewline
sex~:~m&47\%~{\scriptsize~(123)}&44\%~{\scriptsize~(104)}&$ \chi^{2}_{1}=0.42 ,~ P=0.517 ^{2} $\tabularnewline
Primary~Symptoms~:~Depressed&0\%~{\scriptsize~(0)}&0\%~{\scriptsize~(0)}&$^{2}$\tabularnewline
~~~~Headache&0\%~{\scriptsize~(0)}&0\%~{\scriptsize~(0)}&$^{2}$\tabularnewline
~~~~Hangnail&0\%~{\scriptsize~(0)}&0\%~{\scriptsize~(0)}&$^{2}$\tabularnewline
~~~~Muscle~Ache&0\%~{\scriptsize~(0)}&0\%~{\scriptsize~(0)}&$^{2}$\tabularnewline
~~~~Stomach~Ache&0\%~{\scriptsize~(0)}&0\%~{\scriptsize~(0)}&$^{2}$\tabularnewline
\hline
\end{tabular}
\end{center}
\noindent
{\scriptsize $a$\ }{$b$\ }{\scriptsize $c$\ } represent the lower quartile $a$,
the median $b$, and the upper quartile $c$\ for continuous variables.\\
Numbers after percents are frequencies.\\\indent Tests used:\\
\textsuperscript{\normalfont 1}Wilcoxon test; \textsuperscript{\normalfont 2}Pearson test
\end{table}
You need some boilerplate around this to set up the document -- the LaTeXTemplate in TexShop should do.
Your \begin{table} is commented out with a preceding %
There are some missing backslashes in the legend text -- possibly these are just cut & paste artefacts?
This thrown-together version works for me, though you may need to tweak it for your purposes:
\documentclass[11pt]{article}
\begin{document}
\begin{table}[!tbp]
\caption{Descriptive Statistics by treatment\label{f}}
\begin{center}
\begin{tabular}{lccc}
\hline
\hline
\multicolumn{1}{l}{}&\multicolumn{1}{c}{Drug}&\multicolumn{1}{c}{Placebo}&\multicolumn{1}{c}{Test Statistic}
\tabularnewline
&\multicolumn{1}{c}{{\scriptsize $N=263$}}&\multicolumn{1}{c}{{\scriptsize $N=237$}}&
\tabularnewline
\hline
age&{\scriptsize 46.5~}{49.9 }{\scriptsize 53.2} &{\scriptsize 46.7~}{50.0 }{\scriptsize 53.4} &$ F_{1,498}=0.1 ,~ P=0.754 ^{1}$
\tabularnewline
sex~:~m&47\%~{\scriptsize~(123)}&44\%~{\scriptsize~(104)}&$ \chi^{2}_{1}=0.42 ,~ P=0.517 ^{2} $
\tabularnewline
Primary~Symptoms~:~Depressed&0\%~{\scriptsize~(0)}&0\%~{\scriptsize~(0)}&$^{2}$
\tabularnewline
~~~~Headache&0\%~{\scriptsize~(0)}&0\%~{\scriptsize~(0)}&$^{2}$
\tabularnewline
~~~~Hangnail&0\%~{\scriptsize~(0)}&0\%~{\scriptsize~(0)}&$^{2}$
\tabularnewline
~~~~Muscle~Ache&0\%~{\scriptsize~(0)}&0\%~{\scriptsize~(0)}&$^{2}$
\tabularnewline
~~~~Stomach~Ache&0\%~{\scriptsize~(0)}&0\%~{\scriptsize~(0)}&$^{2}$
\tabularnewline
\hline
\end{tabular}
\end{center}
\noindent {\scriptsize $a$\ }{$b$\ }{\scriptsize $c$\ } represent the lower quartile $a$, the median $b$, and the upper quartile $c$ for continuous variables.\\
Numbers after percents are frequencies.\\
\indent Tests used:\\
\textsuperscript{\normalfont 1}Wilcoxon test;
\textsuperscript{\normalfont 2}Pearson test
\end{table}
\end{document}
If you use the package stargazer (available on CRAN), you can use the argument out="filename.tex" to output the LaTeX code directly into a Tex document. That document should then be easy to compile.
It sounds like you may not have a document class defined, or you are not using a document class that defines the table environment.

Storing and recalling variable number of text strings in LaTeX

I'm currently writing an API document in LaTeX. I have a tabular environment with a list of error codes and descriptions, like so:
\begin{tabular}{|c|l|}
\hline
\textbf{Code} & \textbf{Description} \\ \hline
1 & (description of error 1) \\ \hline
2 & (description of error 2) \\ \hline
\end{tabular}
At various places later in the document, I reference an error's code and its description together, like so:
Possible error conditions:
\begin{itemize}
\item 1---(description of error 1)
\end{itemize}
I want to automate this process so I don't have to retype the descriptions every time. I have tried using a counter, labels, and the \savebox command, but it's pretty cumbersome:
\newcounter{error}
% Inside the tabular environment:
\newsavebox{\ErrorOne}
\savebox{\ErrorOne}{(description of error 1)}
\refstepcounter{error} \label{ErrorOne} \arabic{error} & \usebox{\ErrorOne} \\ \hline
and later, to reference the error,
\ref{ErrorOne}---\usebox{\ErrorOne}
I particularly object to having to use ErrorOne for the labels but \ErrorOne (with the leading backslash) for the saveboxes. I also don't really want names like ErrorOne, since I might need to change the order at some point. What I want is to be able to define a few commands:
\newerror{errorlabel}{Description} % defines the error (doesn't output anything)
\errorcode{errorlabel} % outputs the error code
\errordesc{errorlabel} % outputs the error description
and then be able to say something like
\newerror{ArgumentError}{Too many arguments}
\newerror{DatabaseError}{Could not connect to database}
% Inside the tabular environment:
\errorcode{ArgumentError} & \errordesc{ArgumentError} \\ \hline
\errorcode{DatabaseError} & \errordesc{DatabaseError} \\ \hline
% Later on:
\errorcode{DatabaseError}---\errordesc{DatabaseError}
with the error codes (1, 2, 3, ...) being automatically generated like labels would be.
Any ideas?
The following works for me
\catcode`\#=11
\newcounter{error}
\def\newerror#1#2{\refstepcounter{error}%
\expandafter\xdef\csname errno##1\endcsname{\arabic{error}}%
\expandafter\xdef\csname errds##1\endcsname{#2}%
}
\def\errorcode#1{\expandafter\printerrinfo \csname errno##1\endcsname}
\def\errordesc#1{\expandafter\printerrinfo \csname errds##1\endcsname}
\def\printerrinfo#1{\ifx#1\relax\errmessage{Error code is invalid}%
\else\expandafter#1\fi}
\catcode`\#=12
\newerror{ArgumentError}{Too many arguments}
\newerror{DatabaseError}{Could not connect to database}
\errorcode{DatabaseError}---\errordesc{DatabaseError}
In your preamble, create new commands for each error, then just call the command:
\newcommand{\errorone}{this is the error and its description}
then in the body, just call the new command:
\begin{tabular}{|c|l|}
\hline
\textbf{Code} & \textbf{Description} \\ \hline
1 & \errorone \\ \hline

Resources