[Tex/LaTex] Scale images in proportion to textwidth

calculationsdimensionsgraphicsincludegraphicslengths

[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}}

Best Answer

As far as I understand your question, according to your pseudocode, you want to define the width or scale at one point, like an variable. You could use newcommand and store the scale/width in it, like in the example below

\documentclass[a4paper]{article}
\usepackage{graphicx}

\newcommand{\somewidth}{.7\textwidth}
%\newcommand{\somescale}{1}

\begin{document}
\includegraphics[width=\somewidth]{example-image-a}
%\includegraphics[scale=\somescale]{example-image-a}
\end{document}
Related Question