From afc552b89fab02101345ff3a8f5b92f28919c12c Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Wed, 17 Apr 2024 23:12:56 -0700 Subject: [PATCH] LaTeX writer: include `\pandocbounded` with image commands... unless a height/width is specified. --- src/Text/Pandoc/Writers/LaTeX.hs | 7 +++++-- test/command/3450.md | 2 +- test/writer.latex | 18 ++++++++++-------- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/Text/Pandoc/Writers/LaTeX.hs b/src/Text/Pandoc/Writers/LaTeX.hs index e347d240be062..2c5f71819637f 100644 --- a/src/Text/Pandoc/Writers/LaTeX.hs +++ b/src/Text/Pandoc/Writers/LaTeX.hs @@ -1094,8 +1094,11 @@ inlineToLaTeX (Image attr@(_,_,kvs) _ (source, _)) = do inHeading <- gets stInHeading return $ (if inHeading then "\\protect" else "") <> - (if isSVG then "\\includesvg" else "\\includegraphics") <> - options <> braces (literal source'') + (case dimension Width attr `mplus` dimension Height attr of + Nothing -> id + Just _ -> ("\\pandocbounded" <>) . braces) + ((if isSVG then "\\includesvg" else "\\includegraphics") <> + options <> braces (literal source'')) inlineToLaTeX (Note contents) = do setEmptyLine False externalNotes <- gets stExternalNotes diff --git a/test/command/3450.md b/test/command/3450.md index 5ccfd05ff6e9b..2a96c911b90a9 100644 --- a/test/command/3450.md +++ b/test/command/3450.md @@ -8,5 +8,5 @@ % pandoc -fmarkdown-implicit_figures -t latex ![image](lalune.jpg){height=2em} ^D -\includegraphics[width=\textwidth,height=2em]{lalune.jpg} +\pandocbounded{\includegraphics[width=\textwidth,height=2em]{lalune.jpg}} ``` diff --git a/test/writer.latex b/test/writer.latex index b1d784f02cf79..d0beec0679cd7 100644 --- a/test/writer.latex +++ b/test/writer.latex @@ -39,15 +39,17 @@ \usepackage{xcolor} \usepackage{graphicx} \makeatletter -\def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth\else\Gin@nat@width\fi} -\def\maxheight{\ifdim\Gin@nat@height>\textheight\textheight\else\Gin@nat@height\fi} -\makeatother -% Scale images if necessary, so that they will not overflow the page -% margins by default, and it is still possible to overwrite the defaults -% using explicit options in \includegraphics[width, height, ...]{} -\setkeys{Gin}{width=\maxwidth,height=\maxheight,keepaspectratio} +\newsavebox\pandoc@box +\newcommand*\pandocbounded[1]{% scales image to fit in text height/width + \sbox\pandoc@box{#1}% + \Gscale@div\@tempa{\textheight}{\dimexpr\ht\pandoc@box+\dp\pandoc@box\relax}% + \Gscale@div\@tempb{\linewidth}{\wd\pandoc@box}% + \ifdim\@tempb\p@<\@tempa\p@\let\@tempa\@tempb\fi% select the smaller of both + \ifdim\@tempa\p@<\p@\scalebox{\@tempa}{\usebox\pandoc@box}% + \else\usebox{\pandoc@box}% + \fi% +} % Set default figure placement to htbp -\makeatletter \def\fps@figure{htbp} \makeatother \ifLuaTeX