7

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.

David
  • 259

1 Answers1

5

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.

David Carlisle
  • 757,742
  • 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
  • Thanks a lot! Also thanks to @cfr who apparently deleted his answer. I appreciated in particular also the explication of \dimexpr and \numexpr. I realized that \dimexpr needs lengths, but that numexpr takes only integers is very interesting to know. – David Jan 17 '16 at 08:30
  • @DavidCarlisle just another short question. If I use your definition with the \makebox command, then I become an Underfull \hbox warning unless I delete all the blank lines between \twofig and the rest. Is there a way to avoid that? (likely not) – David Jan 17 '16 at 09:17
  • 1
    @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
  • 1
    @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
  • @DavidCarlisle Ok I thought the problem was that \makebox inserted a linebreak, but trying in another document confirmed that I was supposing wrong. I don't know what the problem in my document is. If I start a new paragraph it leaves a blank line between images and new paragraph. It is however not that bad, as long I know how to work around it, especially because I'm writing a two column summary (for personal use) and I also set the parindent to 0. – David Jan 17 '16 at 11:11