crsh
crsh

Reputation: 1716

Rmd/Kntir: Markdown citations in LaTeX environments

I want to create a threeparttable in a Rmd/Knitr-document and to add a note to the bottom of the table. The table is created by a R-function inside a chunk with results = "asis". I did not add the function to the working example because it's quite verbose and the problem is evident from the pure LaTeX code.

This works and the result looks as expected.

---
title: "Untitled"
output: pdf_document
header-includes:
- \usepackage{threeparttable}
- \usepackage{booktabs}
- \usepackage{longtable}
references:
- id: rao2001basic
  title: Basic Research in Parapsychology
  author:
  - family: Rao
    given: K.R.
  issued:
    year: 2001
  publisher: McFarland
  type: book
---

\begin{table}[h]
\centering
\begin{threeparttable}
\caption{A summary table of the cars dataset.}
\begin{tabular}{lrr}
\toprule
Descriptives & speed & dist\\
\midrule
Mean & 15.4 & 42.98\\
SD & 5.29 & 25.77\\
Min & 4 & 2\\
Max & 25 & 120\\
\bottomrule
\end{tabular}
\tablenotes{\item\textit{Note.} This table was created by @rao2001basic. }
\end{threeparttable}
\end{table}

enter image description here

Unfortunately, the citation in the table caption is not working. It works fine if I take it out of the LaTeX environment, but not inside. Is there a way to parse the Markdown in the LaTeX environment?

Upvotes: 18

Views: 1398

Answers (2)

crsh
crsh

Reputation: 1716

I found that if you are willing to use the bookdown::pdf_document2() format, you can use text references to solves this problem without having to mess with LaTeX:

---
title: "Untitled"
output: bookdown::pdf_document2
header-includes:
- \usepackage{threeparttable}
- \usepackage{booktabs}
- \usepackage{longtable}
references:
- id: rao2001basic
  title: Basic Research in Parapsychology
  author:
  - family: Rao
    given: K.R.
  issued:
    year: 2001
  publisher: McFarland
  type: book
---

(ref:tablenote)
This table was created by @rao2001basic.

\begin{table}[h]
\centering
\begin{threeparttable}
\caption{A summary table of the cars dataset.}
\begin{tabular}{lrr}
\toprule
Descriptives & speed & dist\\
\midrule
Mean & 15.4 & 42.98\\
SD & 5.29 & 25.77\\
Min & 4 & 2\\
Max & 25 & 120\\
\bottomrule
\end{tabular}
\tablenotes{\item\textit{Note.} (ref:tablenote)}
\end{threeparttable}
\end{table}

This even works when tables are created by in R:

```{r results = "asis"}
knitr::kable(mtcars[1:3, ], caption = "(ref:tablenote)")
```

Upvotes: 0

Thell
Thell

Reputation: 5958

This sort of issue is essentially an escaping issue or rather an avoidance issue of pandoc's automatic latex block begin/end recognition.

This particular case could be written with the environment commands directly as

\table[h]
\centering
\threeparttable
\caption{A summary table of the cars dataset.}
\begin{tabular}{lrr}
\toprule
Descriptives & speed & dist\\
\midrule
Mean & 15.4 & 42.98\\
SD & 5.29 & 25.77\\
Min & 4 & 2\\
Max & 25 & 120\\
\bottomrule
\end{tabular}
\tablenotes[flushleft]
\item\textit{Note.} This table was created by @rao2001basic.
\endtablenotes
\endthreeparttable
\endtable

but if the begin{env}/end{env} are truly needed then macros can be used like this

\def \btable{\begin{table}}
\def \etable{\end{table}}
\def \bthreeparttable{\begin{threeparttable}}
\def \ethreeparttable{\end{threeparttable}}
\def \btablenotes{\begin{tablenotes}}
\def \etablenotes{\end{tablenotes}}

It would be nice if a robust generic solution existed for renaming of begin{env}/end{env} that could allow selective markdown within tex blocks. Something like...

\newcommand\mdbegin[2]{%
  \ifstrempty{#1}{%
    \begin{#2}
  }{%
    \begin{#1}[#2]
  }%
}

\newcommand\mdend[1]{%
  \end{#1}
}

which works for this, using the etoolbox package, but I don't think it would be a recommended solution.

Upvotes: 4

Related Questions