# Define Macros depending on argument

I'd like to draw some linear Systems with tikz. The problem is the orientation of the Systems. Some have

• input on left - output on right
• input on right - output on left
• input above - output below
• input below - output above

So to handle all the variations with a single \command, i'd like to use the arguments

• ud (up down)
• du (down up)
• lr (left right)
• rl (right left)

So what I need is a switch in Latex to define the Orientationparameter.

pseudocode:

\drawSYS{Text}{lr}

Command: drawSYS
\switch #2
\case 'lr'
\def\ORIENTA left
\def\ORIENTB right
\case 'rl'
\def\ORIENTA right
\def\ORIENTB left
....
\endswitch
\node[anchor=\ORIENTA,\ORIENTB=1cm] () {foobar};


Is this even possible in Tex?

-
I don't really understand what you're asking exactly. Maybe give an example of what you'd like the macro to look like and what it should expand to depending on its arguments. – TH. Jun 14 '11 at 9:16
Naturally it is possible. You only need to look at the documentation of tikz and the use of words like "left" "right" "north" "above" .... Or take the optional arguments [t], [b] of tabular. – Ulrike Fischer Jun 14 '11 at 9:33
I'd like to have a single macro which has a single argument to define weather it is left(anchor right) or right(anchor left) (+ the combinations with up/down) – xeroc Jun 14 '11 at 10:47
I'm not sure that I completely understand, but it seems as though what you'd like is to specify a single option which did, say, anchor=east,rotate=90. Or anchor=east,xscale=-1. That's easy enough to do using styles. Does that sound like what you want? – Loop Space Jun 14 '11 at 11:07

This is a general "cases" type command that is also usefull ouside the tikz community ;-). It returns an \ifcase <pos> in a list

\documentclass{article}
\makeatletter
\newcommand*\@testlist{}
\newcommand*\@invalue{}
\newcommand*\@choiceval{}
\newcounter{itemcount}
\newcommand*\ifchoice[2]{%
\edef\@testlist{\zap@space#1 \@empty}%
\edef\@invalue{\zap@space#2 \@empty}%
\def\@choiceval{99}%
\setcounter{itemcount}{-1}%
\@for\@testitem:=\@testlist\do{%
\stepcounter{itemcount}%
\ifx\@invalue\@testitem\relax
\edef\@choiceval{\the\c@itemcount}%
\fi}%
\ifcase\@choiceval}

\newcommand*\drawSYS[2]{%
\ifchoice{ud, du, lr, rl}{#2}%
%-------------------- 0  = ud
\def\ORIENTA{Up}%
\def\ORIENTB{Down}%
\or%-------------------- 1  = du
\def\ORIENTA{Down}%
\def\ORIENTB{Up}%
\or%-------------------- 2  = lr
\def\ORIENTA{Left}%
\def\ORIENTB{Right}%
\or%-------------------- 3  = rl
\def\ORIENTA{Right}%
\def\ORIENTB{Left}%
\else%------------------ 99 = default
\def\ORIENTA{Left}%
\def\ORIENTB{Right}%
\fi
(#1)(#2)[\ORIENTA][\ORIENTB]}%--- Test output

\makeatother
\begin{document}
\drawSYS{xxx}{ rl }
\end{document}

-
This is a very good generalized solution. – Yiannis Lazarides Jun 14 '11 at 13:57
Thats exactly what I was searching for. Thanks – xeroc Jun 15 '11 at 9:53

The xparse package has a lot of argument dependent macro-defining capabilities. I don't know if it supports string comparison though.

-

You can use the pgfkeys .is choice option. It looks like you don't actually want to execute pieces of code depending on the choice, but merely set styles, which makes this approach the obvious choice. Note that you could also execute code pieces by using \pgfkeys{/tikz/orientation/lr/.code=<code>}:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}

\tikzset{orientation/.is choice,
orientation/lr/.style={anchor=left,right=1cm},
orientation/rl/.style={anchor=right,left=1cm}
}

\newcommand{\drawSYS}[2]{
\node [orientation=#1] {#2};
}

\begin{tikzpicture}[every node/.style=draw]