Search code examples
multiple-columnsdocxquarto

Quarto docx and columns


I'm using R / RStudio and Quarto and publishing to a Word document. I'm trying to generate column output using this example for HTML: Two columns layout in Quarto.

When I use this example and publish to HTML it works just fine and show like this:

enter image description here

However, when I try to publish to Word I fail to get the text into columns and it always ends up like this:

enter image description here

Am I trying to do something that Quarto can't do, or is there some secret sauce that's needed to make this work?

Thanks for any /all help.


Solution

  • [Edited for clarity]

    I found 'A' way to do this using Office Open XML (OOXML) to build the sections within the .qmd file

    This is the first section part ...

    ```{=openxml}
    <w:p>
        <w:pPr>
            <w:sectPr>
                <!-- <w:pgSz w:w="12240" w:h="15840"/> -->
                <!-- <w:pgMar w:gutter="0" w:footer="720" w:header="720" w:left="1440" w:bottom="1440" w:right="1440" w:top="1440"/> -->
                <w:cols w:space="720"/>
                <w:jc w:val="both"/>
            </w:sectPr>
        </w:pPr>
    </w:p>
    ```
    

    Put your text and graphs here, including R code.

    And then close the section with this ...

    ```{=openxml}
    <w:p>
        <w:pPr>
            <w:sectPr>
                <w:type w:val="continuous"/>
                <!-- <w:pgSz w:w="12240" w:h="15840"/> -->
                <!-- <w:pgMar w:gutter="0" w:footer="720" w:header="720" w:left="1440" w:bottom="1440" w:right="1440" w:top="1440"/> -->
                <w:cols w:space="720" w:num="2"/>
            </w:sectPr>
        </w:pPr>
    </w:p>
    ```
    

    Here is a full .qmd file that brings it all together: 1) Create a new docx reference-doc (call it custom-reference-doc.docx) and put it in your main .qmd directory; 2) Open the reference-doc and create a new style called 'Column Style' with Calibri Light & Block Text; 3) Create a new (Word) Quarto Document; 4) Copy / paste this code into RStudio

    ---
    title: "Word Document With Columns"
    format: 
        docx:
            reference-doc: custom-reference-doc.docx
            number-sections: true
    subtitle: 'Columns Be Done!'
    abstract: '**Abstract:** Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.'
    subject: 'Columns in docx'
    ---
    
    # Quarto
    
    Quarto enables you to weave together content and executable code into a finished document. To learn more about Quarto see <https://quarto.org>.
    
    <br><br>
    
    ```{=openxml}
    <w:p>
        <w:pPr>
            <w:sectPr>
                <!-- <w:pgSz w:w="12240" w:h="15840"/> -->
                <!-- <w:pgMar w:gutter="0" w:footer="720" w:header="720" w:left="1440" w:bottom="1440" w:right="1440" w:top="1440"/> -->
                <w:cols w:space="720"/>
                <w:jc w:val="both"/>
            </w:sectPr>
        </w:pPr>
    </w:p>
    ```
    ## Columns with text and code
    
    This is the inclusion of R code with surrounding text in the default text style.
    
    ```{r}
    #| warning: false
    
    library(ggplot2)
    x <- ggplot(data = diamonds, 
                aes(x = price,
                    y = depth,
                    color = cut)) +
        geom_point() +
        labs(title = "Diamond Price v. Depth",
             caption = "Diamonds Are A Girls Best Friend") +
        geom_hline(yintercept = mean(diamonds$depth),
                   color = 'blue') +
        theme_bw() +
        theme(plot.title = element_text(hjust = 0.5, colour = 'blue'),
              plot.caption = element_text(colour = 'lightgray'))
    ```
    
    Now the text style changes to a `custom-style` found in the included **reference-doc**, *custom-reference-doc.docx* - Calibri Light + Blocked Text.
    
    ::: {custom-style="Column Style"}
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
    
    ::: {custom-style="Body Text"}
    ## Columns with text and Plot
    
    This is the inclusion of an R plot with surrounding text in the default text style. Also, note the **out.width: 50%** in the code to keep the graph sized within the 2-columns. (Code is shown using Quarto's *Fenced Echo*.)
    
    ```{{r}}
    #| warning: false
    #| out-width: 50%
    
    x
    ```
    
    ```{r}
    #| echo: false
    #| warning: false
    #| out-width: 50%
    
    x
    ```
    :::
    
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
    
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
    
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
    :::
    
    </br></br>
    
    ```{=openxml}
    <w:p>
        <w:pPr>
            <w:sectPr>
                <w:type w:val="continuous"/>
                <!-- <w:pgSz w:w="12240" w:h="15840"/> -->
                <!-- <w:pgMar w:gutter="0" w:footer="720" w:header="720" w:left="1440" w:bottom="1440" w:right="1440" w:top="1440"/> -->
                <w:cols w:space="720" w:num="2"/>
            </w:sectPr>
        </w:pPr>
    </w:p>
    ```