adding \label{} in kable kableExtra latex output
Asked Answered
E

3

7

I am trying to create a latex formatted table in R with a \label{} option.

Something similar to

library(tidyverse)
library(knitr)
library(kableExtra)

data_frame(a = 1:3, b = 2:4) %>% 
  kable(align = 'c', format = 'latex') %>% 
  kable_styling(full_width = FALSE)

the resulting output is below, but i would like to add the \label{label_value} parameter to the latex table with a label_value I can define

\begin{table}[H]
\centering
\begin{tabular}{c|c}
\hline
a & b\\
\hline
1 & 2\\
\hline
2 & 3\\
\hline
3 & 4\\
\hline
\end{tabular}
\end{table}

I am trying to automatically create tables in R which I can save and then include in a latex document

Ecclesiology answered 7/1, 2019 at 22:32 Comment(2)
where is the \label parameter placed? You can manually edit the output of kable like a regular string. You just need to make sure you give the resulting strings the attributes from the original outputEnginery
looks like gsub() can work nicely to edit the automatic label created when using the caption argument and still retain the attributes. Thanks for the suggestions OganMEcclesiology
W
8

If you put a caption in the call to kable, you'll get a label automatically, based on the name you give to the code chunk.
If you are running it at the top level as in your example, it's kind of clunky: you need put the label in your caption, and you'll get two labels, yours and the auto-generated useless one. For example,

library(tidyverse)
library(knitr)
library(kableExtra)

data_frame(a = 1:3, b = 2:4) %>% 
  kable(align = 'c', format = 'latex', 
       caption = "Table caption \\label{tab:example}") %>% 
  kable_styling(full_width = FALSE)

which produces

\begin{table}[t]

\caption{\label{tab:}Table caption \label{tab:example}}
\centering
\begin{tabular}{c|c}
\hline
a & b\\
\hline
1 & 2\\
\hline
2 & 3\\
\hline
3 & 4\\
\hline
\end{tabular}
\end{table}

I'm not sure, but I'd expect in some circumstances you'd need to manually edit out the auto-generated label \label{tab:}. So use knitr or R-markdown and avoid the problem.

Wavellite answered 8/1, 2019 at 0:4 Comment(2)
great solution user2554330. I'm currently writing a manuscript in Tex Shop with tables/graphics produced in R so I was looking for an easy way to automate incorporating the table and graphics and this works well. I don't know that creating the manuscript in R-markdown would work as well for me, but a good suggestion too.Ecclesiology
You can use Sweave-style knitr in TeXShop if you install a custom engine. (Don't use Sweave, use knitr.) It will automatically generate the label. If you can't find or figure out the code for the engine, post another question, but maybe not on Stackoverflow: tex.stackexchange.com would be more appropriate.Wavellite
L
7

You can use knitr::opts_current to control the automatic label generated with the caption:

> library(knitr)
> library(kableExtra)
> opts_current$set(label = "hello")
> data.frame(a = 1:3, b = 2:4) %>% 
+     kable(align = 'c', format = 'latex', 
+           caption = "Table caption") %>% 
+     kable_styling(full_width = FALSE)
\begin{table}

\caption{\label{tab:hello}Table caption}
\centering
\begin{tabular}[t]{c|c}
\hline
a & b\\
\hline
1 & 2\\
\hline
2 & 3\\
\hline
3 & 4\\
\hline
\end{tabular}
\end{table}
Leia answered 8/1, 2019 at 1:40 Comment(1)
That's interesting: the knitr::opts_current help page says that opts_current$set does nothing. I guess that's only true in a knitr document, not in top level code.Wavellite
M
7

There's a built-in option for that.

kable(..., label = "something")

More info at: https://github.com/haozhu233/kableExtra/issues/486

Marlonmarlow answered 7/10, 2020 at 4:47 Comment(1)
I like it, but doesn't give you enough control, because it prefixes with a "tab:" text.Carmencita

© 2022 - 2024 — McMap. All rights reserved.