[Tex/LaTex] Passing arguments to \def inside \newcommand (TikZ/PGF)

macrosprogrammingtikz-pgf

I'm having trouble passing an argument to \pgfmathparse inside a macro I defined. Here is the code:

\documentclass{article}

\usepackage{tikz}

\usetikzlibrary{calc}

\newcommand{\drawmestg}[2]{
    \def\endpt{\pgfmathparse{#1+#2}\pgfmathresult}
    \draw (0,0) -- (\endpt,0);
}

\begin{document}
    \begin{tikzpicture}
        \drawmestg{1}{1}
    \end{tikzpicture}
\end{document}

I must confess, I'm quite clueless why I get the error:

Incomplete \iffalse; all text was ignored after line 14

Best Answer

In the \draw command in your version \endpt is expanded to \pgfmathparse{#1+#2}\pgfmathresult. So you get

\draw (0,0) -- (\pgfmathparse{#1+#2}\pgfmathresult,0);

which greatly confuses TikZ.

Instead you should set \endpt to the result of the calculation, either via

\pgfmathsetmacro{\endpt}{#1+#2}

as Peter suggested or equivalently via

\pgfmathparse{#1+#2}
\edef\endpt{\pgfmathresult}

Alternatively for this MWE you can of course simply do

\newcommand{\drawmestg}[2]{
    \draw (0,0) -- ({#1+#2},0);
}