Skip to content

Figure div attributes are emitted in a nondeterministic order (alt / data-fig-align swap between identical runs) #14798

Description

@gedeck

I have:

  • searched the issue tracker for similar issues
  • installed the latest version of Quarto CLI
  • formatted my issue following the Bug Reports guide

Bug description

Repeated rendering of a document swaps the alt and data-fig-align attributes. For example:

<div id="fig-plot" class="quarto-float …" alt="A plot" data-fig-align="center">
<div id="fig-plot" class="quarto-float …" data-fig-align="center" alt="A plot">

while this is not a problem for the created HTML pages, it causes unnecessary changes in a repository.

Steps to reproduce

Store this as fig.qmd

```qmd
---
title: attribute order
---

![A plot](example.png){#fig-plot fig-align="center" fig-alt="A plot"}
```

Then run the following shell command

```sh
for i in 1 2 3 4 5 6; do quarto render fig.qmd --to html 2>/dev/null; grep -o 'id="fig-plot"[^>]*' fig.html; done
```

Actual behavior

This is the observed output. Not the changes in order between alt and data-fig-align

id="fig-plot" class="quarto-float quarto-figure quarto-figure-center anchored" alt="A plot" data-fig-align="center"
id="fig-plot" class="quarto-float quarto-figure quarto-figure-center anchored" data-fig-align="center" alt="A plot"
id="fig-plot" class="quarto-float quarto-figure quarto-figure-center anchored" data-fig-align="center" alt="A plot"
id="fig-plot" class="quarto-float quarto-figure quarto-figure-center anchored" alt="A plot" data-fig-align="center"
id="fig-plot" class="quarto-float quarto-figure quarto-figure-center anchored" data-fig-align="center" alt="A plot"
id="fig-plot" class="quarto-float quarto-figure quarto-figure-center anchored" data-fig-align="center" alt="A plot"

The order of the attributes is not stable. If you don't observe the issue, repeat it a few times.

Expected behavior

The order of the attributes should be stable.

I used Claude to identify how this could be addressed and it made the following suggestion:

Cause

FloatRefTarget flattens pandoc's ordered AttributeList into a plain Lua map in its constructor (floatreftarget.lua, as_plain_table from pandoc.lua; share/filters/main.lua:23397 in the 1.10.18 bundle):

tbl.attributes = as_plain_table(tbl.attr.attributes)

and the HTML renderer converts that map straight back into an Attr (float_reftarget_render_html_figure, main.lua:24095):

pandoc.Attr(float.identifier, float.classes or {}, float.attributes or {})

Source order is therefore discarded on construction and re-invented at render time from Lua's hash iteration order, which varies per process (Lua 5.4 seeds string hashing per state). It is random per render, not alternating, and only becomes visible once a float has two or more attributes.

Isolated demo, no quarto involved — same script, 8 pandoc lua processes:

local map = { alt = "A plot", ["data-fig-align"] = "center" }
local attr = pandoc.Attr("fig-plot", {}, map)
local order = {}
for _, kv in ipairs(attr.attributes) do order[#order+1] = kv[1] end
print(table.concat(order, " , "))
data-fig-align , alt     data-fig-align , alt     data-fig-align , alt     data-fig-align , alt
alt , data-fig-align     alt , data-fig-align     data-fig-align , alt     alt , data-fig-align

Suggested fix

pandoc.AttributeList also accepts an ordered list of two-element lists, which is stable across processes (6/6 identical runs):

local keys = {}
for k in pairs(map) do keys[#keys+1] = k end
table.sort(keys)
local ordered = {}
for _, k in ipairs(keys) do ordered[#ordered+1] = { k, map[k] } end
pandoc.Attr(id, classes, ordered)   -- alt , data-fig-align, every time

Two options:

  • Minimal — sort the keys wherever a custom node's attribute map is converted back into an Attr. Deterministic, though alphabetical rather than authored order.
  • Better — do not flatten to a hash: keep attributes in an ordered structure (an AttributeList, or a list of pairs) in as_plain_table and the custom-node constructors, so the authored order round-trips.

Worth noting the scope is wider than figures: any custom AST node whose attributes pass through as_plain_table and back through pandoc.Attr has the same nondeterminism. The figure div above is just where it is most visible.

Your environment

  • IDE VSCode, Positron (latest or close to latest version)
  • OS: MacOS Sequoia 15.7.5

Quarto check output

quarto check
Quarto 1.10.18
[✓] Checking environment information...
      Quarto cache location: /Users/petergedeck/Library/Caches/quarto
[✓] Checking versions of quarto binary dependencies...
      Pandoc version 3.10.0: OK
      Dart Sass version 1.101.0: OK
      Deno version 2.7.14: OK
      Typst version 0.15.1: OK
[✓] Checking versions of quarto dependencies......OK
[✓] Checking Quarto installation......OK
      Version: 1.10.18
      Path: /Applications/quarto/bin

[✓] Checking tools....................OK
      TinyTeX: (external install)
      VeraPDF: 1.28.2
      Chrome Headless Shell: (not installed)

[✓] Checking LaTeX....................OK
      Using: TinyTex
      Path: /Users/petergedeck/Library/TinyTeX/bin/universal-darwin
      Version: 2026

[✓] Checking Chrome Headless....................OK
      Using: Chrome found on system
      Path: /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
      Source: MacOS known location

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

[✓] Checking R installation...........OK
      Version: 4.6.1
      Path: /Library/Frameworks/R.framework/Resources
      LibPaths:
        - /Library/Frameworks/R.framework/Versions/4.6/Resources/library
      knitr: 1.51
      rmarkdown: 2.31

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

[✓] Checking Python 3 installation....OK
      Version: 3.12.6
      Path: /Users/petergedeck/cdd/machine-learning-with-tidymodels/.venv/bin/python3
      Jupyter: 5.9.1
      Kernels: ir, python3

(|) Checking Jupyter engine render....Traceback (most recent call last):
  File "/Applications/quarto/share/jupyter/jupyter.py", line 20, in <module>
    from notebook import notebook_execute, RestartKernel
  File "/Applications/quarto/share/jupyter/notebook.py", line 15, in <module>
    from yaml import safe_load as parse_string
ModuleNotFoundError: No module named 'yaml'
WARN: Error encountered when rendering files
[✓] Checking Jupyter engine render....OK

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions