quarto-dev / quarto-cli

Open-source scientific and technical publishing system built on Pandoc.
https://quarto.org
Other
3.77k stars 308 forks source link

layout_ncol not rendering correctly in the presence of `column: page` #8763

Open rdtmcl opened 7 months ago

rdtmcl commented 7 months ago

Bug description

layout-ncol functionality not working

Steps to reproduce


```{r}
#| label: load-packages
#| echo: false

library(ggplot2)
#| echo: false

```{r}
#| label: fig-mpg
#| fig-cap: "City and highway mileage for 38 popular models of cars."
#| fig-subcap:
#|   - "Color by number of cylinders"
#|   - "Color by engine displacement, in liters"
#| layout-ncol: 2
#| column: page

ggplot(mpg, aes(x = hwy, y = cty, color = cyl)) +
  geom_point(alpha = 0.5, size = 2) +
  scale_color_viridis_c() +
  theme_minimal()

ggplot(mpg, aes(x = hwy, y = cty, color = displ)) +
  geom_point(alpha = 0.5, size = 2) +
  scale_color_viridis_c(option = "E") +
  theme_minimal()

### Expected behavior

I am unable to reproduce the quarto computations example in RStudio. the graphs should render side by side as shown here: https://quarto.org/docs/get-started/computations/rstudio.html

### Actual behavior

every time I render the file the graphs end up stacked vertically as opposed to showing up side by side. 

### Your environment

RStudio 2023.12.1+402 "Ocean Storm" Release (4da58325ffcff29d157d9264087d4b1ab27f7204, 2024-01-28) for windows
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) RStudio/2023.12.1+402 Chrome/116.0.5845.190 Electron/26.2.4 Safari/537.36

### Quarto check output

Quarto 1.4.550
[>] Checking versions of quarto binary dependencies..
      Pandoc version 3.1.11: OK
      Dart Sass version 1.69.5: OK
      Deno version 1.37.2: OK
[>] Checking versions of quarto dependencies......OK
[>] Checking Quarto installation......OK
      Version: 1.4.550
       CodePage: 1252

[>] Checking tools....................OK
      TinyTeX: (not installed)
      Chromium: (not installed)

[>] Checking LaTeX....................OK
      Tex:  (not detected)

[>] Checking basic markdown render....OK

[>] Checking Python 3 installation....(None)

[>] Checking R installation...........OK
      Version: 4.3.2
      knitr: 1.45
      rmarkdown: 2.25                              en

[>] Checking Knitr engine render......OK
mcanouil commented 7 months ago

Thanks for the report. Could you properly format your message to make the code legible?


You can share a self-contained "working" (reproducible) Quarto document using the following syntax, i.e., using more backticks than you have in your document (usually four ````). See https://quarto.org/bug-reports.html#small-is-beautiful-aim-for-a-single-document-with-10-lines.

If you have multiple files (and if it is absolutely required to have multiple files), please share as a Git repository.

````qmd
---
title: "Reproducible Quarto Document"
format: html
engine: knitr
---

This is a reproducible Quarto document using `format: html`.
It is written in Markdown and contains embedded R code.
When you run the code, it will produce a plot.

```{r}
plot(cars)

A placeholder image

The end.

Additionally and if not already given, please share the output of quarto check within a code blocks (i.e., using three backticks ```txt), see https://quarto.org/bug-reports.html#check.

cscheid commented 7 months ago

I can repro this, specifically the combination of column: page and the layout option.

mcanouil commented 7 months ago

FYI, the issue is with code cell column: page while it works properly using fenced divs.

Quarto documentHTML
````qmd --- title: "Reproducible Quarto Document" format: html engine: knitr --- ```{r} #| label: ggplot2 #| include: false #| eval: false library(ggplot2) ggplot(mpg, aes(x = hwy, y = cty, color = cyl)) + geom_point(alpha = 0.5, size = 2) + scale_color_viridis_c() + theme_minimal() ggplot(mpg, aes(x = hwy, y = cty, color = displ)) + geom_point(alpha = 0.5, size = 2) + scale_color_viridis_c(option = "E") + theme_minimal() ``` ## Codel cell option ```{r} #| ref-label: ggplot2 #| label: fig-mpg1 #| fig-cap: "City and highway mileage for 38 popular models of cars." #| fig-subcap: #| - "Color by number of cylinders" #| - "Color by engine displacement, in liters" #| layout-ncol: 2 #| column: page ``` ## Fenced div ::: {.column-page} ```{r} #| ref-label: ggplot2 #| label: fig-mpg2 #| fig-cap: "City and highway mileage for 38 popular models of cars." #| fig-subcap: #| - "Color by number of cylinders" #| - "Color by engine displacement, in liters" #| layout-ncol: 2 ``` ::: ```` image
cscheid commented 7 months ago

I was about to post this; for the time being, you can use the ::: column-page workaround, but we'll fix the bug.

m-delem commented 7 months ago

I met this issue today, I found the ::: column-page solution, but another problem arised: this solution interferes with the pdf render. Here's the example I worked with:

---
title: "test"
format: pdf
---

::: column-page
```{r}
#| label: fig-test
#| fig-cap: LaTeX crash?
#| fig-subcap: 
#|  - Test A
#|  - Test B
#| layout-ncol: 2

library(ggplot2)

iris |> 
  ggplot(aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point()

iris |> 
  ggplot(aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point()

:::


This renders well with `quarto render --to html`, but produces the following error with `--to pdf`:

Rendering PDF running xelatex - 1 This is XeTeX, Version 3.141592653-2.6-0.999995 (TeX Live 2023) (preloaded format=xelatex) restricted \write18 enabled. entering extended mode

updating tlmgr

updating existing packages ERROR: compilation failed- error LaTeX Error: Not in outer par mode.

See the LaTeX manual or LaTeX Companion for explanation. Type H for immediate help. ...

l.209



I searched the internet a bit about this error, and it appears to stem from the fact that Quarto tries to enclose a float in a box, which LaTeX doesn't like, but my understanding of what's going on stops there. For now I'm going to try conditional content to have one chunk enclosed in the div for HTML, and one without for PDF.
m-delem commented 7 months ago

I'll share the (clunky) workaround I found to have both renders working with the same code, if it can help anyone with the same needs:

---
title: "test"
format: 
  html: default
  pdf: default
---

```{r}
#| label: test-plot
#| eval: false
#| include: false

library(ggplot2)

iris |> 
  ggplot(aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point()

iris |> 
  ggplot(aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point()

::: {.content-visible unless-format="pdf"} ::: column-page

#| ref-label: test-plot
#| label: fig-test-html
#| fig-cap: Does it work?
#| fig-subcap: 
#|  - Test A
#|  - Test B
#| layout-ncol: 2

::: :::

::: {.content-visible when-format="pdf"}

#| ref-label: test-plot
#| label: fig-test-pdf
#| fig-cap: Does it work?
#| fig-subcap: 
#|  - Test A
#|  - Test B
#| layout-ncol: 2

:::

The output of this code is in [@fig-test-html]{.content-visible unless-format="pdf"} [@fig-test-pdf]{.content-visible when-format="pdf"}.



The first chunk contains the code that the two others will fetch to produce the figures. The first div shows its content for every format except pdf, and includes the `column-page` option. The second div shows only for PDF and is exactly the same but without the `column-page`, as it crashes the LaTeX render even if the PDF doesn't need it.

A major drawback is that these two chunks can't share the same `label`, so I also needed to use conditional content for inline references: `[@fig-test-html]{.content-visible unless-format="pdf"} ` references the first chunk for every format except pdf, and `[@fig-test-pdf]{.content-visible when-format="pdf"}` references the second chunk for pdf output.

The example above should render properly to both HTML and PDF with `quarto render`.