quarto-dev / quarto-cli

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

Wide table with column: page does not fill the page width in PDF #5960

Open davidpomerenke opened 1 year ago

davidpomerenke commented 1 year ago

Bug description

Wide tables do not stretch beyond the body column in PDFs.

image

(For HTML, it is working:)

image

Steps to reproduce

---
title: Example
format:
  pdf:
    papersize: a4
    fontsize: 9pt
    geometry:
     - showframe
  html:
    fontsize: 9pt
---

```{python}
# | label: tbl-official-overview
# | tbl-cap: Official overview.
# | column: page
# | echo: false

from IPython.display import Markdown
import pandas as pd

md = pd.read_csv("table.csv", sep=";").to_markdown(index=False)
print("\n".join(md.split("\n")[:4]))
Markdown(md)

`table.csv`:

````csv
region;city;kpop;cap?;reg?;obs?;2015;2016;2017;2018;2019;2020;2021;2022
Baden-Württemberg;Karlsruhe;283; ;x; ; ; ; ; ; ; ;490;467
Bayern;München;1,260;x;x; ; ; ; ;1,145;1,170;1,364;1,983;1,169
Berlin;Berlin;3,426;x;x;x; ; ; ;4,290;5,405;5,890;6,207;6,442
Brandenburg;Potsdam;182;x;x; ; ; ; ; ;214;238;287;326
Bremen;Bremen;546;x; ; ; ; ; ; ;654;631;623;721
Hamburg;–;–; ; ; ; ; ; ; ; ; ; ; 
Hessen;Wiesbaden;278;x;x; ; ; ; ; ;159;235;304;256
Mecklenburg-Vorpommern;–;–; ; ; ; ; ; ; ; ; ; ; 
Niedersachsen;–;–; ; ; ; ; ; ; ; ; ; ; 
Nordrhein-Westfalen;Köln;963; ;x; ; ; ; ;1,037; ; ; ;1,551
Nordrhein-Westfalen;Wuppertal;360; ;x; ; ; ; ; ; ; ; ;209
Rheinland-Pfalz;Mainz;217;x;x; ; ; ; ;255;248;305;312;411
Saarland;–;–; ; ; ; ; ; ; ; ; ; ; 
Sachsen;Dresden;556;x;x; ; ; ; ; ; ;164;292;405
Sachsen-Anhalt;Magdeburg;235;x;x;x;215;167;135;175;222;312;411;435
Schleswig-Holstein;Kiel;246;x;x; ; ; ; ; ; ; ;272;341
Thüringen;Erfurt;213;x; ; ; ; ; ; ;305;285;331;351
 ;sum;8,771;10;11;2;215;167;135;6,902;8,377;9,424;11,512;13,084

Markdown that is produced as intermediate step:

| region                 | city      | kpop   | cap?   | reg?    | obs?    | 2015   | 2016   | 2017   | 2018   | 2019   | 2020   | 2021   | 2022   |
|:-----------------------|:----------|:-------|:-------|:--------|:--------|:-------|:-------|:-------|:-------|:-------|:-------|:-------|:-------|
| Baden-Württemberg      | Karlsruhe | 283    |        | x       |         |        |        |        |        |        |        | 490    | 467    |
| Bayern                 | München   | 1,260  | x      | x       |         |        |        |        | 1,145  | 1,170  | 1,364  | 1,983  | 1,169  |
| Berlin                 | Berlin    | 3,426  | x      | x       | x       |        |        |        | 4,290  | 5,405  | 5,890  | 6,207  | 6,442  |
| Brandenburg            | Potsdam   | 182    | x      | x       |         |        |        |        |        | 214    | 238    | 287    | 326    |
| Bremen                 | Bremen    | 546    | x      |         |         |        |        |        |        | 654    | 631    | 623    | 721    |
| Hamburg                | –         | –      |        |         |         |        |        |        |        |        |        |        |        |
| Hessen                 | Wiesbaden | 278    | x      | x       |         |        |        |        |        | 159    | 235    | 304    | 256    |
| Mecklenburg-Vorpommern | –         | –      |        |         |         |        |        |        |        |        |        |        |        |
| Niedersachsen          | –         | –      |        |         |         |        |        |        |        |        |        |        |        |
| Nordrhein-Westfalen    | Köln      | 963    |        | x       |         |        |        |        | 1,037  |        |        |        | 1,551  |
| Nordrhein-Westfalen    | Wuppertal | 360    |        | x       |         |        |        |        |        |        |        |        | 209    |
| Rheinland-Pfalz        | Mainz     | 217    | x      | x       |         |        |        |        | 255    | 248    | 305    | 312    | 411    |
| Saarland               | –         | –      |        |         |         |        |        |        |        |        |        |        |        |
| Sachsen                | Dresden   | 556    | x      | x       |         |        |        |        |        |        | 164    | 292    | 405    |
| Sachsen-Anhalt         | Magdeburg | 235    | x      | x       | x       | 215    | 167    | 135    | 175    | 222    | 312    | 411    | 435    |
| Schleswig-Holstein     | Kiel      | 246    | x      | x       |         |        |        |        |        |        |        | 272    | 341    |
| Thüringen              | Erfurt    | 213    | x      |         |         |        |        |        |        | 305    | 285    | 331    | 351    |
|                        | sum       | 8,771  | 10     | 11      | 2       | 215    | 167    | 135    | 6,902  | 8,377  | 9,424  | 11,512 | 13,084 |

The rendered table (just to show that the Markdown is correct):

region city kpop cap? reg? obs? 2015 2016 2017 2018 2019 2020 2021 2022
Baden-Württemberg Karlsruhe 283 x 490 467
Bayern München 1,260 x x 1,145 1,170 1,364 1,983 1,169
Berlin Berlin 3,426 x x x 4,290 5,405 5,890 6,207 6,442
Brandenburg Potsdam 182 x x 214 238 287 326
Bremen Bremen 546 x 654 631 623 721
Hamburg
Hessen Wiesbaden 278 x x 159 235 304 256
Mecklenburg-Vorpommern
Niedersachsen
Nordrhein-Westfalen Köln 963 x 1,037 1,551
Nordrhein-Westfalen Wuppertal 360 x 209
Rheinland-Pfalz Mainz 217 x x 255 248 305 312 411
Saarland
Sachsen Dresden 556 x x 164 292 405
Sachsen-Anhalt Magdeburg 235 x x x 215 167 135 175 222 312 411 435
Schleswig-Holstein Kiel 246 x x 272 341
Thüringen Erfurt 213 x 305 285 331 351
sum 8,771 10 11 2 215 167 135 6,902 8,377 9,424 11,512 13,084

Expected behavior

The table should fill both the body column and the margin.

Actual behavior

In the PDF, the table only fills the body column. The table caption is too far at the left.

In the HTML, things work.

https://quarto.org/docs/authoring/article-layout.html#pdflatex-layout states that PDF does not support all layout options in PDF, but to my understanding this one should work.

Your environment

Quarto check output

[✓] Checking versions of quarto binary dependencies... Pandoc version 3.1.1: OK Dart Sass version 1.55.0: OK [✓] Checking versions of quarto dependencies......OK [✓] Checking Quarto installation......OK Version: 1.3.361 Path: /Applications/quarto/bin

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

[✓] Checking Python 3 installation....OK Version: 3.10.10 Path: /Users/david/Repositories/protest-impact/.venv/bin/python Jupyter: 5.3.0 Kernels: ir, python3

[✓] Checking Jupyter engine render....OK

[✓] Checking R installation...........OK Version: 4.2.1 Path: /Library/Frameworks/R.framework/Resources LibPaths:

[✓] Checking Knitr engine render......OK

mcanouil commented 1 year ago

I don't believe this to be a bug, but a known limitation, see https://quarto.org/docs/authoring/article-layout.html#pdflatex-layout.

I let @dragonstyle confirm this before removing "bug" label.

davidpomerenke commented 1 year ago

Therefore I would argue that this should be treated as a bug.

How it works for figures:

---
title: Example
format:
  pdf:
    papersize: a4
    fontsize: 9pt
    geometry:
     - showframe
  html:
    fontsize: 9pt
---

```{python}
# | label: tbl-official-overview
# | tbl-cap: Official overview.
# | column: page
# | echo: false

from IPython.display import Markdown
import pandas as pd

md = (
    pd.read_csv("tables/official-overview.csv", sep=";")
    .iloc[:7]
    .to_markdown(index=False)
)
Markdown(md)
# | label: fig-test
# | fig-cap: Test overview.
# | column: page
# | echo: false

import matplotlib.pyplot as plt

categories = ["A", "B", "C", "D"]
values = [10, 20, 15, 25]
plt.figure(figsize=(16, 4))
plt.barh(categories, values)
plt.show()


<img width="808" alt="image" src="https://github.com/quarto-dev/quarto-cli/assets/46022183/bf149b1f-f17a-449d-8b57-63d3d2704ff4">
mcanouil commented 1 year ago

Thanks for the follow up it makes things much clearer with the figure working as expected.

davidpomerenke commented 1 year ago

Workaround: Using LaTeX to create the table. It is not a Quarto table then (no label and caption) but displays the wide table properly. (The snippet will not be executable, should just give the idea.)

---
include-in-header:
  text: |
    \usepackage[l3]{csvsimple}
    \usepackage{booktabs}
---

```{python}
# | echo: False
# # | label: queries
# # | tbl-cap: Queries for the media.
# # | column: page

df = climate_query_table()
Markdown(df.to_markdown(index=False))
df.to_csv(tables / "queries.csv", index=False)

:::{.column-page} \begingroup \small\selectfont \csvautobooktabular[separator=comma,respect all]{/Users/david/Repositories/protest-impact/report/tables/queries.csv} \endgroup :::

dragonstyle commented 1 year ago

Adding a note - at least one issue is that the computed long table is using columnwidth when computing sizes.