7

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?

Thanks for your help.

4
  • 1
    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) – Fabian Schuh 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? – Andrew Stacey Jun 14 '11 at 11:07
6

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}
2
  • This is a very good generalized solution. – Yiannis Lazarides Jun 14 '11 at 13:57
  • Thats exactly what I was searching for. Thanks – Fabian Schuh Jun 15 '11 at 9:53
10

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]
\draw [fill=red] (0,0) circle [radius=3pt];
\drawSYS{lr}{X}
\drawSYS{rl}{Y}
\end{tikzpicture}

\end{document}

tikz .is choice

1
  • Oh. This solution is nice. Didn't think of that. thanks – Fabian Schuh Jun 15 '11 at 9:52
1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.