0

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.

dave
  • 147
  • 1
  • 1
  • 12

2 Answers2

0

It is always better to also post your code, otherwise it is difficult to point out the mistakes. This code works for me:

---
format: docx
---

::: {layout-ncol=2}


**Left column**

**Right column** 

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc finibus varius venenatis. Vivamus tincidunt, ex vitae sollicitudin ultrices, lectus massa sagittis eros, id interdum erat augue eget lacus. Vivamus nec nibh pulvinar, volutpat odio a, auctor arcu. Donec ac rutrum leo. Aliquam in diam pulvinar, aliquet mauris quis, laoreet lorem. Duis ut feugiat augue, sit amet tincidunt erat. Integer vitae arcu tristique, dapibus nibh at, rhoncus urna. Proin tincidunt massa eu ligula scelerisque feugiat. Vivamus et maximus tortor. Proin suscipit interdum erat in bibendum. Nulla facilisi. Duis commodo sapien aliquam erat pulvinar, a semper turpis porttitor. Nulla facilisi.
and here

Praesent quis ex odio. Integer a interdum nisl, a luctus orci. Aenean tincidunt ornare ligula vitae dapibus. Pellentesque vitae mauris consectetur mi aliquam efficitur. Nullam scelerisque turpis ante, eu bibendum quam commodo id. Mauris sagittis gravida arcu mollis congue. Etiam gravida tellus vestibulum turpis pretium, in eleifend ipsum euismod. Praesent vel velit eu dolor suscipit elementum. Morbi feugiat augue sapien, pretium venenatis ex venenatis egestas. Maecenas eget mi velit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque ornare sollicitudin quam, a consequat est sodales at. Mauris porttitor turpis aliquam lacinia faucibus. Integer at nisi egestas dolor hendrerit mollis.

:::

enter image description here

Julian
  • 6,586
  • 2
  • 9
  • 33
  • Thank you for your reply. I tried `layout-ncol=2` but it has three issues: 1) it places everything within a table, which has all the issues of tables within Word; 2) all text is centered, and I can't figure out how to left / block justify the text; and 3) being a table and not a (Word) sectioned area for columns, text does not flow from left to right column, which is what I was trying to figure out how to do. As for posting the code, I was using the selected example from the linked page and considered it redundant to also post it here. – dave Jun 13 '23 at 12:16
0

[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>
```
dave
  • 147
  • 1
  • 1
  • 12