Skip to content

Make <br> a universal linebreak across formats #1767

Closed

Description

@olivroy

I think one thing to make "just work" is recognizing <br> as a line break in all output contexts; this'll avoid any need to call fmt_markdown(). This could probably be done in the process_text() function.

We incidentally also need a similar bit of 'extra' markup to represent cross-output tab stops but that's for another issue.

Originally posted by @rich-iannone in #1765 (comment)


As of gt 0.11

  • \n works for grid format (
    just shows as
    )

  • works for md/html
  • No linebreak seems to work for docx (
    shows as
    and \n acts like a space)
  • Haven't looked for latex yet

For rtf

the text_grob argument of as_gtable() can be used.

#1776 (comment)

Activity

  1. ddsjoberg commented on Jul 11, 2024

    @ddsjoberg
    Contributor

    If possible, would it makes sense to make \n the universal line breaker, since that's how it's already interpreted in many other spaces? Then no one needs to learn another

  2. teunbrand commented on Jul 12, 2024

    @teunbrand
    Contributor

    I second the suggestion to make \n to universal line breaker. I don't understand why <br> would be the preferred option.

  3. olivroy commented on Jul 12, 2024

    @olivroy
    CollaboratorAuthor

    I think my main reasoning is that even if gt supports all formats, it still remains an html-first format. When I construct a complex gt table, I do it interactively in html, and hope for the best when trying to convert to other formats. I am doing so blindly (too much sometimes).

    • html output is easier to preview
    • tested more thoroughly

    • works out of the box for html output without fmt_markdown(), md(), html(), or process_md = TRUE

    So getting a LaTeX output or grid or docx is a matter of converting the html to this other format as close as possible to the html output.

    Similarly, ggplot2 is grid first. Therefore, to make it interactive, plotly::ggplotly() interprets the grid system and translates it to html. (i.e. When I am using ggplotly, I trust that it will convert ggplot2 grid to html properly. ggplots are much easier to preview as static.)

    By default, html ignores line breaks and multiple consecutive spaces.

    # no line break
    data.frame(
      x = c("x\ny")
    ) |> 
      gt()
     data.frame(x = c("x\ny") |> 
       gt() |>
      gt::opt_interactive()

    image

    grid is my new second favorite for easy previewing!

    # line break
     data.frame(x = c("x\ny") |> 
       gt() |>
      as_gtable() |>
      plot()

    image

    Unfortunately, the output is not the same, hence a surprise output in my opinion.

    We have to have one source of truth for this to be effective.

    There are also a lot of contexts where \n is ignored in html and in many situations.

    In md

    Text
    on the same line

    In R,

    cli::cli_abort("x\ny")
    #> Error:
    #> !  x y

    In gt, by default.

    data.frame(
      x = c("x\ny")
    ) |> 
      gt()

    Changing \n as a linebreak in html would be a breaking change. I could live with it, but I think it would feel surprising for many JS / md /css / pandoc experts.

    @rich-iannone feel free to add your grain of salt here!

    One of my first use cases for as_gtable() would be to help create reprex (to proxy the html output), but it doesn't really work currently, because of the line break issue inconsistency.

    There is also the danger of helpers like fmt_markdown(), gt(process_md), md(), html() that may not be respected everywhere, and then there is Quarto who may (or may not) post-process the table.

    Edit: my assessment may have been incorrect. <br> works out of the box only for opt_interactive()

  4. rich-iannone commented on Jul 12, 2024

    @rich-iannone
    Member

    Yeah, it might be a bit eccentric but I think <br> would be best for the reasons you outlined and more. Then, we'd have to have a number of examples and some explanations in the docs to explain this will be a cross-output method for introducing a hard line break.

  5. rich-iannone commented on Aug 17, 2026

    @rich-iannone
    Member

    Implemented and available since v1.1.0.

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