\includegraphics: get the "scale" value of a figure whose size is expressed by "width"
All graphics are written to the PDF with a scale factor. When you specify a size the package just converts it to an appropriate scale factor. You can make \Gscale@box
remember that scale factor for you (source). You can use the same \thelastscalefactor
as many times as you need:
\documentclass{article}
\usepackage{graphicx}
\makeatletter
\let\ORG@Gscale@box\Gscale@box
\long\def\Gscale@box#1{%
\xdef\thelastscalefactor{#1}%
\ORG@Gscale@box{#1}}
\makeatother
\begin{document}
% Measuring the image without actually using it
\sbox0{\includegraphics[width=0.4\textwidth]{example-image}}\quad
\noindent % Using with the scale only
\fbox{\includegraphics[width=0.4\textwidth]{example-image}}\quad
\fbox{\includegraphics[scale=\thelastscalefactor]{example-image}}
\noindent
\fbox{%
\makebox[0.4\textwidth][l]{% <-- trick to align without a tabular
\includegraphics[trim = 65 20 150 20, clip, scale=\thelastscalefactor]{example-image}%
}%
}\quad
\fbox{\includegraphics[trim = 65 20 150 20, clip, scale=\thelastscalefactor]{example-image}}
\end{document}
Output:
You can keep the width=
on the graphics and trim/clip separately so not affecting the scaling, see the last example here:
\documentclass{article}
\usepackage{graphicx,adjustbox}
\begin{document}
\includegraphics[width=.4\textwidth]{example-image}
\bigskip
\includegraphics[clip,trim=1cm .5cm .7cm .1cm, width=.4\textwidth]{example-image}
\bigskip
\adjustbox{clip,trim=1cm .5cm .7cm .1cm}{\includegraphics[width=.4\textwidth]{example-image}}
\end{document}
I never liked the idea of scaling and clipping at the same time, not knowing which comes first (keyword order means nothing). This solution clips first then scales by using \savebox
and \adjustbox
.
\documentclass{article}
\usepackage{graphicx}
\usepackage{adjustbox}
\newsavebox{\tempbox}
\begin{document}
\noindent
\savebox{\tempbox}{\includegraphics[trim = 65 20 150 20, clip]{example-image}}%
\fbox{\adjustbox{width=0.4\textwidth}{\usebox\tempbox}}
\end{document}