Is there a way to prevent group headers from being rendered as bold when using group_rows function in kableExtra?
I have been using the amazing kableExtra package developed by #Hao to decorate tables outputted from knitr::kable. I want to add group headers, but I do not want these to be in bold. These headers are wrapped in \textbf{} when outputted to latex, and I have not been able to work out which kableExtra function does this "code decoration".
Reproducible example:
---
title: "group_rows kableExtra"
output: pdf_document
---
```{r setup, include=FALSE}
require(kableExtra)
require(knitr)
```
```{r table, results='asis'}
knitr::kable(head(mtcars), booktabs = TRUE, format = "latex") %>%
group_rows("Group 1", 2, 3)
```
#bsalzer on github is helping me recently add some customizable options to group_rows including this feature you are asking for. If you install from github, you can do things like
knitr::kable(head(mtcars), booktabs = TRUE, format = "latex") %>%
group_rows("Group 1", 2, 3, bold = F)
and that will do the trick.
Related
kableExtra seems to disrupt the proper formatting of kables in LaTeX when the caption contains certain characters.
kableExtra::kable will be formatted properly if no caption is used, or if standard knitr::kable is used.
The following code will yield oddly formatted results if kableExtra is loaded.
---
title: "Mock"
output:
pdf_document:
keep_tex: true
---
```{r header, echo= FALSE, include = FALSE, warning= FALSE}
library(skimr)
library(knitr)
# library(kableExtra)
resumir <- function(var, unit = "", caption = NULL) {
if(is.null(caption)) {caption <- deparse(substitute(var))}
skim_to_wide(var) %>%
mutate_all(as.numeric) %>%
kable(caption = paste0(caption, " (", unit, ")"))
}
mtcars2 <- mtcars %>%
rename("NO_CYL" = 'cyl', "PEAK_PERF" = "mpg")
attach(mtcars2)
```
`r resumir(POWER_HRS, unit = "$hours$")`
I think I know already what's happening but after all the hassle I went through to find the issue I think it's good if this goes up on SO and someone posts an answer (or I will after a bit).
So, to keep it simple, the problem turned out to be having underscores in the caption. The only solution that has worked is to remove them, which is not overly unreasonable.
---
title: "Mock"
output:
pdf_document:
keep_tex: true
---
```{r header, echo= FALSE, include = FALSE, warning= FALSE}
library(knitr)
library(kableExtra)
attach(mtcars)
```
`r kable(cyl, caption = "Cylinder_No.")`
There was a bug report for knitr but knitr currently works fine.
I am using RMarkdown to write reproducible reports, however, I am looking for guidance on how to change the justification on table/figure captions when using Kable?
Also, can you bold or italicize the Table 1: component of the caption?
knitr::kable(head(iris), 'latex', caption = 'Title of table',
booktabs = TRUE) %>%
kableExtra::kable_as_image()
This code will produce a generally nice looking table. However, I want to left-justify the title and bold the text "Table 1:" which automatically precedes my table caption.
Thanks for your help.
You can use the LaTeX captions package to customize your captions. For example, this document
---
output: pdf_document
header-includes:
- \usepackage[justification=raggedright,labelfont=bf,singlelinecheck=false]{caption}
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r echo=FALSE}
knitr::kable(head(iris), caption = 'Title of table',
booktabs = TRUE)
```
produces this table output:
Alternatively, if you really want this in a screenshot file, use
library(knitr)
library(kableExtra)
kable(head(iris), format="latex", caption = 'Title of table',
booktabs = TRUE) %>%
as_image(file="~/temp/table.png",
latex_header_includes="\\usepackage[justification=raggedright,labelfont=bf,singlelinecheck=false]{caption}")
Compiling a report with bookdown I encounter difficulties in referencing tables generated with the huxtable package. For my work, LaTex/PDF, as well as an HTML version of the report, need to be created.
When rendering the document knitr::is_XXX_output() selects the optimal way to display the tables, see MWE:
```{r chunk-label, results='asis', fig.cap='chunk-caption'}
set.seed(1234)
dat <- data.frame(cond = factor(rep(c("A","B"), each=2)),
rating = c(rnorm(2),rnorm(2, mean=.8)))
hux <- as_hux(dat) %>%
set_caption('hux caption') %>%
set_label("tab:hux-label")
if (knitr::is_html_output()) {
print_html(hux) # output table html friendly (requires in chunk options "results='asis'")
}
if (knitr::is_latex_output()) {
hux
}
```
I am not sure whether it is recommended to use the caption and label commands provided by huxtable
set_caption('pipe caption') and set_label("tab:hux-label")
or knitr
chunk-label and fig.cap='chunk caption'
For figures, the latter works very well, but unfortunately not for tables.
The hook for "tab.cap" as discussed in following did not work well with bookdown and if PDF & HTML are needed.
Using table caption on R markdown file using knitr to use in pandoc to convert to pdf
Help and recommendations are very much appreciated!
If you upgrade to huxtable 4.3.0 (now on CRAN), it automatically takes care of bookdown table captions for you. Here's a short example:
---
title: "Bookdown test"
output:
bookdown::pdf_book: default
link-citations: yes
---
```{r setup, include=FALSE}
library(dplyr)
library(huxtable)
knitr::opts_chunk$set(echo = FALSE)
```
My table is \#ref(tab:foo1). The other table is \#ref(tab:foo2). The third is \#ref(tab:foo3).
```{r}
hux(a = 1:5, b = 1:5) %>%
set_caption("My labelled table") %>%
set_label("tab:foo1")
hux(a = 1:5, b = 1:5) %>%
set_caption("My unlabelled table")
hux(a = 1:5, b = 1:5) %>%
set_caption("My labelled table, prefix should be autoadded!") %>%
set_label("foo2")
hux(a = "A table with no caption, but a label") %>%
set_label("tab:foo3")
hux(a = "A table with no caption or label")
```
Not everything is perfect. If you set echo = TRUE you will need to manually insert \usepackage[table]{xcolor} before \usepackage{fancyvry} in the TeX header.
I am attempting to output a latex table using r markdown, kable and kableExtra. When i use the option row.names=FALSE instead of row.names=TRUE the latex code generates \vphantom code which produce an error to create the pdf .
It seems the problem is linked to the row_spec option.
Here is Rmarkdown code (.Rmd File):
---
title: "Test"
output:
pdf_document:
fig_caption: true
keep_tex: true
---
{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
{r}
library(knitr)
library(kableExtra)
temp <- mtcars[1:5,1:5]
kable(temp, format = "latex", booktabs = F,row.names=F) %>%
kable_styling(position = "center") %>%
row_spec(1, bold = T, background = "red")
The error is :
! Forbidden control sequence found while scanning use of
\check#nocorr#.
\par l.105 ...color{red} \textbf{21.0 &\vphantom{1} 6}
& \textbf{160} & \textbf{...
Do you have any issue of what is happening ?
This is caused by the duplicated rows in the dataframe, as both rows 1 and 2 are the same.
Reviewing the code for row_spec_latex, when kableExtra is used against a kable table, it checks for duplicated rows. If it finds one, it inserts the vphantom argument within the fix_duplicated_rows_latex internal function. This vphantom insertion is then messing up the formatting of the textbf function.
This seems like a slight bug, so it may be worth reporting it as an issue in kableExtra: https://github.com/haozhu233/kableExtra . I am sure that the vphantom is added for a good reason though, but doubt this was an intended consequence.
Supporting code:
---
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(knitr)
library(kableExtra)
temp <- mtcars[1:5,1:5]
```
```{r}
# Keeping the row names (means all rows are unique)
kable(temp, format = "latex", booktabs = F) %>%
kable_styling(position = "center") %>%
row_spec(1, bold = T, color = "red")
```
```{r}
# Highlighting second row (which doesn't have the vphantom statement)
kable(temp, format = "latex", booktabs = F, row.names=F) %>%
kable_styling(position = "center") %>%
row_spec(2, bold = T, color = "red")
```
It's possible to reference a figure in knitr like that:
```{r myfig}
plot(1,1)
```
Figure \ref{fig:myfig} shows ...
The same is not possible for tables, e.g.
```{r my_table, results='markup', fig.cap='capture'}
tab <- read.table('my_table.txt', sep = '\t')
kable(tab,
format='pandoc',
digits = 3,
caption =
"Description")
```
Table \ref{table:my_table} shows ...
doesn't work! Is it possible to make this work without digging into latex? If no, what would I have to do to make it work?
With format='pandoc' you need to enter the \label command in the caption.
With format='latex' the reference is automatically created as tab:chunk_label. For example,
---
output:
pdf_document
tables: true
---
```{r results='markup'}
tab <- head(iris)
knitr::kable(tab,
format='pandoc',
digits = 3,
caption = "Pandoc table\\label{tab:pandoc_table}"
)
```
```{r latex_table, results='markup'}
tab <- head(iris)
knitr::kable(tab,
format='latex',
digits = 3,
caption = "LaTeX table",
booktabs = TRUE
)
```
Table \ref{tab:pandoc_table} was done using Pandoc,
while Table \ref{tab:latex_table} used \LaTeX.
Replace table with tab \#ref(tab:my_table)