[Tex/LaTex] `\dimexpr` with multiplication by real number and with parenthesis

dimension-expressionsdimensions

I wish to draw an object with tikz by a macro, therefore I have to calculate expressions like:

defined by:

\newcommand{\myhexagon}{2}{...}

\newcommand{\myobject}{2}{
 \myheaxgon{#1}{0.5*#2}
 \myhexagon{0.89*#1-4mm}{0.79*(#1-(0.89*#1-4mm))+#2}}
}

called by (e.g):

\myobject{2cm}{3mm}
\myobject{12mm}{3mm}

I've tried to solve with

\newcommand{\myobject}{2}{
 \def\const{0.89}
 \def\constII{0.79}

 \myheaxgon{#1}{0.5*#2}
 \myheaxgon{\the\dimexpr \const#1 -4mm\relax}{\the\dimexpr\constII\the\dimexpr#1-\the\dimexpr\const#1-4mm\relax\relax+#2\relax}
}

(viz. replace '0.89*' by '\const' and '()' by '\the\dimexpr ... \relax') but I failed. Does anyone have some idea?

Best Answer

Let's analyze what happens with \myobject{2cm}{3mm}. First the inner \dimexpr:

\the\dimexpr\const#1-4mm\relax

Macro parameter substitution is just that: text substitution. So when #1=2cm this becomes

\the\dimexpr\0.892cm-4mm\relax

not a multiplcation. If you want to multiply \const with #1, use \const\dimexpr#1\relax, like you have done with \constII. So the inner one becomes:

\the\dimexpr\const\dimexpr#1\relax-4mm\relax

With these parameters the value becomes 39.26477pt.

Now we substitute this is the outer expression. This will become then:

\the\dimexpr\constII\the\dimexpr#1-39.26477pt\relax+#2\relax

Now substitute the parameters and you get:

\the\dimexpr0.79\the\dimexpr2cm-39.26477pt\relax+3mm\relax

Now again we have an inner expression:

\the\dimexpr2cm-39.26477pt\relax

This gives 42.9589pt. But note: because you use \the, you get a text, not a length value. So with the textual substitution in the outer expression, you get:

\the\dimexpr0.7942.9589pt+3mm\relax

And instead of a multiplication, you get an illegal number with two decimal points. Lesson: don't use \the inside expressions.

With these corrections the final expression becomes:

\the\dimexpr\constII\dimexpr#1-\dimexpr\const\dimexpr#1\relax-4mm\relax\relax+#2\relax

which gives 22.47186pt. Whether that is the expected answer, I don't know.