Better handling for Figures in ODT output? #11033
Replies: 2 comments
-
As a workaround, a python macro for LibreOffice that resizes all too-big images to document width (thanks to ChatGPT though it took several rounds to get it to work): https://codeberg.org/iandol/dotfiles/src/branch/main/bin/FitImagesToPageWidth.py |
Beta Was this translation helpful? Give feedback.
-
I checked the ODF spec and you can at least get relative scaling to the containing frame's width with In fact Pandoc does already support this when setting an explicit image link attribute with {width=100%}: <text:p text:style-name="FigureWithCaption">
<draw:frame draw:name="img2" style:rel-width="100.0%" style:rel-height="scale" svg:width="3000.0pt" svg:height="985.0pt">
<draw:image xlink:href="Pictures/1.png" xlink:type="simple" xlink:show="embed" xlink:actuate="onLoad" />
</draw:frame>
</text:p> ...but at least my writing editor does not add link attributes by default so this doesn't help me. The easiest solution therefore is a Lua filter. This Lua filter sets images > 600px1 to 100% width if no width attribute has been set: -- if no image width passed set images > 600px to 100% width
function Image(im)
local sizes = nil
local mediatype, content = pandoc.mediabag.fetch(im.src)
if mediatype and mediatype:match("^image/") then
sizes = pandoc.image.size(content)
end
if sizes and sizes.width > 600 and not im.attributes.width then
im.attributes.width = "100%"
im.attributes.height = nil
end
return im
end Having 100% set as the default of ODT conversions would certainly be better for me (most academic images fill the container width), but I recognise this would change existing behaviour and be detrimental for people who include many small images in their work, so Lua filters rock! Footnotes
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I always have trouble with ODT not sizing images to the page width as a default. Given this simple markdown and only
pandoc -t test.[docx|odt]
(xkcd.png
has 300PPI set,xkcd2.png
has no PPI info):We get very different outputs between Word which scales to document size:
…and ODT:
The two images are identical in size (2953x970px) apart from the PPI metadata.
When the same image is manually imported in LibreOffice its default is to scale it to page width (both the with-PPI and no-PPI variants):
Pandoc's XML for the ODT looks like:
The manually imported ODT looks like:
LibreOffice obviously knows the document size and can adjust the width and height appropriately. I assume the problem is Pandoc doesn't know the document size. For DOCX the size info is simply removed and Word chooses appropriately, but perhaps ODT demands a size and the problem is how to choose one? Is there any way to improve this from Pandoc's side?
Here is the image (PPI removed) I used for testing if you want to reproduce this testcase.
Beta Was this translation helpful? Give feedback.
All reactions