Robert Homann | 13 Jun 15:08
Picon
Picon

Macros with optional parameters in paths

Hi!

Currently, I am trying to write some macros for drawing parse trees, based on
TikZ. Each of these macros corresponds to an operator, and they are designed to
embed each other in order to generate nodes on a path. So, inside a tikzpicture
environment, I start drawing with a \path command, followed by some of my
macros that emit "node" and "child" commands, and stop with a semicolon.

This works all well, but now I would like my macros to accept optional
parameters. Unfortunately, it seems like \path is choking on the \@ifnextchar
command that is used to handle the []-brackets.

Could someone please take a look at the artificial example below that
illustrates my problem?

---
\listfiles
\documentclass[11pt,a4paper]{scrartcl}
\usepackage{tikz}

\makeatletter

\def\Works{\@ifnextchar[{\@Works}{\@Works[]}}
\def\@Works[#1]#2#3#4{\path[draw] (0,#2) node[draw] {#3} -- (2,#2) node[draw#1] {#4};}

\def\Fails{\@ifnextchar[{\@Fails}{\@Fails[]}}
\def\@Fails[#1]#2#3#4{(0,#2) node[draw] {#3} -- (2,#2) node[draw#1] {#4}}

\makeatother

\begin{document}
\begin{tikzpicture}
\Works{0}{A}{B}
\Works[,circle]{-1}{C}{D}
\end{tikzpicture}

\vspace{1cm}

\begin{tikzpicture}
\path[draw] \Fails{0}{A}{B} ;
\path[draw] \Fails[,circle]{-1}{C}{D} ;
\end{tikzpicture}
\end{document}
---

Macros \Works and \Fails do essentially the same thing, but the \path command
and semicolon are not included in the latter (resembling the situation in my
"real" macros). As long as the \path command is inside a macro definition,
there is no problem with optional parameters. Macro \Fails does not work,
however, and latex and pdflatex both throw the following error at me.

---
! Package tikz Error: Giving up on this path.

See the tikz package documentation for explanation.
Type  H <return>  for immediate help.
 ...

l.24 \path[draw] \Fails
                       {0}{A}{B} ;
---

I also tried \newcommand instead of \def to define the macros, but to no avail.
After playing around a bit (no guru powers involved...), I figured that
\@ifnextchar is the culprit. I can't imagine any way how to implement my macros
without \@ifnextchar, however, so I am stuck for now.

Does anybody know a solution to this problem?

 *File List*
scrartcl.cls    2002/09/18 v2.9j LaTeX2e KOMA document class
scrlfile.sty    2002/09/18 v2.9j LaTeX2e KOMA package
  size11.clo    2001/04/21 v1.4e Standard LaTeX file (size option)
typearea.sty    2002/09/18 v2.9j LaTeX2e KOMA package
    tikz.sty    2006/10/17 v1.10 (rcs-revision 1.68)
     pgf.sty    2006/10/11 v1.10 (rcs-revision 1.7)
  pgfrcs.sty    2006/10/26 v1.10 (rcs-revision 1.14)
  pgfrcs.code.tex
 pgfcore.sty    2006/10/11 v1.10 (rcs-revision 1.4)
graphicx.sty    1999/02/16 v1.0f Enhanced LaTeX Graphics (DPC,SPQR)
  keyval.sty    1999/03/16 v1.13 key=value parser (DPC)
graphics.sty    2001/07/07 v1.0n Standard LaTeX Graphics (DPC,SPQR)
    trig.sty    1999/03/16 v1.09 sin cos tan (DPC)
graphics.cfg    2001/08/31 v1.1 graphics configuration of teTeX/TeXLive
  pdftex.def    2001/11/29 v0.03i graphics/color for pdftex
  pgfsys.sty    2006/10/16 v1.10 (rcs-revision 1.19)
  pgfsys.code.tex
pgfsyssoftpath.code.tex    2006/10/16  (rcs-revision 1.4)
pgfsysprotocol.code.tex    2006/10/16  (rcs-revision 1.4)
  xcolor.sty    2004/07/04 v2.00 LaTeX color extensions (UK)
   color.cfg    2001/08/31 v1.1 color configuration of teTeX/TeXLive
 pgfcore.code.tex
pgfbaseshapes.sty    2006/10/16 v1.10 (rcs-revision 1.16)
pgfbaseshapes.code.tex
pgfbaseplot.sty    2006/10/16 v1.10 (rcs-revision 1.5)
pgfbaseplot.code.tex
pgfbaseimage.sty    2006/10/16 v1.10 (rcs-revision 1.5)
pgfbaseimage.code.tex
pgfbaselayers.sty    2006/10/16 v1.10 (rcs-revision 1.5)
pgfbaselayers.code.tex
pgfbasesnakes.sty    2006/10/16 v1.10 (rcs-revision 1.10)
pgfbasesnakes.code.tex
pgfbasepatterns.sty    2006/10/16 v1.10 (rcs-revision 1.9)
pgfbasepatterns.code.tex
pgfcomp-version-0-65.sty    2006/10/11 v1.10 (rcs-revision 1.4)
    calc.sty    1998/07/07 v4.1b Infix arithmetic (KKT,FJ)
  pgffor.sty    2006/10/16 v1.10 (rcs-revision 1.5)
  pgffor.code.tex
 xkeyval.sty    2006/11/18 v2.5f package option processing (HA)
 xkeyval.tex    2006/11/18 v2.5f key=value parser (HA)
    tikz.code.tex
supp-pdf.tex

bye, Robert

--

-- 
Windows is not the answer.
Windows is the question.
The answer is "No".

-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/

Gmane