Consider the following code:
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{etoolbox}
\begin{document}
\newcommand{\ARROW}[2]{
\ifstrequal{#2}{left}{
\newcommand{\coef}{-1}
}{}
\ifstrequal{#2}{right}{
\newcommand{\coef}{1}
}{};
\coordinate(a) at (#1);
\coordinate(b) at ($(a) + (\coef*2cm, 0)$);
\draw[->](a) -- (b);
}
\begin{tikzpicture}
\ARROW{1, 0}{right};
\ARROW{2, 1}{left};
\end{tikzpicture}
\end{document}
When invoking the second time the \newcommand
\ARROW
(\ARROW{2, 1}{left};
) the code complains that \coef
is already designed.
How to optimise the code to circumvent this problem?
\newcommand{\ARROW}[2]{{...}}
. Then\coef
will be local inside\ARROW
.