Search code examples
quarto

How to insert hard line break in Quarto document


I'm working with the Quarto document preparation system to programmatically generate large documents using Python. That is I am not using the RStudio or similar editor. All text must be generated as Python strings.

I have a simple table where one cell has several lines worth of content and I figure that I can add line-breaks within the cell. I understand that simple tables cannot include standard line breaks (\n) but I'm wondering if there is a way to insert so-called "hard-line-breaks" in cells. The user manual mentions them but states that only the editor can insert them. Is it possible to do this using string operations in the markdown source text?

Barring that is there a simple way to break lines within a cell in a table?


Solution

  • Quarto tables generated by RStudio visual Markdown editor

    There's not much magic involved when it comes to hard line breaks, just lines that end with a \.
    QMD file created in RStudio with visual mode enabled, a (Grid) table with both regular and hard line breaks looks like this:

    ---
    title: "tbl"
    format: html
    editor: visual
    ---
    
    +-------+-------------+
    | Col1  | Col2        |
    +=======+=============+
    | press | press\      |
    |       | shift+enter |
    | enter |             |
    +-------+-------------+
    |       |             |
    +-------+-------------+
    

    With all the whitespace and linefeeds next to rendered output:

    enter image description here

    While RStudio visual editor starts with simple pipe tables, it switches automatically to grid tables when it encounters a line break or anything else in a cell that's not supported by simple pipe tables.


    Python and grid-style Markdown tables

    For generating Markdown grid tables in Python, tabulate with tablefmt="grid" is quite handy. Or pandas.DataFrame.to_markdown() in case of pandas, also built on top of tabulate.

    from tabulate import tabulate
    
    td = [["press\n\nenter","press\\\nshift+enter"],["no\nbackslash","",]]
    hdr = ["Col1", "Col2"]
    print(tabulate(td,headers=hdr, tablefmt="grid"))
    

    Result:

    +-----------+-------------+
    | Col1      | Col2        |
    +===========+=============+
    | press     | press\      |
    |           | shift+enter |
    | enter     |             |
    +-----------+-------------+
    | no        |             |
    | backslash |             |
    +-----------+-------------+
    

    Rendered with Quarto: enter image description here

    Pandoc multiline tables

    For whatever reason this is not mentioned in Quarto docs, but Pandoc also supports multiline_tables and rendering it with Quarto & Pandoc that are bundled with RStudio works fine, though for hard line breaks it still needs \ before a break. Slighty modified sample from Pandoc documentation:

    -------------------------------------------------------------
     Centered   Default           Right Left
      Header    Aligned         Aligned Aligned
    ----------- ------- --------------- -------------------------
       First    row                12.0 Example of\ 
                                        a row that
                                        spans multiple lines.
    
      Second    row                 5.0 Here's another one. Note
                                        the blank line between
                                        rows.
    -------------------------------------------------------------
    
    Table: Here's the caption. It, too, may span\
    multiple lines.
    

    Renders as: enter image description here