1

[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}}
  • 2
    Please provide compilable code. Please explain what you want to do. What is \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. – cfr Jul 26 '18 at 3:23
  • “… I don't want to adjust the scaling each time I change \textwidth”? Why are you changing \textwidth? Why on earth are you doing so continuously, as your wording seems to suggest? – GuM Jul 26 '18 at 3:39
  • @cfr: Thanks for your comment. I can't provide a compilable code because I don't know how to write a compilable code that accomplishes what I want. To address the other questions of yours, I'll modify my question. – Ryo Jul 27 '18 at 5:24
  • No, but you can provide code for a document which sets the problem up so people only have to do the bit you're stuck on. – cfr Jul 27 '18 at 22:24
  • @cfr: Okay, I understand. I've added a compilable code using egreg's solution. It illustrates what I want and why such a scaling is necessary/useful. – Ryo Jul 28 '18 at 12:59
3

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}
  • Quick remark, wouldn't a length be better suited for this like \newlength{\somewidth} \setlength{\somewidth}{0.7\tewtwidth} ? – BambOo Jul 26 '18 at 8:57
  • @sporc: Thank you for your help! My initial post seems to have been unclear. I cannot use width because the images have different sizes. If you specify a single width for multiple images, they will be scaled differently. Your \newcommand{\somescale}{1} doesn't work because the number 1 is not proportional to \textwidth. – Ryo Jul 27 '18 at 6:04
1

I'm not sure what this fixed scaling factor would be useful for.

Anyway, here's a way:

\documentclass{article}

\usepackage{graphicx}
\usepackage{xparse}

\ExplSyntaxOn
\NewExpandableDocumentCommand{\myscale}{}
 {
  \fp_eval:n { round ( \textwidth  / \dim_to_fp:n { 200mm }, 5) }
 }
\ExplSyntaxOff

\begin{document}

\myscale

\bigskip

\includegraphics{example-image}

\bigskip

\includegraphics[scale=\myscale]{example-image}

\end{document}

enter image description here

  • Thank you! My final question is, is it possible to encapsulate the calculation in a macro like \ratio{\textwidth}{200mm} ? It would be useful in other context. The calc package seems to miss this calculation (divide a length by another length to get a dimensionless number). – Ryo Jul 28 '18 at 13:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.