19

I have a macro to define the scale of my figures:

\newcommand{\myscale}{0.35}

Now I have a figure that needs to be scaled twice as big, so I tried to use something like this:

\begin{figure}[hbt]
  \includegraphics[scale=2\myscale]{my-image}
  \caption{My Caption}
  \label{fig:my-label}
\end{figure}

This results a very big image, as 2\scale expands to 20.35...

I tried/found:

  • Using scale=2*\myscale, but still get errors
  • Some more things using calc, again did not work
  • \real{}, but it does not seem to work also
  • intcalc, but this only seems to work for integers, which is not always the case for \myscale.
  • pgf package, but it seems a complex for a simple multiplication
  • fp package works, but still somewhat complex

The easiest way (that works for me), is this fp-based implementation:

\begin{figure}[hbt]
  \FPeval\calculatedScale{2*\myscale}
  \includegraphics[scale=\calculatedScale]{my-image}
  \caption{My Caption}
  \label{fig:my-label}
\end{figure}

Am I missing something? Is there a more simple/elegant solution? Preferably directly usable with the \includegraphics command without creating the additional \calculatedScale.

0

3 Answers 3

10

The l3fp package allows expandable calculations with floating point numbers. Expandability means that you don't need to store the result in a temporary variable.

\documentclass{article}
\usepackage{graphicx} % 

%%%%
% Provide the command \fpeval as a copy of the code-level \fp_eval:n.
\usepackage{expl3}[2012-07-08]
\ExplSyntaxOn
\cs_new_eq:NN \fpeval \fp_eval:n
\ExplSyntaxOff
%%%%

\begin{document}
\newcommand{\myscale}{0.53}
\includegraphics[scale=\fpeval{2*\myscale}]{your-image}
\end{document}
6
  • Your example give this error: Runaway argument? scale=\fpeval {2*\myscale }]{your-image} \end {document} ! Paragraph ended before \Gin@iii was complete.. Is there anything that might went wrong with the \fpeval{} command?
    – Veger
    May 22, 2013 at 13:44
  • 1
    @Veger You need the graphicx package for keyval syntax: I've edited the answer.
    – Joseph Wright
    May 22, 2013 at 13:53
  • Sorry about that, I had not tested appropriately. May 22, 2013 at 13:59
  • It helps a bit, as I get loads of new errors like Undefined control sequence, Missing number, treated as zero and Illegal unit of measure (pt inserted). With scale=0.7 all errors disappear. Is there another problem with the syntax?
    – Veger
    May 22, 2013 at 14:13
  • @Veger: your expl3 is too old, it does not have \fp_eval:n. May 22, 2013 at 14:23
6

Maybe simply (second version is with the scaling):

\documentclass{article}
\usepackage{graphicx}


\begin{document}

\includegraphics[scale=0.2]{it}

\scalebox{2}{\includegraphics[scale=0.2]{it}

\end{document}
1
  • This is indeed also a possibility! Although it seems redundant to use two scales/commands. Are there any drawbacks to this method?
    – Veger
    May 22, 2013 at 14:15
0

Bruno's answer above was super helpful, thanks for that. I didn't even know about this facility in latex3 and was trying to use pgfmath operations which was not working.

Here is an overleaf project https://www.overleaf.com/read/kbbhfkpywfnp that has sample of making this table chartbar chart

The latex for this is

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{xcolor}

% makes a table with bars that scale to desired maximum percent or degrees values

% based on https://tex.stackexchange.com/questions/115558/simple-way-to-multiply-two-values
%%%%
% Provide the command \fpeval as a copy of the code-level \fp_eval:n.
\usepackage{expl3}
\ExplSyntaxOn
\cs_new_eq:NN \fpeval \fp_eval:n
\ExplSyntaxOff
%%%%

\begin{document}
\pagestyle{empty}

\newcommand{\barwidth}{10} % cm
\newcommand{\percentscale}{100} % max scale for percent bars
\newcommand{\degscale}{90} % max scale for degree bars

\def\pcbar#1{%%
  #1s\% & {\color{red}\rule{\fpeval{#1/\percentscale*\barwidth} cm}{8pt}}%
}
\def\degbar#1{%%
  #1s$^\circ$ & {\color{red}\rule{\fpeval{#1/\degscale*\barwidth} cm}{8pt}}%
}

\begin{table*}
\begin{tabular}{rl}
 \pcbar{90}\\
 \pcbar{60}\\
 \pcbar{30}\\

\degbar{10}\\
\degbar{50}\\
\degbar{89}\\
\end{tabular}
\end{table*}

\end{document}
2
  • 3
    You can simply do \usepackage{xfp} that provides \fpeval.
    – egreg
    Jan 11, 2022 at 10:46
  • Thanks! This is much better and more understandable. Hopefully supported by journal submission tex installations. I updated the overleaf project linked above. Jan 12, 2022 at 11:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .