You can use / instead of -. You also need to wrap the coordinates in {}
(see the comments on this answer by Martin if you wish to know why), then your example would become:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\foreach \pointA/\pointB in {{(1,0)}/{(2,2)},{(3,4)}/{(2,1)}}{
\draw \pointA -- \pointB;
}
\end{tikzpicture}
\end{document}
This can also be extended to more than 2 elements, just add another / \pointC
. All of this is treated on page 506 of the Tikz manual.
EDIT
Ok, after your edit (better to specify that, otherwise Peter and I look silly ;)
), my answer isn't all that great anymore. Basically you can't do what you want due to the same reasons as in the comment I referenced above. You can work around it though. Note: this is just something hacked together quickly, it probably doesn't handle everything great, but it handles your MWE and can probably be extended.
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\newcounter{n}
\makeatletter
\def\wrap(#1)#2{%
\stepcounter{n}%
\coordinate (\number\value{n}) at (#1);%
\if#2(%
\wraprest#2%
\else\fi%
}
\def\wraprest(#1)#2{%
\stepcounter{n}%
\coordinate (\number\value{n}) at (#1);%
\if#2(%
\wrap#2%
\else\fi%
}
\begin{document}
\begin{tikzpicture}
\wrap(1,0)(2,0)(3,0)(4,0)(5,0)(6,0)(7,0)(8,0)x%
\foreach \pointA in {1,3,...,\number\value{n}}{
\pgfmathsetmacro{\pointB}{\pointA+1}
\draw (\pointA) -- (\pointB);
}
\end{tikzpicture}
\makeatother
\end{document}
This uses Peter's method of defining coordinates and using those in the list. First a coordinate is created for each (x,y)
and these are numbered, such that lines go from n
to n+1
where n
is odd. If you want delimiters between pairs those can be added with a slight modification. (The x
at the end of the wrap
can be anything, just not (
).
The styles listed in the post you linked are already defined in TikZ, you do not need to create a new style to use them.
You can create a style if you need a pattern that doesn't exist.
For example, mygridstyle
in my MWE creates a style with a sequence of dashes with a length of 8pt, 6pt, 4pt, and 2pt separated by a space of 4pt.
\documentclass{article}
\usepackage{tikz}
\tikzset{mygridstyle/.style={dash pattern=on 8pt off 4pt on 6pt off 4pt on 4pt off 4pt on 2pt off 4pt}}
\begin{document}
Standard dashdotted:
\begin{tikzpicture}
\draw[gray, dashdotted, xstep=1.3cm, ystep=1.6cm] (0,0) grid (4,4);
\end{tikzpicture}
Standard dotted, thick line:
\begin{tikzpicture}
\draw[gray, dotted, xstep=1.3cm, ystep=1.6cm, thick] (0,0) grid (4,4);
\end{tikzpicture}
Standard dash, with 4pt line width:
\begin{tikzpicture}
\draw[gray, dotted, xstep=1.3cm, ystep=1.6cm, line width=4pt] (0,0) grid (4,4);
\end{tikzpicture}
Customized sytle, very thick line:
\begin{tikzpicture}
\draw[gray, mygridstyle, xstep=1.3cm, ystep=1.6cm, very thick] (0,0) grid (4,4);
\end{tikzpicture}
\end{document}
![enter image description here](https://i.stack.imgur.com/8BYke.png)
Best Answer
You can find the definitions of these and many others in the
tikz.code.tex
file in your distribution (e.g. MikTeX has it underC:\Program Files (x86)\MiKTeX 2.9\tex\generic\pgf\frontendlayer\tikz\
)For quick comparison, see the output below: