# Arithmetic operation between command arguments and latex parameters

I'm defining a new command that places two images one next to the other and I want to add an horizontal space between the two images which corresponds to one third of the space that remains blank. The command definition reads:

\newcommand{\twofig}[4]{%
\begin{center}\includegraphics[width=#2\columnwidth]{#1.png}%
\hspace{(1-#2-#4)/3 \columnwidth }%
\includegraphics[width=#4\columnwidth]{#3.png}%
\end{center} }


I'm missing what I should use in order to get this operation solved. I've tried with \dimexpr but I always got errors. Is it not the right solution? When should I use \dimexpr and when \numexpr? The etex manual also was not so helpful for me.

• Welcome! If you put an \hfill on each side and in the middle, it will put 1/3 the available space in each place. – cfr Jan 16 '16 at 22:43
• Stick 2 \hfill in the middle. – cfr Jan 16 '16 at 22:51
• Related (but no duplicate): tex.stackexchange.com/questions/236188/… – cryingshadow Jan 16 '16 at 23:02

as cfr says, no calculation is necessary, but if #2\columnwidth works then #2 must be a factor but \numexpr needs an integer and \dimexpr needs a length so neither can calculate (1-#2-#4)

\makebox[\textwidth]{%
\hfill
\includegraphics{...}%
\hfill
\includegraphics{...}%
\hfill}


should do what you want.

In general you can do

\makebox[\textwidth]{%
\hspace{\stretch{1}%
\includegraphics{...}%
\hspace{\stretch{2}%
\includegraphics{...}%
\hspace{\stretch{3}%
}


which will stretch the glue in the ratio 1:2:3, the example above is of course equivalent to having each argument of \stretch be 1.

• I was 8 seconds faster ;). – cfr Jan 16 '16 at 22:52
• @cfr but I explained why an error for \dimexpr which took more than 8 sec:-) (also \hfill\box{} looks ...odd – David Carlisle Jan 16 '16 at 22:53
• Well, a complete example takes more time, too. You might be right about the box though. – cfr Jan 16 '16 at 22:55
• @david23 tex douesn't have a floating point type just \count registers and \dimen and \skip registers which etex generalised to \numexpr and \glueexpr and \dimenexpr – David Carlisle Jan 17 '16 at 10:27
• @david23 don't see why you should get any underful boxes but blank lines are always significant they can not be used to just lay out the source file for cosmetic reasons, they are the instruction \par to end a paragraph. – David Carlisle Jan 17 '16 at 10:28