Skip to content

feat: allow specification of height and width for landscape #12921

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 30 additions & 17 deletions src/resources/filters/quarto-post/landscape.lua
Original file line number Diff line number Diff line change
@@ -1,34 +1,38 @@
-- landscape.lua
-- Copyright (C) 2024-2024 Posit Software, PBC
--
-- Author: [Edvin Syk](https://github.com/edvinsyk/)
-- Author: [Edvin Syk](https://github.com/edvinsyk/)

function landscape_div()
local ooxml = function(s)
return pandoc.RawBlock('openxml', s)
end


local function to_twips(x)
local num_unit = { x:match("([%d%.]+)%s*(%a+)") }
local num = tonumber(num_unit[1])
local unit = num_unit[2]
if unit == "cm" then
return num * 1440 / 2.54
elseif unit == "in" then
return num * 1440
else
error("Unsupported unit: " .. tostring(unit))
end
end

local docx_section_open = '<w:p><w:pPr><w:sectPr>'
local docx_section_close = '</w:sectPr></w:pPr></w:p>'
-- Define the end of a portrait section for DOCX
local end_portrait_section = ooxml '<w:p><w:pPr><w:sectPr></w:sectPr></w:pPr></w:p>'

-- Define the end of a landscape section for DOCX
local end_landscape_section = ooxml [[
<w:p>
<w:pPr>
<w:sectPr>
<w:pgSz w:h="11906" w:w="16838" w:orient="landscape" />
</w:sectPr>
</w:pPr>
</w:p>
]]

local end_portrait_section = ooxml(docx_section_open .. docx_section_close)

-- LateX commands for starting and ending a landscape section
local landscape_start_pdf = pandoc.RawBlock('latex', '\\begin{landscape}')
local landscape_end_pdf = pandoc.RawBlock('latex', '\\end{landscape}')

local landscape_start_typst = pandoc.RawBlock('typst', '#set page(flipped: true)')
local landscape_end_typst = pandoc.RawBlock('typst', '#set page(flipped: false)')

local function Meta(meta)
metaInjectLatex(meta, function(inject)
inject("\\usepackage{pdflscape}")
Expand All @@ -40,6 +44,15 @@ function landscape_div()
if div.classes:includes('landscape') then
if FORMAT:match 'docx' then
-- DOCX-specific landscape orientation
local height = div.attributes.height or "8.5in"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand why you added unit handling here, but this makes me think that we need to figure out how to get unit handling to behave uniformly across Quarto.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gordonwoodhull What do you think if we took the code you wrote in src/resources/filters/modules/typst_css.lua and made it a general-purpose Lua module to be used across our filters? It would require a bit of refactoring, but I think it's worth it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feel free to take over the PR.
It was mostly a proof of concept of what it could be.

Copy link
Contributor

@gordonwoodhull gordonwoodhull Jun 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure! The routines in typst_css.lua

  1. parse a CSS length with suffix
  2. output the Typst equivalent of a parsed length, where 5 out of 43 length units pass through, and two are translated (px and the pseudo-unit %).

I guess what we'd do is move the parse functions to, like, a parse_css module, and then write length output functions for other output formats, where docx always outputs to twips with no suffix.

And we could leave the door open to generalize colors in the same way later.

local width = div.attributes.width or "11in"

-- Define the end of a landscape section for DOCX
local end_landscape_section = ooxml(
docx_section_open ..
'<w:pgSz w:h="' .. to_twips(height) .. '" w:w="' .. to_twips(width) .. '" w:orient="landscape" />' ..
docx_section_close
)
div.content:insert(1, end_portrait_section)
div.content:insert(end_landscape_section)
elseif FORMAT:match 'latex' then
Expand Down
Loading