Expanding a sequence inside a TikZ path
Here is a solution using \pgfextra
, insert path
and two custom keys:
\documentclass[tikz]{standalone}
\tikzset{
recurse lattice path/.code args={#1#2}{
\ifx#1.
\else
\ifx#1u
\tikzset{insert path={-- ++(0,1) node[fill=black]{}}}
\else
\tikzset{insert path={-- ++(1,0) node[fill=black]{}}}
\fi
\tikzset{recurse lattice path=#2}
\fi
},
lattice path/.code={%
\draw (0,0) node[fill=black]{}
\pgfextra{\tikzset{recurse lattice path={#1.}}};
},
}
\begin{document}
\begin{tikzpicture}
\tikzset{lattice path=rrurrur}
\end{tikzpicture}
\end{document}
Edit:
Here is a simpler solution without \pgfextra
:
\documentclass[tikz]{standalone}
\tikzset{
dep u/.style={insert path={-- ++(0,1) node{}}},
dep r/.style={insert path={-- ++(1,0) node{}}},
dep d/.style={insert path={-- ++(0,-1) node{}}},
dep l/.style={insert path={-- ++(-1,0) node{}}},
recurse lattice path/.code args={#1#2}{
\ifx#1.\else\tikzset{dep #1,recurse lattice path=#2}\fi
},
lattice path/.style={recurse lattice path=#1.}
}
\begin{document}
\begin{tikzpicture}
\tikzset{every node/.style={circle,fill,draw=none,inner sep=2pt}}
\draw (0,0) node{} [lattice path=rrurrurrdddlll];
\end{tikzpicture}
\end{document}
Basically, you can't put a macro defined using \newcommand
inside a TikZ path as it does a lot of stuff "under the covers" (e.g., \futurelet
) which TikZ cannot handle.
You can, however, put arbitrary code inside \pgfextra{}
in a TikZ path. In this case you could build the path inside \pgfextra
and then insert it afterwards, like this:
\documentclass[tikz,border=0.125cm]{standalone}
\def\addtolatticepath#1{%
\expandafter\def\expandafter\latticepath\expandafter{\latticepath#1}%
}
\def\latticepathletteru{\addtolatticepath{ -- ++(0,1) }}
\def\latticepathletterd{\addtolatticepath{ -- ++(0,-1) }}
\def\latticepathletterl{\addtolatticepath{ -- ++(-1,0) }}
\def\latticepathletterr{\addtolatticepath{ -- ++(1,0) }}
\def\parselatticepath#1{%
\def\latticepath{node {}}%
\Parselatticepath#1@}
\def\Parselatticepath#1{%
\ifx#1@%
\let\next=\relax%
\else%
\csname latticepathletter#1\endcsname%
\addtolatticepath{ node {} }%
\let\next=\Parselatticepath
\fi%
\next}
\tikzset{%
insert lattice path/.style={%
every node/.style={
circle,
fill,
draw=none,
inner sep=2pt
},
insert path={%
\pgfextra{\parselatticepath{#1}}%
\latticepath
}
}
}
\begin{document}
\begin{tikzpicture}
\draw (0,0) [insert lattice path={rrurruururuu}];
\end{tikzpicture}
\end{document}
This is an old question, but as I do not see a lot of answers here using parser
PGF library, I add one.
\documentclass[tikz,border=7pt]{standalone}
\usepgfmodule{parser}
% macro that add #1 to the current path when used inside \pgfextra
\def\insertpath#1{\tikzset{insert path={#1}}}
% define the parser "lattice path"
\pgfparserdef{lattice path}{initial}{the letter u}{\insertpath{node{} -- ++(0,1) }}
\pgfparserdef{lattice path}{initial}{the letter r}{\insertpath{node{} -- ++(1,0) }}
\pgfparserdef{lattice path}{initial}{the letter d}{\insertpath{node{} -- ++(0,-1)}}
\pgfparserdef{lattice path}{initial}{the letter l}{\insertpath{node{} -- ++(-1,0)}}
\pgfparserdef{lattice path}{initial}{the character .}{\pgfparserswitch{final}}
\begin{document}
\begin{tikzpicture}[every node/.style={circle,fill=red,inner sep=2pt}]
\draw (0,0) \pgfextra{\pgfparserparse{lattice path}rrurrurrdddlll.} node{};
\end{tikzpicture}
\end{document}