How to suppress automatic table name and number in an .Rmd file using xtable or knitr::kable?
It turns out that I needed to add the following in the YAML section:
header-includes:
- \usepackage{caption}
AND the following somewhere before the code chunk:
\captionsetup[table]{labelformat=empty}
Now it works:
---
title: "Suppress automatic table name and number"
output: pdf_document
header-includes:
- \usepackage{caption}
---
\captionsetup[table]{labelformat=empty}
```{r myirischunk, results = 'asis', tab.cap = NULL, echo = TRUE}
print(knitr::kable(head(iris), caption = "Table 21.a - My very own table name"))
```
This has also been described here:
Get rid of captions using texreg in markdown
And yes, I'm a bit embarrased that I didn't find that answer straight away.
Anyway, thanks to daroczig for pointing me in the tex direction instead of trying to solve the problem using chunk options or something like that.
Adding a letter to all table numbers in RMarkdown
An alternative solution is avaiable via LaTeX. Add the following somewhere in the body of the document to change the default figure and table names to include the letter.
\def\figurename{Figure A}
\def\tablename{Table A}
To reference a table or figure in text use, e.g., Table A\@ref(tab:label)
, after defining the table label as normal.
This alone will leave a space in the table/figure caption (e.g., 'Table A 1' instead of 'Table A1'). This can be solved by adding the following to the YAML:
header-includes:
- \usepackage{caption}
- \DeclareCaptionLabelFormat{nospace}{#1#2}
- \captionsetup[figure]{labelformat=nospace}
- \captionsetup[table]{labelformat=nospace}
The \DeclareCaptionLabelFormat creates a function (here labeled nospace
), which overrides the default caption label when called. #1
represents the label assigned to refer to tables or figures (e.g., 'Table A') and #2
represents the number of the table or figure. The captionsetup
lines change the label format for all tables and figures to the format defined by 'nospace'.
Thus, the code produces, e.g., 'Table A1: Table name' as the caption. If instead you wanted, e.g., 'Table A-1: Table name', the code in \DeclareCaptionLabelFormat
should be changed to {#1-#2}
.
---
title: "Supplement A"
output: bookdown::pdf_document2
toc: false
header-includes:
- \usepackage{caption}
- \DeclareCaptionLabelFormat{nospace}{#1#2}
- \captionsetup[figure]{labelformat=nospace}
- \captionsetup[table]{labelformat=nospace}
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(knitr)
library(kableExtra)
library(bookdown)
```
\def\figurename{Figure A}
\def\tablename{Table A}
See Table A\@ref(tab:cars) for something about cars.
(ref:cars) Table caption
```{r cars}
kable(mtcars[1:5, ], booktabs=TRUE, caption="(ref:cars)") %>%
kable_styling(latex_options="hold_position", position="left")
```
Answer inspired by: Figure name in caption using RMarkdown
Caption latex package documentation: https://ctan.org/pkg/caption
Using table caption on R markdown file using knitr to use in pandoc to convert to pdf
If you do not insist on using a LaTeX/HTML-only solution with the otherwise awesome xtable
package, you might achieve the same with Pandoc's markdown. One option is to add the caption manually below the table, or use my R Pandoc writer package:
> library(pander) # load pkg
> panderOptions('table.split.table', Inf) # not to split table
> set.caption('Hello Fisher!') # add caption
> pander(head(iris)) # show (almost) any R object in markdown
-------------------------------------------------------------------
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
-------------- ------------- -------------- ------------- ---------
5.1 3.5 1.4 0.2 setosa
4.9 3.0 1.4 0.2 setosa
4.7 3.2 1.3 0.2 setosa
4.6 3.1 1.5 0.2 setosa
5.0 3.6 1.4 0.2 setosa
5.4 3.9 1.7 0.4 setosa
-------------------------------------------------------------------
Table: Hello Fisher!
Then use Pandoc to convert this markdown file to HTML, LaTeX, docx, odt or any other popular document formats.
Scale kable table to fit page width
One option is to use kable_styling
from the kableExtra
package. The option latex_options="scale_down"
will fit the table within the paper margins. See the vignette for detailed examples on all of the formatting options.
---
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(knitr)
library(kableExtra)
```
```{r}
kable(cbind(mtcars[1:5,], mtcars[1:5,]))
```
```{r}
kable(cbind(mtcars[1:5,], mtcars[1:5,]), format="latex", booktabs=TRUE) %>%
kable_styling(latex_options="scale_down")
```
get rid of captions when using texreg in Rmarkdown
In the YAML section where LaTeX packages can be included, add the caption package:
header-includes:
- \usepackage{caption}
Then at the beginning of the RMarkdown document body add:
\captionsetup[table]{labelformat=empty}
This removes the caption labels for all tables.
How to add a check mark to a xtable in an Rmd. file used to create a pdf
Change your call to xtable()
to:
print(xtable(Softwares, align = rep("c", 8)), scalebox='0.75',
type = "latex", sanitize.text.function = function (x) x)
knitr, R Markdown, and xtable: xtable tables within HTML table
I think your code will work if you put results=asis
in the chunk options.
<table border = 1>
<tr>
<td>
```{r results='asis', echo=FALSE}
library(xtable)
data(tli)
print(xtable(tli[1:20, ]),type='html')
```
</td>
<td>
```{r results='asis', echo=FALSE}
library(xtable)
data(tli)
print(xtable(tli[1:20, ]),type='html',comment=FALSE)
```
</td>
</tr>
</table>
Related Topics
Interpolate/Extend Quarterly to Monthly Series
How to Extract Fitted Splines from a Gam ('Mgcv::Gam')
R Not Finding Package Even After Package Installation
Best Practices for Storing and Using Data Frames Too Large for Memory
How to Drop Unused Levels from a Data Frame
How to Annotate() Ggplot with Latex
Remove Empty Elements from List with Character(0)
Avoid Wasting Space When Placing Multiple Aligned Plots Onto One Page
Recommended Package for Very Large Dataset Processing and MAChine Learning in R
Easier Way to Plot the Cumulative Frequency Distribution in Ggplot
How to Create, Structure, Maintain and Update Data Codebooks in R
Plot Size and Resolution with R Markdown, Knitr, Pandoc, Beamer
Error in Fetch(Key):Lazy-Load Database
R Reading in a Zip Data File Without Unzipping It