Skip to content

Commit

Permalink
Fix docx scaling for subfigGrid
Browse files Browse the repository at this point in the history
Fixes #394
  • Loading branch information
lierdakil committed Dec 15, 2024
1 parent 2fcd7a0 commit 40852a9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
18 changes: 12 additions & 6 deletions lib-internal/Text/Pandoc/CrossRef/References/Blocks/Subfigures.hs
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,19 @@ runSubfigures (label, cls, attrs) images caption = do
= perc/100.0
| otherwise = error "Only percent allowed in subfigure width!"
blkToRow :: Block -> [Block]
blkToRow (Para inls) = mapMaybe inlToCell inls
blkToRow (Para inls) = map inlToCell $ zip widths $ mapMaybe getImg inls
blkToRow x = [x]
inlToCell :: Inline -> Maybe Block
inlToCell (Image (id', cs, as) txt tgt) = Just $
Figure (id', cs, []) (Caption Nothing [Para txt]) [Plain [Image ("", cs, setW as) txt tgt]]
inlToCell _ = Nothing
setW as = ("width", "100%"):filter ((/="width") . fst) as
getImg x@Image{} = Just x
getImg _ = Nothing
inlToCell :: (Double, Inline) -> Block
inlToCell (w, Image (id', cs, as) txt tgt) =
Figure (id', cs, []) (Caption Nothing [Para txt]) [Plain [Image ("", cs, setW w as) txt tgt]]
inlToCell _ = error "impossible"
setW w as
-- With docx, since pandoc 3.0, 100% is interpreted as "page width",
-- even in table cells. Hence, this hack.
| isDocxFormat opts = ("width", T.pack (show $ w * 100) <> "%"):filter ((/="width") . fst) as
| otherwise = ("width", "100%"):filter ((/="width") . fst) as

replaceSubfigs :: [Inline] -> WS (ReplacedResult [Inline])
replaceSubfigs = (replaceNoRecurse . concat) <=< mapM replaceSubfig
Expand Down
5 changes: 4 additions & 1 deletion lib-internal/Text/Pandoc/CrossRef/Util/Options.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,

{-# LANGUAGE OverloadedStrings#-}

module Text.Pandoc.CrossRef.Util.Options (Options(..), isLatexFormat) where
module Text.Pandoc.CrossRef.Util.Options (Options(..), isLatexFormat, isDocxFormat) where
import Data.Text (Text)
import Text.Pandoc.CrossRef.Util.Template
import Text.Pandoc.CrossRef.Util.Util (isFormat)
Expand Down Expand Up @@ -85,3 +85,6 @@ data Options = Options { cref :: Bool

isLatexFormat :: Options -> Bool
isLatexFormat = ((||) <$> (isFormat "latex") <*> (isFormat "beamer")) . outFormat

isDocxFormat :: Options -> Bool
isDocxFormat = isFormat "docx" . outFormat

0 comments on commit 40852a9

Please sign in to comment.