Ok, the title might be a bit cryptic and my question is a bit lengthy, so please bear with me.

What I want to do is: I have two images with different aspect ratios that I want to place next to each other using subfloat. Together both images should fill the \textwidth and on the other hand have the same height. Also a gap between both images should be allowed. Analytically there is only one physical height (say \finalheight) for both images, such that all conditions are fullfilled:

\finalheight = (\textwidth - gap_width) / (width_im_1/height_im_1 + width_im_2/height_im_2)

where the annotations _im_1 and _im_2 refer to the first and the second image, respectively. Once calculated the \finalheight can then used in the figure command as follows:


I have already a code that works within the body of the document i.e.:







However, I now tried to package the above code as a macro:


with only these lines modified:

\setlength{\gapspace}{#1}  .

This however results in an

"! Illegal unit of measure (pt inserted)" 

error when trying to evaluate

\def\first{\includegraphics{#2}}  . 

Now finally my question: Is there some reason that \includegraphics may not be used this way in a macro. If not is the approach altogether wrong to use a macro to set a variable as explained above. I've been using LaTeX for quite some time, but I never messed too much with macros or custom variables myself and could not find help for this issue searching the web.


Inculding the suggestions from the answer below the code works, the problem was the wrong syntax in the \newcommand definition: {3} instead of [3]. Unfortunately, the error message sent me on the wrong path, I should have seen that. After inculsion of the improvemts from How to preserve lenghts to use with \includegraphics scaling, when calculating them with pgf also the error message is solved. In case it is useful for somebody I add an minimal working example including the solutions from the answers to this and the other question:

\documentclass[12pt, twoside, paper=A4]{scrbook}
  • 1
    Welcome to TeX.SX! – Sean Allred Jul 3 '13 at 15:26
  • 1
    Analytically, W1 = (k2/(k1+k2)) \textwidth, where k1 and k2 are the image aspect ratios (k=height/width) and W1 is width of image 1. Obviously W2=\textwidth-W1. – Steven B. Segletes Jul 3 '13 at 17:15

Please always show a complete document that generates the problem. You show some code and you show an error message, but not the code that generates the error.

TeX is a macro expansion language you can almost always put any sequence of commands in a macro and they work the same way. Here, where no catcode/verbatim trickery is used it is certainly possible.


That middle line should presumably be defining \second.


would normally be a synax error but as you say it works, presumably you have loaded calc package? (Again it is much easier if you show a complete document with all relevant packages)

The syntax for \newcommand that you show is incorrect


Is not a syntax error but 3 is the entire body of the definition, you want


You probably want to add % at the end of every line as well.

  • Thanks for your quick answer! I'll keep your advice in mind,next time I'll provide a more complete example. Unfortunaltely, your proposed modifications do not change the outcome. I'll prepare a minimal, updated example and add it to the original question. – Ascurion Jul 3 '13 at 16:26
  • Actually I have been mistaken, producing the minimal example I found that the code runs fine, altough the error message persists. – Ascurion Jul 3 '13 at 17:10

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.