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
:Macro parameter substitution is just that: text substitution. So when
#1=2cm
this becomesnot 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:With these parameters the value becomes
39.26477pt
.Now we substitute this is the outer expression. This will become then:
Now substitute the parameters and you get:
Now again we have an inner expression:
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: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:
which gives
22.47186pt
. Whether that is the expected answer, I don't know.