Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 666
crsh Avatar asked Mar 24 '15 20:03

crsh


People also ask

How do I add a citation in Rmarkdown?

The usual way to include citations in an R Markdown document is to put references in a plain text file with the extension . bib, in BibTex format. Then reference the path to this file in index. Rmd's YAML header with bibliography: example.

Can I use LaTeX code in R markdown?

By default, Pandoc will preserve raw LaTeX code in Markdown documents when converting the document to LaTeX, so you can use LaTeX commands or environments in Markdown.

How do I add LaTeX to Rmarkdown?

Markdown can be included in LaTeX documents by means of the markdown package. To use this, simply include \usepackage{markdown} in the preamble of your document.

How do you cite in R?

Items can be cited directly within the documentation using the syntax @key where key is the citation key in the first line of the entry, e.g., @R-base . To put citations in parentheses, use [@key] .


1 Answers

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.

like image 117
Thell Avatar answered Sep 21 '22 06:09

Thell