Store and reproduce effective graphics scaling
Here is my try:
The three images have different widths (big, medium and small), but the star has the same size on each. I used \settowidth to get the original image width, and \pgfmathsetmacro to divide the dimentions (answere by Matthew Leingang here helped me).
\documentclass{article}
\usepackage{graphicx}
\usepackage{pgf}
\begin{document}
\newlength{\imagewidth}
\settowidth{\imagewidth}{\includegraphics{img_big}}
\pgfmathsetmacro{\myscale}{\the\textwidth/\the\imagewidth}
\includegraphics[width=\textwidth]{img_big}
\includegraphics[scale=\myscale]{img_medium}
\includegraphics[scale=\myscale]{img_small}
\end{document}
You can then add a \newcommand in the preambule:
\newlength{\imagewidth}
\newcommand\getscale[1]{%
\settowidth{\imagewidth}{\includegraphics{#1}}
\pgfmathsetmacro{\myscale}{\the\textwidth/\the\imagewidth}
}
And get the scaling factor for an image using:
\getscale{img_big}
\includegraphics[scale=\myscale]{img_medium}
The following example defines \setscalewidth[<options>]{<image}
(and \setscaleheight
) to set the scale used by LaTeX when including an image using \includegraphics[<options>]{<image>}
:
\documentclass{article}
\usepackage{xparse,graphicx}
\ExplSyntaxOn
\cs_new_eq:NN \calc \fp_eval:n
\ExplSyntaxOff
\makeatletter
\newcommand{\scalefactor}{1}% Default scale factor
\newcommand{\setscalewidth}[2][]{% \setscalewidth[<options>]{<image>}
\settowidth{\@tempdima}{\includegraphics{#2}}% Original width
\settowidth{\@tempdimb}{\includegraphics[#1]{#2}}% Modified width
\edef\scalefactor{\calc{\strip@pt\@tempdimb/\strip@pt\@tempdima}}% modified/original = scale factor
}
\newcommand{\setscaleheight}[2][]{% \setscaleheight[<options>]{<image>}
\settoheight{\@tempdima}{\includegraphics{#2}}% Original height
\settoheight{\@tempdimb}{\includegraphics[#1]{#2}}% Modified height
\edef\scalefactor{\calc{\strip@pt\@tempdimb/\strip@pt\@tempdima}}% modified/original = scale factor
}
\begin{document}
\setscalewidth[width=0.2\textwidth]{example-image}% Define scale width
\includegraphics[width=0.2\textwidth]{example-image} \quad % Print original image using width= specification
\includegraphics[scale=\scalefactor]{example-image} % Print original image using scale= specification
\end{document}
Once set, the scaling factor is stored in \scalefactor
.
With expl3
facilities, you can easily get the scaling factor:
\documentclass{article}
\usepackage{graphicx}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\setscale}{mmm}
{% #1 is the object to scale,
% #2 the width to scale to,
% #3 the name for the scale factor
\hbox_set:Nn \l_tmpa_box { #1 }
\tl_clear_new:c {l_douba_scale_#3_tl}
\tl_set:cx {l_douba_scale_#3_tl}
{
\fp_eval:n
{
round
(
\dim_to_fp:n { #2 }
/
\dim_to_fp:n { \box_wd:N \l_tmpa_box }
, 5
)
}
}
}
\DeclareExpandableDocumentCommand{\usescale}{m}
{
\tl_use:c {l_douba_scale_#1_tl}
}
\ExplSyntaxOff
\begin{document}
\raggedright
\setscale{\includegraphics{example-image}}{\textwidth}{myscale}
\texttt{\usescale{myscale}}
\includegraphics[width=\textwidth]{example-image}
\includegraphics[scale=\usescale{myscale}]{example-image-1x1}
\end{document}