2

I have pictures with different aspect ratios. I want to keep the aspect ratio in each of my inserted pictures.

As an option to \includegraphics I would to have

height = 0.6\textheight

if the width of the image is greather than 0.7*\textwidth when the option height = 0.6\textheight is invoked, and

width = 0.7\textwidth

if the width of the image is less than or equal to 0.7*\textwidth when the option height = 0.6\textheight is invoked.

Is this possible to get automatically?

P.S. I hope my question makes sense.

9
  • store image in box and than measure its width. if it is bigger than 0.7\textwidth than use includegraphics[height=0.6\textheight]{...}, otherwice you use \includegraphics[width=9.7\textwidth]{...} ... but i'm sure that you know this :-)
    – Zarko
    Nov 7, 2017 at 22:05
  • @Zarko Hmm! I'm, not sure exactly how to do thiis; can you please give a complete MWE in an answer? Nov 7, 2017 at 22:07
  • It doesn't make much sense: if the width is more than \textwidth, you'd get an overfull.
    – egreg
    Nov 7, 2017 at 22:43
  • @SvendTveskæg: You should look at the adjustbox package. It allows settings like min height, max height, min width and max width.
    – Werner
    Nov 7, 2017 at 22:46
  • @Werner That looks interesting. Can you give an answer with the proper use? :-) Nov 7, 2017 at 22:53

3 Answers 3

1

With the necessary test for not going off the textwidth:

\documentclass{article}
\usepackage{graphicx}

\newsavebox{\svendbox}

\newcommand{\svendincludegraphics}[2][]{%
  \sbox{\svendbox}{\includegraphics[height=0.6\textheight,#1]{#2}}%
  \ifdim\wd\svendbox>0.7\textwidth
    \ifdim\wd\svendbox>\textwidth
      \resizebox{\textwidth}{!}{\usebox{\svendbox}}%
    \else
      \usebox{\svendbox}%
    \fi
  \else
    \resizebox{0.7\textwidth}{!}{\usebox{\svendbox}}%
  \fi
}

\begin{document}

\noindent
\svendincludegraphics{example-image}

\noindent
\svendincludegraphics[angle=90]{example-image}

\end{document}

As you see you can also specify additional keys for \includegraphics.

enter image description here

1

Here's a solution. It does what Zarko suggested in his comment.

\newbox\mybox
\def\myincludegraphics#1{
    \setbox\mybox\hbox{\includegraphics{#1}
    \ifdim\wd\mybox>0.7\textwidth\relax
        \includegraphics[height=0.6\textheight]{#1}
    \else
        \includegraphics[width=0.7\textwidth]{#1}
    \fi
}
2
  • this would add a lot of space if used in horizontal mode, also appears to be missing a } in the \setbox and missing the height specification on the initial loading. (the request is to check the width after scaling the height) Nov 8, 2017 at 12:33
  • I could delete this; the other answers seem to be much better. Nov 8, 2017 at 15:28
1

i'm late with my answer, but anyway let me elaborate my comment. it is more rude than other, but here we are:

\documentclass{article}
\usepackage{graphicx}
\newsavebox\imagesize
\newlength\imagewidth
\newlength\widthtreshold
\setlength\widthtreshold{0.7\textwidth}

\begin{document}
\sbox\imagesize{\includegraphics[width=0.5\textwidth]{example-image}}
\settowidth\imagewidth{\usebox{\imagesize}}

\ifdim\imagewidth<\widthtreshold
    \includegraphics{example-image-a}
\else
    \includegraphics{example-image-b}
\fi

\medskip
\sbox\imagesize{\includegraphics[width=0.31\textwidth]{example-image}}
\settowidth\imagewidth{\usebox{\imagesize}}

\ifdim\imagewidth<\widthtreshold
    \includegraphics{example-image-a}
\else
    \includegraphics{example-image-b}
\fi
\end{document}

enter image description here

to image i didn't add size of images (that they stay small). if image width is shorter than 0.7\textwidth is shown image A, otherwise image B.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .