Skip to content

Best practice for cross-output indentation with gt #1027

Open

Description

@ddsjoberg

Hey @rich-iannone ! Thank you for all the amazing work you do on {gt} ❤️

In {gtsummary}, I've been using tab_style() to indent rows in the resulting tables (example below). But this method only works for HTML.

gt::tab_style(
  style = gt::cell_text(indent = gt::px(10), align = "left"),
  locations = gt::cells_body(...)
)

I want to update to use non-breaking spaces for the indentation, as this is supported by HTML, PDF, RTF, and Word. I am using the HTML non-breaking space " " to place the indentation. To get {gt} to recognize the spaces, I am formatting the cells with fmt_markdown().

image

My testing indicates that this solution works well for all the output types. But I don't feel 100% confident about the solution. It feels strange to me to process cells with a function that formats markdown text to get gt to recognize the HTML code in the cell. I do not want to implement an "off-label" use of fmt_markdown() in case the off-label use to later turned off.

Finally the question Is this how you recommend I implement indentation for all the output formats? Is it ok to use the markdown formatting function for HTML code? THANK YOU @rich-iannone !

One limitation of this method, is that it cannot be applied to numeric columns that have a formatting function applied to them. For example, if a column is numeric and I am applying fmt_percent() to the cell, I can't update the formatter to also place the " " in front of the formatted result to indent it.

Activity

  1. ddsjoberg commented on Sep 23, 2022

    @ddsjoberg
    ContributorAuthor

    UPDATE!

    With help from @rich-iannone , we've landed on a near-perfect solution.

    • Previously, I was using tab_style() to include indentation, but this only works for HTML output.
    • We've moved on to using unicode non-breaking spaces. You can prepend a cell with "\U00A0" to insert indentation.
    • One issue with simply adding the unicode characters is that it would only work with character columns. Otherwise, a numeric column would be coerced to character when the unicodes are added.
    • Moreover, if a numeric column were being formatted with one of the fmt_() functions, a formally numeric column would error when we try to format it.
    • In Rich's infinite wisdom, he's included the text_transform() function. This function is processed after the fmt_*(), so we can add the "\U00A0" characters in this function and it will work with both numeric and character columns!
    • The one caveat to this is RTF output. Adding the unicode text via text_transform() yields a malformed unicode character in a rendered document. However, the unicode still works if added directly to the data frame before passing to gt::gt(). This, however, will be addressed in a future release of gt, and the text_transform() solution will work for all output types at that time.

    @rich-iannone would you like me to keep this issue open for the RTF+text_transform() or add that to another issue specifcally for that? Thanks!

  2. rich-iannone commented on Sep 26, 2022

    @rich-iannone
    Member

    @ddsjoberg thanks for the great write up on the state of things with indentation! Let’s keep this issue open until I make those changes to RTF (ensuring the changes to be made fully solve the remaining problem at hand).

  3. self-assigned this
    on Mar 27, 2023
  4. ddsjoberg commented on Mar 27, 2023

    @ddsjoberg
    ContributorAuthor

    Hey hey @rich-iannone !

    Here's a reprex of the RTF indentation issue with the unicode characters,

    Thank you!

    ---
    title: "gt: RTF + text_transform() for indentation"
    format: rtf
    ---
    
    ## Quarto
    
    ```{r}
    library(gt)
    packageVersion("gt")
    
    mtcars[1:4, 1:4] |> 
      gt::gt() |> 
      gt::cols_align(align = "left") |> 
      gt::text_transform(
        locations = gt::cells_body(columns = "mpg", rows = 2:4), 
        fn = function(x) paste0("\U00A0\U00A0\U00A0\U00A0", x)
      )
    ```
    

    image

  5. ddsjoberg commented on Aug 29, 2023

    @ddsjoberg
    ContributorAuthor

    One more thing to consider when identifying one indent solution to rule all the types of output 🍁

    The unicode whitespace solution is working for HTML, Word, and PDF, with that slight escaping issue in RTF. A gtsummary user recently reported as issue where they needed the raw latex code. They were converting the gtwummary table to gt, then converting to raw latex code with gt::to_latex(). The unicode whitespaces/indentations are not preserved, unfortunately. This indentation problem seems like a tricky one! 🤔

  6. bzkrouse commented on Mar 1, 2024

    @bzkrouse

    Amazing summary @ddsjoberg and @rich-iannone!! @rich-iannone - Any updates on when the unicode support will be available for RTF?

  7. mayavvi commented on Aug 19, 2025

    @mayavvi

    Hi @rich-iannone ,

    I have some new findings regarding this issue. In the gtsummary package, the as_gt function (line 176) uses Unicode non-breaking spaces ("\U00A0") as implemented by @ddsjoberg. In the gt package, the utils_render_rtf function (line 845) first checks whether x is UTF-8, but this ignores that "\U00A0" is a Unicode character. This may be causing the issue Daniel reported earlier.

    Could you help confirm if my understanding is correct? Thanks!

    Image Image
  8. ande4148 commented on Feb 26, 2026

    @ande4148

    Hi @rich-iannone . If this thread is still being monitored, I'd like to point out that the same indent issue is present for tab_stub_indent(). Renders fine to html and in RStudio, but loses indent on conversion to pdf. I will try the unicode solution if that is still the workaround. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions