[Tex/LaTex] How to do simple calculation in LaTeX

calculations

Consider the following macro:

\newcommand{\sxfigure}[4]{
\includegraphics[width=#1*#3 cm, height=#2*#3 cm]{#4}
}

#1 and #2 is some value to determine the width and height of the figure, #3 is to control the zoom ratio. However, the above code doesn't work. Could someone provide a solution? I have tried the following, still not work. Could some point out the mistake?

\usepackage{pgf}
\newcommand{\sxmultiply}[2]{\pgfmathparse{#1*#2}\pgfmathresult}
\newcommand{\sxfigure}[5]{
\begin{figure}
\begin{center}
\includegraphics[width=$\sxmultiply{#3}{#5}$ cm, height=$\sxmultiply{#4}{#5}$ cm]{#1}
\end{center}
\caption{#2}
\end{figure}
}

Best Answer

The fp package is an easy-to-use arithmetic bundle that could help here:

enter image description here

\documentclass{article}
\usepackage{graphicx}% http://ctan.org/pkg/graphicx
\usepackage[nomessages]{fp}% http://ctan.org/pkg/fp
\newcommand{\sxfigure}[4]{%
  \FPeval\widthdim{#1*#3}% Calculate width dimension
  \FPeval\heightdim{#2*#3}% Calculate height dimension
  \includegraphics[width=\widthdim cm, height=\heightdim cm]{#4}%
}
\begin{document}
\sxfigure{1}{2}{1.5}{example-image-a} \quad
\sxfigure{1}{2}{1.7}{example-image-a}
\end{document}

However, I would opt for an improved syntax using a key-value approach for exactly the reason mentioned in the first paragraph of the xkeyval documentation:

Using keys in macro definition has the advantage that the 9 arguments maximum can easily be avoided and that it reduces confusion in the syntax of your macro when compared to using a lot of (optional) arguments. Compare for instance the following possible syntaxes of the macro \mybox which might for instance use its arguments to draw some box containing text

\mybox[5pt][20pt]{some text}[red][white][blue]
\mybox[text=red,background=white,frame=blue,left=5pt,right=20pt]{some text}

Notice that, to be able to specify the frame color in the first example, the other colors need to be specified as well. This is not necessary in the second example and these colors can get preset values. The same thing holds for the margins.