0

When I use common options of \includegraphics like width, height, scale, etc. I would like the resulting size of the image never exceeds its natural size.

In case of width, replacing it with the max width option (borrowed from adjustbox package) it's a solution, but what about scale?

And what if I want to avoid upsizing of images globally?

P.S. I think that a nice solution would be adding a new option to \includegraphics like did some answers to this other question: Size image by area

mmj
  • 1,702
  • 1
    Why using scale at all? – egreg Jul 15 '21 at 12:35
  • 2
    scale is surely not an issue at all. you are just saying the scale factor is at most 1 so that's an author requirement not something for latex to check. – David Carlisle Jul 15 '21 at 12:36
  • @egreg Actually I use scale very rarely, but I would like the solution to be able to avoid upsizing of any image over its natural size also if I use scale or any other way to resize an image (at least with \includegraphics). – mmj Jul 15 '21 at 12:56
  • A case may arise if you are writing a template that uploads images later,
    provided by a user. You may want to keep images within certain dimensions, except if they are smaller than the allotted space.

    In that case, you should check the actual dimensions of the image to be inserted and then decide which \includegraphics setup to apply.

    See https://tex.stackexchange.com/a/185272/161015

    – Simon Dispa Jul 15 '21 at 13:21

1 Answers1

1

Here is a quick fix. THE MWE should produce three identical images and one smaller image. Note that \includelimit only supports size changes.

BTW, this also answers the linked question. Also, do not use \dimen0 with \includegraphics.

\documentclass{standalone}
\usepackage{graphicx}

\newcommand{\includelimit}[2][]% will get bad results with [rotate] {\bgroup \sbox0{\includegraphics{#2}}% \sbox1{\includegraphics[#1]{#2}}% \dimen1=\ht0 \ifdim\ht1<\dimen1 \dimen1=\ht1\fi \dimen2=\wd0 \ifdim\wd1<\dimen2 \dimen2=\wd1\fi \includegraphics[width=\dimen2, height=\dimen1]{#2}% \egroup}

\begin{document} \includegraphics{example-image} \includelimit[width=20cm]{example-image} \includelimit[height=20cm]{example-image} \includelimit[scale=0.5]{example-image} \end{document}

demo


This shows how to replace \includegraphics with the new version. I do NOT recommend this since it no longer supports all the keywords.

\documentclass{standalone}
\usepackage{graphicx}

\let\oldincludegraphics=\includegraphics \renewcommand{\includegraphics}[2][]% will get bad results with [rotate] {\bgroup \sbox0{\oldincludegraphics{#2}}% \sbox1{\oldincludegraphics[#1]{#2}}% \dimen1=\ht0 \ifdim\ht1<\dimen1 \dimen1=\ht1\fi \dimen2=\wd0 \ifdim\wd1<\dimen2 \dimen2=\wd1\fi \oldincludegraphics[width=\dimen2, height=\dimen1]{#2}% \egroup}

\begin{document} \oldincludegraphics{example-image} \includegraphics[width=20cm]{example-image} \includegraphics[height=20cm]{example-image} \includegraphics[scale=0.5]{example-image} \end{document}

John Kormylo
  • 79,712
  • 3
  • 50
  • 120
  • This solution works, thanks! However I wonder if it's possible to add a new option to \includegraphics like did some answers to this other question: https://tex.stackexchange.com/q/446660 – mmj Jul 16 '21 at 10:07