You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Best practice for cross-output indentation with gt #1027
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().
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.
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!
@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).
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! 🤔
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!
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!
Activity
ddsjoberg commentedon Sep 23, 2022
UPDATE!
With help from @rich-iannone , we've landed on a near-perfect solution.
tab_style()to include indentation, but this only works for HTML output."\U00A0"to insert indentation.fmt_()functions, a formally numeric column would error when we try to format it.text_transform()function. This function is processed after thefmt_*(), so we can add the"\U00A0"characters in this function and it will work with both numeric and character columns!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 togt::gt(). This, however, will be addressed in a future release of gt, and thetext_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!rich-iannone commentedon Sep 26, 2022
@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).
ddsjoberg commentedon Mar 27, 2023
Hey hey @rich-iannone !
Here's a reprex of the RTF indentation issue with the unicode characters,
Thank you!
ddsjoberg commentedon Aug 29, 2023
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! 🤔bzkrouse commentedon Mar 1, 2024
Amazing summary @ddsjoberg and @rich-iannone!! @rich-iannone - Any updates on when the unicode support will be available for RTF?
mayavvi commentedon Aug 19, 2025
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!
ande4148 commentedon Feb 26, 2026
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!