Skip to content

Commit 37ce214

Browse files
claude: Fix listing centering in Typst book templates
Add show rule injection to left-align listing figures in Typst output. This overrides template centering for listing-kind figures by injecting a show-set rule after the template is applied. - Add injected_listing_align_rule tracking variable - Inject #show figure.where(kind: "quarto-float-lst"): set align(start) - Remove old workaround that only worked inside figure content - Add ensurePdfTextPositions test for listing alignment Fixes #9724 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 21dd5a0 commit 37ce214

3 files changed

Lines changed: 21 additions & 7 deletions

File tree

‎src/resources/filters/customnodes/floatreftarget.lua‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
local drop_class = require("modules/filters").drop_class
55
local patterns = require("modules/patterns")
66

7+
-- Track whether we've injected the Typst show rule for listing alignment
8+
local injected_listing_align_rule = false
9+
710
local function split_longtable_start(content_str)
811
-- we use a hack here to split the content into params and actual content
912
-- see https://github.com/quarto-dev/quarto-cli/issues/7655#issuecomment-1821181132
@@ -976,6 +979,16 @@ end, function(float)
976979
local kind = "quarto-float-" .. ref
977980
local supplement = titleString(ref, info.name)
978981

982+
-- Inject show rule to left-align listing figures (only once per document)
983+
-- This overrides any template centering for listing-kind figures
984+
-- https://github.com/quarto-dev/quarto-cli/issues/9724
985+
if ref == "lst" and not injected_listing_align_rule then
986+
injected_listing_align_rule = true
987+
quarto.doc.include_text("before-body", [[
988+
#show figure.where(kind: "quarto-float-lst"): set align(start)
989+
]])
990+
end
991+
979992
-- Check if this is a margin figure (has .column-margin or .aside class)
980993
-- Skip margin handling for subfloats - the parent handles margin placement
981994
if hasMarginColumn(float) and not float.parent_id then
@@ -1096,13 +1109,6 @@ end, function(float)
10961109
caption_location = "bottom"
10971110
end
10981111

1099-
if (ref == "lst") then
1100-
-- FIXME:
1101-
-- Listings shouldn't emit centered blocks.
1102-
-- We don't know how to disable that right now using #show rules for #figures in template.
1103-
content:insert(1, pandoc.RawBlock("typst", "#set align(left)"))
1104-
end
1105-
11061112
if float.has_subfloats then
11071113
-- subrefnumbering defaults to quarto-subfloat-numbering in quarto_super
11081114
-- (simple "1a" for articles, chapter-based "1.1a" for books)

‎tests/docs/smoke-all/typst/orange-book/chapter1.qmd‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ See @lem-triangle for the inequality.
8888
8989
## Code Listings {#sec-code-listings}
9090
91+
LISTING_BODY_ALIGN_TEST is body text that should left-align with code listings.
92+
9193
```{#lst-hello .python lst-cap="Hello World in Python"}
9294
def hello():
9395
print("Hello, World!")

‎tests/docs/smoke-all/typst/orange-book/index.qmd‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,12 @@ _quarto:
248248
- "McCarthy \\(1960\\) inspired this classification"
249249
- "Codd \\(1970\\) informs our approach"
250250
- "Lamport \\(1978\\) for temporal considerations"
251+
ensurePdfTextPositions:
252+
- # Code listings should be left-aligned with body text, not centered
253+
- subject: "hello():"
254+
relation: leftAligned
255+
object: "LISTING_BODY_ALIGN_TEST"
256+
tolerance: 25
251257
---
252258

253259
# Preface {.unnumbered}

0 commit comments

Comments
 (0)