I am trying to draw a simple path using TikZ, with a custom syntax to specify it. This works:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\newcommand{\latticepath}[1]{
\draw[thick] (0,0) node{} \foreach \x in {#1}{ -- \if u\x ++(0,1) \else ++(1,0) \fi node{}};
}
\begin{tikzpicture}[x=3mm,y=3mm]
\tikzset{every node/.style={circle,fill,draw=none,inner sep=2pt}}
\latticepath{r,r,u,r,r,u,u,r,u,r,u,u}
\end{tikzpicture}
\end{document}
However, I would prefer not to comma-separate the values. So I wrote this command to convert a sequence of letters into path components:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\makeatletter\let\mygobble\@gobble\makeatother
\newcommand{\readpath}[3]{
\def\pathimpl##1{\ifx##1.\let\rest\mygobble\else\ifx##1u#1\else#2\fi\def\rest####1{####1}\fi\rest\pathimpl}
\pathimpl#3.
}
\readpath{Up }{Right }{rrurruururuu}
However this implementation produces a bunch of errors due to the use of macros in paths:
\newcommand{\betterlatticepath}[1]{
\draw[thick] (0,0) node{} \readpath{++(0,1) node{}}{++(1,0) node{}}{#1};
}
\begin{tikzpicture}
\tikzset{every node/.style={circle,fill,draw=none,inner sep=2pt}}
\betterlatticepath{rrurruururuu}
\end{tikzpicture}
\end{document}
Package tikz Error: Giving up on this path. Did you forget a semicolon?.
Undefined control sequence.
You can't use `macro parameter character #' in restricted horizontal mode.
Parameters must be numbered consecutively.
Parameters must be numbered consecutively.
Argument of \rest has an extra }.
Runaway argument?
I tried \pgfextra
, but this doesn't help. How can I make this work?
\documentclass{...}
, the required\usepackage
's,\begin{document}
, and\end{document}
. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem.