2

I'd like to know if it's possible to set the width of an image as \textwidth and increase the resulting height without affecting the width.

In other words, putting width=\textwidth also the height is modified to preserving the original ratio of the image, what I'd like to be able to do is increase the height of the image starting from the one obtained with width=\textwidth.

Like this:

\includegraphics[width=\textwidth, height=\obtainedHeight+10em]{image}
Sebastiano
  • 54,118

3 Answers3

2

Welcome to TeX.SE!

People usually like to preserve dimension ration of images :-), however, if like to distorted it, than you can define image height accordingly with:

  • define new \savebox, for example \imagebox
  • in \imagebox store image with width of \textwidth
  • measure height and depth of saved image
  • with \dimexpr macro change natural height of image as you wish, see MWE (Minimal Working Example) below:

enter image description here

\documentclass{article}
\usepackage{graphicx}
%---------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%
\newsavebox\imagebox

\begin{document}
    \begin{figure}
\sbox\imagebox{\includegraphics[width=\linewidth]{example-image-duck}}
        \includegraphics[width=\linewidth,
                         height=\dimexpr\ht\imagebox+\dp\imagebox+10em\relax]{example-image-duck}
        \caption{My distorted duck}
        \label{fig:duck}
    \end{figure}
\end{document}
Zarko
  • 296,517
  • It doesn't work. Just leave height=\dimexpr\linewidth\relax and you will see that the height is bigger than should. – antshar Jun 09 '20 at 21:21
  • 1
    @antshar, it works, however not as OP desire (at the first attempt I misunderstood a question). Now the answer is corrected. – Zarko Jun 09 '20 at 21:38
  • Thank you very much, it works! I've learned a lot looking for the commands that you used, I have only a few questions:
    • There is a particular reason why you used \linewidth instead of \textwidth?
    • The commands \dimexprs and \relax are necessary or you put them to have a more cleaned code?
    – Tommaso Sgarbanti Jun 09 '20 at 22:39
  • @TommasoSgarbanti, \linewidth consider width of environment, where is used. In your case it is equal to \textwidth.. \dimexpr in used example works also without \relax. More about it see https://tex.stackexchange.com/questions/86385/what-is-the-difference-between-relax-and – Zarko Jun 09 '20 at 23:46
1

The simplest way to achieve this is via \resizebox, also provided by graphicx:

\resizebox{\textwidth}{\dimexpr\height+10em}{%
  \includegraphics[width=\textwidth]{<image>}%
}

Here's a complete example:

enter image description here

\documentclass{article}

\usepackage{graphicx}

\begin{document}

\includegraphics[width=3em]{example-image} \quad
\includegraphics[width=3em,height=3em]{example-image} \quad
\includegraphics[width=3em,height=5em]{example-image} \quad
\resizebox{3em}{\dimexpr\height+2em}{%
  \includegraphics[width=3em]{example-image}%
}

\end{document}
Werner
  • 603,163
0

I agree totally with Werner (+1), but I want to add some remarks that are too long for a comment:

First, inside a two columns document, multicols environment, minipages, etc. width=\textwidth maybe is not what you may want, so it is safer be used to use width=\linewidth instead.

But the point for this comment is that it seems odd that you may want increase the relative length \height, that is relative to the width of the figure, with a length that is relative to the font size (10em) because then the extent of the deformation could be different depending on of the width of the image (the narrower the figure, the more it stretches vertically) and also may change unexpectedly by font setting somewhere before.

In the example below, All the images "A","B","C" and "Image" have the same dimensions, and the same code ...

\resizebox{\linewidth}{\dimexpr\height+10em}{%
\includegraphics[width=\linewidth]{example-image-a}}

... does not double the height in the first "A", but it is almost triplicated in the second "A".

So, only in case that this is not what you want, the code for the "B" and "C" images show, that the \dimexpr\height+10em of the \resizebox could be changed by a simpler x.x\height where x.x is the times that you want increase the \height, or even simpler, use a \scalebox{1}[x.x]{...} instead of a \resizebox. Unlike the code for "A", the images "B" and "C" will always be stretched in the same extent.

mwe

\documentclass{article}
\usepackage{graphicx,multicol}
\parindent0pt
\begin{document}
\begin{multicols}{2}
\includegraphics[width=\linewidth]{example-image}
\includegraphics[width=\linewidth]{example-image}
\resizebox{\linewidth}{\dimexpr\height+10em}{\includegraphics[width=\linewidth]{example-image-a}}
\end{multicols}

\begin{multicols}{4}
\includegraphics[width=\linewidth]{example-image}
\includegraphics[width=\linewidth]{example-image}
\resizebox{\linewidth}{\dimexpr\height+10em}{\includegraphics[width=\linewidth]{example-image-a}}
\resizebox{\linewidth}{2.7\height}{\includegraphics[width=\linewidth]{example-image-b}}
\scalebox{1}[2.7]{\includegraphics[width=\linewidth]{example-image-c}}
\end{multicols}

\begin{multicols}{6}\tiny
\includegraphics[width=\linewidth]{example-image}
\includegraphics[width=\linewidth]{example-image}
\resizebox{\linewidth}{\dimexpr\height+10em}{\includegraphics[width=\linewidth]{example-image-a}}
\resizebox{\linewidth}{2.7\height}{\includegraphics[width=\linewidth]{example-image-b}}
\scalebox{1}[2.7]{\includegraphics[width=\linewidth]{example-image-c}}
\end{multicols}
\end{document}

\end{document}
Fran
  • 80,769