ddsjoberg/gtsummary

Bug Report: New line in header not present in Quarto PDF output

Closed this issue · 10 comments

Consider the following MWE:

---
title: "Title"
format:
  pdf: default
---

\newpage

# Section 1

```{r}
#| echo: false
#| warning: false
#| label: tbl-label
#| tbl-cap: "**Table caption**"
library(tidyverse)
library(gtsummary)
trial2 <- trial |> select(trt, age, grade)
trial2 |>
  tbl_summary(by = trt) |>
  add_p()
```

Compile it to html with quarto:

quarto render main.qmd --to html

You get:
image

Not compile to PDF:

quarto render main.qmd --to pdf

The number of observations are in the same line of the column headers:
image

This clutters the column headers and we risk that the page width is not enough to hold the table width.

Even worse, the HTML "preview" is not representative of the PDF output anymore.

Thanks for the post @10b14224cc

The gt package doesn't respect the standard line breaker. So we are stripping it when exporting to PDF.

See the note here: https://www.danieldsjoberg.com/gtsummary/reference/as_gt.html#note

So I have to open a bug report against the gt package?

They are working on a universal linebreak syntax for all output types now in the gt package (rstudio/gt#1767).

We're only removing the linebreak \n, you can try injecting a latex-specific break. I don't personally need PDF output, so I haven't tested it much, but maybe something like modify_header(all_stat_cols() ~ "**{level}** \linebreak N = {n}"). But I am not sure latex code injected like this is evaluated or if gt escapes it.

Another alternative is to export your table to PDF via kableExtra with as_kable_extra(), which does respect the line break but comes with other limitations. For the moment, there is no silver bullet that is the absolute best table-drawing package for all output types (although, I think gt will be there soon).

Can't we just put the N in another row?

I came up with the following multi-output solution. It's really ugly but at least it works.

You will need the PR I submitted, because we need to output curly braces without glue processing them. (we are using {{ to escape a { now)

Moreover, gt::as_latex is latex-excaping the characters, so we need to revert that.

And don't ask me why we need so many \ to match a single latex \ because I don't know....

EDIT: See this StackOverflow question for correct code

You will need the #2048, because we need to output curly braces without glue processing them.

You can get away without using the PR by doubling up the curly brackets.

glue::glue("{{dont_evaluate_me}}")
#> {dont_evaluate_me}

Created on 2024-10-27 with reprex v2.1.1

And don't ask me why we need so many \ to match a single latex \ because I don't know....

Welcome to the hell that is R+latex 😈 🔥 So much escaping...

Oh ok that worked.

Changed the source code.

How can I target those superscripts?

image

They get put outside the makecell:

\makecell{\textbf{Drug A} \\ N = 98}\textsuperscript{\textit{1}}

I am not sure how to fix that. But I think there is a gt function to move the placement of the footnote to the beginning, and a few other options as well.

It seems your original issue is resolved, so I am going to go ahead and close this issue. Happy Coding!

I am not sure how to fix that. But I think there is a gt function to move the placement of the footnote to the beginning, and a few other options as well.

It seems your original issue is resolved, so I am going to go ahead and close this issue. Happy Coding!

Well, I still think it's a hack and it should do that natively without the hack

and it will, as soon as it's supported in gt. on top of that, there are other table printers available with gtsummary that do not require any hacks