0

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

Community
  • 1
  • 1
Richard
  • 95
  • 2
  • 11
  • off-topic: `header=T` and `check.names=F` in `read.delim` and then you can omit the next 3 lines of code. – Fran Oct 09 '18 at 09:29

1 Answers1

0

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

Thierry
  • 18,049
  • 5
  • 48
  • 66
  • Thanks for testing. It appears that I've included the line: panderOptions('table.caption.prefix', '') in the first table in the rmarkdown file, and this has propagated through all of the subsequent tables messing up the captions. I need to work out how to better manually label the tables to avoid, e.g., Table 2. Table 3. ... for a given table. – Richard Apr 27 '16 at 15:09
  • @Richard if you change `table.caption.prefix`, then it will potentially screw up the markdown syntax, see warning on the related [man page](http://rapporter.github.io/pander/#general-options). If you want to add a number prefix to your table, do that inside of the `caption` part. Or open a ticket on GH to add another option for increasing table numbers, I will look into this -- although will not be able to provide a global solution that works with all document formats. – daroczig Apr 27 '16 at 17:23
  • Let LaTeX do the table numbering. – Thierry Apr 27 '16 at 19:01
  • I've added an example of internal links to the table. – Thierry Apr 27 '16 at 19:07
  • This is very helpful. Together with inserting \captionsetup[table]{labelformat=empty} in the header-includes gives me the flexibility to modify table numbering and table referencing in the text. Cheers. – Richard Apr 28 '16 at 04:41