pdflatexr-markdownpdflatexquarto

splitting table caption into multiple lines


```{r echo=FALSE,warning=FALSE}
#| label: tbl-testTable
#| tbl-cap: Results of Accuracy and measures of confusion matrix Results for different number of insider trading transactions
#| tbl-subcap: ["Results of the benchmarking methods", "Results of different transactions \\newline and features in proposed method Random Forest"]

```

Context: I have two sub-tables with different captions in quarto (RMarkdown).

Issue: When I use \\newline in tbl-cap, the table rendered with multiple lines. When I use \\newline with the tbl-subcap, the line does not split, does not wrap with the table width and the caption overflows the width.

Attempted Solution: I tried \\newline to split the tbl-subcap into multiple line (see reference code above).

Expected Solution: I want the caption string to wrap with the width of the table and not to overflow the table width.

I tried \\newline to split the tbl-subcap into multiple line (see reference code above)


Solution

  • If your output format is pdf, you can latex package subcaption to tweak the subcaptions width using the command \captionsetup[subtable]{width=<a-suitable-width>\textwidth} instead of using the \newline command.

    ---
    title: "Test"
    format: pdf
    include-in-header: 
      text: |
        \usepackage{caption}
        \captionsetup[table]{width=0.9\textwidth}
        \usepackage{subcaption}
        \captionsetup[subtable]{width=0.55\textwidth}
        \usepackage{float}
        \floatplacement{table}{H}
    ---
    
    
    ## Tables
    
    ```{r}
    #| label: tbl-testTable
    #| echo: false
    #| warning: false
    #| layout: "[[1], [-1], [1]]"
    #| tbl-cap: Results of Accuracy and measures of confusion matrix Results for different number of insider trading transactions
    #| tbl-subcap:
    #|      - "Results of the benchmarking methods"
    #|      - "Results of different transactions and features in proposed method Random Forest"
    
    knitr::kable(head(CO2)) 
    knitr::kable(head(CO2))
    ```