5

Simple question. I would like to do this:

\newcommand{\scatterplotwidth}{0.5\linewidth}
....
\includegraphics[width=0.9\scatterplotwidth]{myfig}

Without the 0.9 it works. Is there a way to have 0.9 times a command variable?

  • Welcome to TeX.SX! Please help us to help you and add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. – user31729 Aug 11 '14 at 22:28
  • 4
    \newcommand{\scatterplotwidth}{\dimexpr0.5\linewidth\relax} – egreg Aug 11 '14 at 22:29
  • Typically one would use \newlength{\scatterplotwidth} ... \setlength{\scatterplotwidth}{0.5\linewidth}. – Werner Aug 11 '14 at 23:36
3

You can define \scatterplotwidth to behave like a dimension parameter that loads a fraction of the current \linewidth by

\newcommand{\scatterplotwidth}{\dimexpr0.5\linewidth\relax}

Then \scatterplotwidth will be legal anywhere a length is needed, for instance in

\includegraphics[width=0.9\scatterplotwidth]{myfig}

By the way, the suggested

\newlength{\scatterplotwidth}
\setlength{\scatterplotwidth}{0.5\linewidth}

would not give the same result, because the value of \scatterplotwidth would be fixed to the size of \linewidth at the moment when \setlength is executed.

A different strategy based on length parameters would be defining a new command:

\newlength{\scatterplotwidth}
\newcommand{\scatterplot}[2][1]{%
   \setlength{\scatterplotwidth}{0.5\linewidth}%
   \includegraphics[width=#1\scatterplotwidth]{#2}%
}

that will be used like

\scatterplot{myfig}

or

\scatterplot[0.9]{myfig}

In the former case, 0.5\linewidth (with the current \linewidth) would be used, in the latter case, the scaling would be applied.

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.