I wanted to create a macro that can be invoked in the middle of a tikz path and behave differently, conditional on one of the arguments.
Here is an example document:
\documentclass[a4paper]{amsart}
\usepackage{ifthen, tikz}
\newcommand{\nameandlabel}[2]{%
node [midway, \ifthenelse{\equal{#1}{a}}{above}{below}] {#2}%
}
\begin{document}
\begin{tikzpicture}[xscale = 25mm]
\node (a) at (0, 0) {$A$};
\node (b) at (1, 0) {$B$};
\node (c) at (2, 0) {$C$};
\draw (a) -- (b) node [midway, above] {$f$};
\draw (b) -- (c) \nameandlabel{a}{$g$};
\end{tikzpicture}
\end{document}
This document does not compile. It results in this error message, which is surprising because \equal is a macro provided by ifthen.
! Undefined control sequence.
<argument> \equal
{a}{a}
l.16 \draw (b) -- (c) \nameandlabel{a}{$g$}
;
I saw discussion on the TeX-LaTeX Stack Exchange that said that ifthen was obsolete and that the \IfStrEq macro from the xstring package might be used instead. So I changed the example to the following:
\documentclass[a4paper]{amsart}
\usepackage{xstring, tikz}
\newcommand{\nameandlabel}[2]{%
node [midway, \IfStrEq{#1}{a}{above}{below}] {#2}%
}
\begin{document}
\begin{tikzpicture}[xscale = 25mm]
\node (a) at (0, 0) {$A$};
\node (b) at (1, 0) {$B$};
\node (c) at (2, 0) {$C$};
\draw (a) -- (b) node [midway, above] {$f$};
\draw (b) -- (c) \nameandlabel{a}{$g$};
\end{tikzpicture}
\end{document}
But this results in an even more incomprehensible error message:
! Argument of \XC@definec@lor has an extra }.
<inserted text>
\par
l.16 \draw (b) -- (c) \nameandlabel{a}{$g$}
;
Why does this still not work?


\nameandlabeldoes not get expanded before TikZ tries to make sense of the path. You may sidestep the problem by proper use of key-val: with\tikzset{a/.style={above}, mystyle/.style={midway,below}}you can do...(c) node[mystyle,a] {$g$}ornode[mystyle]for defaulting to below... – Bordaigorl Nov 14 '13 at 18:30aas an alias foraboveand then change the def of your macro tonode [midway, #1] {#2}– Bordaigorl Nov 14 '13 at 20:38\ifxunacceptable? If you do not need anything fancier than checking it is anathan you could get away with\ifx#1a{above}\else{below}\fi(which does work in your code) – Bordaigorl Nov 14 '13 at 20:41\IfStrEqis not fully expandable (meaning it will leave traces besidesleftandrightin the options which clutter up the key parser from PGFkeys). Similar to the problem in (x)ifthen in TikZ (and related question). You will need to do conditional outside of the keys (either outside of the path, inside of\pgfextra{…}or with the/utils/execkey). – Qrrbrbirlbel Nov 14 '13 at 23:49xscaleincorrectly. This key should only get a value, not a length. What you are essentially doing isxscale=71.13188. Why don’t you simply define a style with the namea(or similar) that is defined asa/.style={above}, another key likebforbelowcan be defined as well. By the way, do you know theautoand theswapoptions/keys? – Qrrbrbirlbel Nov 14 '13 at 23:53x, notxscaleI guess. I decided the easiest thing to do is just to define two different macros. Actually, in the real task I wanted 4 different possibilities (above, below, left and right) so I just created 4 different macros. – Hammerite Nov 15 '13 at 17:59