[I rewrite this post to address the questions posed in the comments.]
First I formulate what I want to accomplish as clearly as I can. After that, I explain why I want to do that.
My question: How to scale multiple images by one single scaling factor which is proportional to \textwidth
?
In a pseudo code, I want to accomplish this:
myscale = \textwidth / 200mm
\includegraphics[scale=myscale]{fig1}
\includegraphics[scale=myscale]{fig2}
where "200mm" is a constant length that I've chosen as an example. But my question doesn't depend on its value. I cannot provide a compilable code because I don't know how to write such a code.
Please don't pay too much attention to the "/ 200mm
" part. As long as myscale
is proportional to \textwidth
, I don't care how to get the result. But, one problem is that \textwidth
has a dimension of length whereas myscale
is dimensionless. So, we want to divide \textwidth
by some length to get a nondimensional number.
Now why do I want to do that?
I write a manuscript, in which I include many images. I arrange multiple images and add annotations like "(a)" to produce a single figure. For that purpose, I use the picture
environment. The unitlength
of the picture
environment is set to 0.01\textwidth
. If the images themselves are scaled proportionally to \textwidth
, the whole figure doesn't change even when you change \textwidth
.
Now why do I change \textwidth
so often?
1) I write my manuscript without caring about which journal I will submit it. Only when I'm close to the final version, do I decide which journal to submit it and I switch to the publisher's style file, which will change \textwidth
.
2) I minimize the margins when I print out my manuscript to save paper.
3) I sometimes put the whole picture environment in minipage
to scale the figure as a whole. (Remember minipage
changes \textwidth
.)
4) Some of my figures are used in my presentations, which I produce using the beamer
class, which uses a drastically different \textwidth
.
Edit: Here is a solution using egreg's code. It demonstrates how my idea is used. Note that when the figure is put in the minipage
environment, all elements which constitute the figure and their relative positions are proportionately scaled, so that the scaled figure looks similar to the original figure, except the font size isn't scaled, which is fine in my use cases.
\documentclass{article}
\usepackage{graphicx}
\usepackage{xparse}
\ExplSyntaxOn
\NewExpandableDocumentCommand{\myscale}{}
{
\fp_eval:n { round ( \textwidth / \dim_to_fp:n { 200mm }, 5) }
}
\ExplSyntaxOff
\newcommand{\myfigure}{%
\setlength{\unitlength}{0.01\textwidth}
\fbox{%
\begin{picture}(70,90)
\put(0,45){\includegraphics[scale=\myscale]{example-image-a}}
\put(0, 0){\includegraphics[scale=\myscale]{example-image-b}}
\put(0,85){This is fig2.}
\put(0,40){This is fig1.}
\end{picture}%
}
}%\myfigure
\begin{document}
\myfigure
\begin{minipage}{0.7\textwidth}
\myfigure
\end{minipage}
\end{document}
My final question is, is it possible to encapsulate the calculation of the scale in a macro such that you can use it as \newcommand{\myscale}{\ratio{\textwidth}{200mm}}
? I tried the following but I got ! Missing number, treated as zero.
/<to be read again>
/\tex_protected:D
when the macro \myscale
is used.
\newcommand{\ratio}[2]{%
\ExplSyntaxOn
\NewExpandableDocumentCommand{\myscale}{}
{
\fp_eval:n { round ( #1 / \dim_to_fp:n { #2 }, 5) }
}
\ExplSyntaxOff
}
\newcommand{\myscale}{\ratio{\textwidth}{200mm}}
\somelength
? Why should the images being different sizes stop you using.4\textwidth
? And why would you need to change it if\textwidth
changes? Isn't the point of using\textwidth
that you don't have to? Where is the 200mm from? Right now, I have no idea what you're trying to do or why the code you know doesn't do it.\textwidth
”? Why are you changing\textwidth
? Why on earth are you doing so continuously, as your wording seems to suggest?