Inserting table in R Markdown

latex

#1

I am a college student, and use R markdown to create the reports for my assignments. Generally, I use kable in knitr to create tables.

In my latest project, I needed to insert the table of transformations for Yeo - Johnson Transformation for normality, which is given here.

I tried the following:

---
title: "Latex Table in R Markdown"
output: pdf_document
---

Some text

$$
\begin{table}[]
\centering
\caption{Transformations Associated with the Johnson System}
\begin{tabular}{|l|l|l|l|}
\hline
Johnson Family & Transformation & Parameter Conditions & X Condition \\ \hline
S_B & Z=\gamma + \eta ln(\frac {X - \epsilon} {\lambda + \epsilon - X}) & \eta, \lambda >0, -\infty < \gamma, \epsilon < \infty & \epsilon < X < \epsilon + \lambda \\ \hline
S_L & Z=\gamma + \eta ln(X - \epsilon) & \eta >0, -\infty < \gamma, \epsilon < \infty & X > \epsilon \\ \hline
S_U & Z=\gamma + \eta \sinh^{-1}(\frac {X - \epsilon} {\lambda}) & \eta, \lambda >0, -\infty < \gamma, \epsilon < \infty & -\infty < X < \infty \\ \hline
\end{tabular}
\end{table}
$$

Some text

This gives me the following error:

! Missing $ inserted.
<inserted text> 
                $
l.96 S_
       B & Z=\gamma + \eta ln(\frac {X - \epsilon} {\lambda + \epsilon - X})...

Error: Failed to compile a.tex. See a.log for more info.
In addition: Warning message:
running command '"pdflatex" -halt-on-error -interaction=batchmode "a.tex"' had status 1 
Execution halted

I generated the table code using LaTeX Table Generator. So its failure is surprising. I tried to use the same in Overleaf, but also got the same error message. If I render it to HTML instead of PDF, the compilation happens, but it does not show the table. Only the raw LaTeX code is displayed then.

Can someone please point out the mistake?

Thanks


#2

The $$ tags are what you use to put in an equation. So what your code is trying to do is insert your table as an equation, and it can't resolve the syntax.

What you probably want to do is generate the table with inline equations inside the table. To do this, you'll need to use a lot of $ tags. Try the following:

---
title: "Latex Table in R Markdown"
output: pdf_document
---

Some text


\begin{table}[]
\centering
\caption{Transformations Associated with the Johnson System}
\begin{tabular}{|l|l|l|l|}
\hline
Johnson Family & Transformation & Parameter Conditions & X Condition \\ \hline
$S_B$ & $Z=\gamma + \eta ln(\frac {X - \epsilon} {\lambda + \epsilon - X})$ & $\eta, \lambda >0, -\infty < \gamma, \epsilon < \infty$ & $\epsilon < X < \epsilon + \lambda$ \\ \hline
$S_L$ & $Z=\gamma + \eta ln(X - \epsilon)$ & $\eta >0, -\infty < \gamma, \epsilon < \infty$ & $X > \epsilon$ \\ \hline
$S_U$ & $Z=\gamma + \eta \sinh^{-1}(\frac {X - \epsilon} {\lambda})$ & $\eta, \lambda >0, -\infty < \gamma, \epsilon < \infty$ & $-\infty < X < \infty$ \\ \hline
\end{tabular}
\end{table}


Some text

#3

Thanks, now there's no error on PDF compilation and the table appeared exactly I would have liked.

But the problem is that the location of the table gets changed. See here.

Example.pdf (91.5 KB)

Can you suggest something to avoid this?

On the other hand, I tried raw markdown and used the following:

| Johnson Family | Transformation | Parameter Conditions | X Condition |
|----------------|-----------------------------------------------------------------------|---------------------------------------------------------|-------------------------------------|
| $S_B$ | $Z = \gamma + \eta \ ln(\frac {X - \epsilon} {\lambda + \epsilon - X})$ | $\eta, \lambda >0, -\infty < \gamma, \epsilon < \infty$ | $\epsilon < X < \epsilon + \lambda$ |
| $S_L$ | $Z = \gamma + \eta \ ln(X - \epsilon)$ | $\eta >0, -\infty < \gamma, \epsilon < \infty$ | $X > \epsilon$ |
| $S_U$ | $Z = \gamma + \eta \sinh^{-1}(\frac {X - \epsilon} {\lambda})$ | $\eta, \lambda >0, -\infty < \gamma, \epsilon < \infty$ | $-\infty < X < \infty$ |

This works, but I can't control the width of the columns and cannot add the vertical lines.

Is there any way to achieve these?


#4

The table gets moved because the \begin{table} command opens a floating environment. LaTeX wants to put tables at the bottom of a page and figures at the top of a page. You have two primary options for avoiding this default behavior

Option 1

Tell LaTeX you want the table to stay where it is relative to the text. This requires the LaTeX package placeins and a flag telling LaTeX how strongly you feel about the placement of the table. The !h flag says you feel very strongly that it should happen "here".

The disadvantage here is that it doesn't always work. Sometimes LaTeX ignores your preference, and I won't pretend to understand why it does or does not. If you really want to force LaTeX to behave as you want, you can put \FloatBarrier before and after your table. A floating environment will never move beyond its FloatBarriers. (\FloatBarrier is part of the placeins package).

---
title: "Latex Table in R Markdown"
output: pdf_document
header-includes:
- \usepackage{placeins}
---

and then start your table with

\begin{table}[!h]

Option 2

You may also avoid the entire floating environment altogether by not using the \begin{table} command. The disadvantage here is that \begin{tabular} doesn't have a native caption. So you will need to use the LaTeX package caption and the \captionof command to register the caption with the document.

---
title: "Latex Table in R Markdown"
output: pdf_document
header-includes:
- \usepackage{caption}
---

Some text

\begin{center}
\captionof{table}{Transformations Associated with the Johnson System}
\begin{tabular}{|l|l|l|l|}
\hline
Johnson Family & Transformation & Parameter Conditions & X Condition \\ \hline
$S_B$ & $Z=\gamma + \eta ln(\frac {X - \epsilon} {\lambda + \epsilon - X})$ & $\eta, \lambda >0, -\infty < \gamma, \epsilon < \infty$ & $\epsilon < X < \epsilon + \lambda$ \\ \hline
$S_L$ & $Z=\gamma + \eta ln(X - \epsilon)$ & $\eta >0, -\infty < \gamma, \epsilon < \infty$ & $X > \epsilon$ \\ \hline
$S_U$ & $Z=\gamma + \eta \sinh^{-1}(\frac {X - \epsilon} {\lambda})$ & $\eta, \lambda >0, -\infty < \gamma, \epsilon < \infty$ & $-\infty < X < \infty$ \\ \hline
\end{tabular}
\end{center}

#5

Both of the methods work. I prefer the second though.

Thanks a lot!