Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

code in figures are centered in docx documents #11937

Open
ejeandel opened this issue Jan 23, 2025 · 7 comments
Open

code in figures are centered in docx documents #11937

ejeandel opened this issue Jan 23, 2025 · 7 comments
Labels
bug Something isn't working docx Issues with the docx format themes Related to HTML theming or any other style related issue (like highlight-style)

Comments

@ejeandel
Copy link

ejeandel commented Jan 23, 2025

Bug description

When a .qmd document is rendered in docx, then each line of the python code in the figure will be centered, which makes it completely unusable.

the bug doesn't appear with the .odt, .tex or .html format

The code-line-numbers functionality also doesn't work. But nothing in the documentation states that it should work for formats other than tex and html, so maybe that's normal.

PS: (Any chance .odt will work with the book format at some point ?)

Steps to reproduce

---
format: docx
---

::: {#fig-python} 

```{.python code-line-numbers="true"}
for i in range(3):
    print(i)
    if i < 2:
        print(i+1)
```

interesting code 
:::



```python
for i in range(3):
    print(i)
    if i < 2:
        print(i+1)
```

Expected behavior

The figure should be left aligned, and the spacing from python should be respected

Actual behavior

Each row of the code in the figure is centered

Your environment

Linux Debian 12,

Quarto check output

Quarto 1.6.40
[✓] Checking environment information...
      Quarto cache location: /home/ejeandel/.cache/quarto
[✓] Checking versions of quarto binary dependencies...
      Pandoc version 3.4.0: OK
      Dart Sass version 1.70.0: OK

      Deno version 1.46.3: OK
      Typst version 0.11.0: OK
[✓] Checking versions of quarto dependencies......OK
[✓] Checking Quarto installation......OK
      Version: 1.6.40
      Path: /opt/quarto/bin

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

[✓] Checking LaTeX....................OK
      Using: Installation From Path
      Path: /usr/bin
      Version: 2024

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

(bug also happens on 1.7.13)

@ejeandel ejeandel added the bug Something isn't working label Jan 23, 2025
@mcanouil
Copy link
Collaborator

mcanouil commented Jan 23, 2025

@ejeandel Did you look at the style of your reference document regarding figures as you chose to make your code a "figure"?
Did you try fig-align?

Side note: could you add the proper YAML header/frontmatter to your example to make it "ready to work" without having the team or users do extra work to set format for instance?

@mcanouil mcanouil added the docx Issues with the docx format label Jan 23, 2025
@ejeandel
Copy link
Author

ejeandel commented Jan 23, 2025

I don't have any reference document, the only thing in the directory is the file I rendered.

fig-align=left works but I'd consider this a workaround, not a solution (fig-align=right for instance would only align right on word, not on tex/html/odt)

@mcanouil
Copy link
Collaborator

mcanouil commented Jan 23, 2025

I don't have any reference document, the only thing in the directory is the file I rendered.

You do, even if it's the default provided by Quarto/Pandoc.
See the guides about docx format:

fig-align=left works but I'd consider this a workaround, not a solution (fig-align=right for instance would only align right on word, not on tex/html/odt)

You are using a figure environment to put code. Figures are centred by default.
The use of fig-align is not a "workaround". Your use of a figure environment in this case is a workaround.

To be clear, almost everything for docx format is controlled by the reference document (few edge cases with opened issues), and this document is directly coming from Pandoc.

Edit: the following alignment issue might be at play in some regards

@mcanouil mcanouil added the themes Related to HTML theming or any other style related issue (like highlight-style) label Jan 23, 2025
@ejeandel
Copy link
Author

I've read the doc, I meant I didn't change the default.

I don't understand the point about fig-align
When I put fig-align=right, the code is aligned right in a .docx file but not in a html/odt/tex file. Are you saying the html/odt/tex backends are wrong ?

@mcanouil
Copy link
Collaborator

mcanouil commented Jan 23, 2025

Styling does not work the same way in LaTeX, Typst, HTML/CSS and Docx (OpenXML).

The alignment in Word document is the same no matter if the "object" is a paragraph, a figure, a table, etc.
Word aligns everything. The other formats align the block called "float" (not the content).
So the block of code is aligned in LaTeX, Typst, and HTML. You might have noticed that actually the block has a 100% width, so alignment does not have any visual impact.
This being said, I am not sure fig-align implementation accounts for things that are not images which is possible since Quarto 1.4, i.e., code blocks have their own style which might take precedence over figure cross-ref styles.

@cderv
Copy link
Collaborator

cderv commented Jan 23, 2025

The problem here is that for Docx document and cross references element, we are putting content into a docx table.

Image

And this table has its content centered aligned. By aligning on the left, the code block is not modified.

Image

So I think this is an improvement to do for our crossref, especially with code content. Note that same results with code listing https://quarto.org/docs/authoring/cross-references.html#code-listings

Image

Related issues

@mcanouil
Copy link
Collaborator

mcanouil commented Jan 23, 2025

Yes, but caution is required because hardcoding alignment values breaks the ability to customise docx as illustrated in other issues.

Maybe, Quarto needs a modified reference document with other styles it could use.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working docx Issues with the docx format themes Related to HTML theming or any other style related issue (like highlight-style)
Projects
None yet
Development

No branches or pull requests

3 participants