This error can be avoided by using the environ
package. For example, here's a working modifiation of your code using \NewEnviron
of that package:
\documentclass{article}
\usepackage{tabularx}
\usepackage{booktabs}
\usepackage[table]{xcolor}
\usepackage{environ}
\makeatletter
\NewEnviron{defaultTable}[2] {
\@float{table}[h]
\noindent
\tabularx{\textwidth}{#1}
\specialrule{0.5pt}{10pt}{0pt}
\rowcolor[gray]{.9}
\gdef\mycaption{#2}
\BODY
\bottomrule}[
\endtabularx
\emph{\caption{\mycaption}}
\end@float
]
\makeatother
\begin{document}
\begin{defaultTable}{X X}{Test Table}
1 & 2 \\
\end{defaultTable}
\end{document}
Note: either end also the last row by \\
(or do it after \BODY
). (In the original environment definition \\
before \bottomrule
doesn't help.)
The table is based on LaTeX. Using Rmarkdown inside the table environment is never going to work, as it doesn't recognise Rmarkdown. Using LaTeX citations is logical idea, however, LaTeX citations don't work the same way Rmarkdown citations work. This is an Rmarkdown document and doesn't provide any instructions for LaTeXtables. As you can see, the YAML header provides Rmarkdown citation instructions:
---
bibliography: library.bib
csl: conservation-biology.csl
---
With the bibliography file (.bib) and citation style (.csl) specified. The citations are read by pandoc (that's what knitr uses to convert .rmd files to .md files) and turned into plain text in the .md file. To get a latex/pdf document, the .md file is further converted to a .tex file using pdflatex. The Rmarkdown citation [@Saunders2007] is now written plain text.
It is better to use the LaTeX citation \cite{Saunders2007}
instead, as this will be recognized as LaTeX and therefore LaTeX will try to look up the citation. However, no reference library is given for LaTeXcitations. To do this, one needs to write \bibliography{library}{}
at the bottom of the document.
However, we want the citation styles to be the same. I'm using conservation-biology.csl here. Unfortunately, latex/bibtex doesn't use .csl files but .bst files (using \bibliographystyle{stylename}
at the bottom of the document). As there are way more .csl files than .bst available (check this massive .csl repo), it's unlikely that your specific Rmarkdown citation style can also be found as .bst file.
Of course, you can write your own .bst file, but for the number of citations that are contained in the table it might be easier to just hand write them. To make sure they appear in the references list, use this:
---
nocite: |
@Saunders2007
...
Best Answer
You can only place rules after the table preamble or after a line change command
\\
(or similar). You can add\\
after the lastminipage
:Notice that I commented out a superfluous blank space after the first
\end{minipage}
(otherwise, you'll get an overfull\hbox
). I also killed the space before and after the tabular contents using@{}c@{}
(again, to prevent an overfull\hbox
).