Richard
Richard

Reputation: 105

R: knitr: pander: aligning caption to table

I'm trying to center align a caption to a pandoc table in an Rmarkdown file (outputting to PDF). The table is page centered and I'd like the caption to be similarly centered. I can't seem to find an option in pander/pandoc.table which lets me do this in Rmarkdown that I'm writing in Rstudio/Knitr. My only solution at present is to include a string of ...nbsp;

I've checked panderOptions, and I've also followed the comments (without success) in xtable caption alignment left aligned with table or centered (using knitr), by inserting code in the yaml header. And, I've added a line break as suggested here: Issue with creating PDf file with Pander+Knitr: Error when putting table with caption and plot directly next to each other.

Any suggestions would be very welcome.

Thanks, Richard

Rmarkdown doc:

header-includes:
 - \usepackage[
    singlelinecheck=false,
    justification=centering
    ]{caption}
output:
    pdf_document 
---

```{r table2, echo=FALSE, message=FALSE, warnings=FALSE, results='asis'}
library(pander)
panderOptions('keep.line.breaks', TRUE )
table2 <- "HHHH member | Description of future    
            H11111                | standard model    
            H22222                | low model      
            H33333                | decreasing projection"
df2 <- read.delim(textConnection(table2), header=FALSE, sep="|", strip.white=TRUE, stringsAsFactors=FALSE)
names(df2) <- unname(as.list(df2[1,]))
df2 <- df2[-1,] # remove first row
row.names(df2) <- NULL
pandoc.table(df2, 
             caption= "Table 2. Insert title here\n", style = "multiline", split.cells = c(20, 25))
```

System: Linux Mint (17.1)/Ubuntu Trusty RStudio: 0.98.1103 Pander: 0.6.0 Knitr: 1.12.3

Upvotes: 0

Views: 2989

Answers (1)

Thierry
Thierry

Reputation: 18487

Simply adding --- at the top solved the problem on my machine.

---
header-includes:
 - \usepackage[
    singlelinecheck=false,
    justification=centering
    ]{caption}
output:
    pdf_document 
---

```{r table2, echo=FALSE, message=FALSE, warnings=FALSE, results='asis'}
library(pander)
panderOptions('keep.line.breaks', TRUE )
table2 <- "HHHH member | Description of future    
            H11111                | standard model    
            H22222                | low model      
            H33333                | decreasing projection"
df2 <- read.delim(textConnection(table2), header=FALSE, sep="|", strip.white=TRUE, stringsAsFactors=FALSE)
names(df2) <- unname(as.list(df2[1,]))
df2 <- df2[-1,] # remove first row
row.names(df2) <- NULL
pandoc.table(df2, 
             caption= "\\label{tab:MyLabel}Insert title here\n", style = "multiline", split.cells = c(20, 25))
```

In table&nbsp;\ref{tab:MyLabel} blabla

\autoref{tab:MyLabel}

Edit: add a label and reference to the table in LaTeX style

Upvotes: 0

Related Questions