The source of the difficulty is that ellipses are constructed in a particular way in TikZ. They are paths that start from the x-axis and proceed counter-clockwise around their centre. The vast majority of the time, the exact parametrisation doesn't matter. You appear to have found the one situation where it does!
In the actual question, you only want to be able to mirror the ellipse, and so draw it starting from the negative x-axis (the title of the question suggests a more flexible approach). That's actually not too hard since we can exploit the symmetry of the ellipse. The key is to provide it with a negative x-radius, since then it will start from the negative x-axis (and proceed clockwise, but we could correct for that by negating the y-radius as well). To do this, we interrupt the call from the node shape to the drawing command and flip the sign of the x-radius. The simplest way to do this is to redefine the \pgfpathellipse
macro to do the negation and then call the original macro. The following code does this.
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations,shapes,decorations.markings}
\makeatletter
\let\origpgfpathellipse=\pgfpathellipse
\def\revpgfpathellipse#1#2#3{%
#2%
\pgf@xa=-\pgf@x
\origpgfpathellipse{#1}{\pgfqpoint{\pgf@xa}{0pt}}{#3}}
\makeatother
\tikzset{
reversed ellipse/.style={
ellipse,
reverse the ellipse%
},
reverse the ellipse/.code={
\let\pgfpathellipse=\revpgfpathellipse
}
}
\begin{document}
\begin{tikzpicture}
\node[ellipse,
draw,
postaction={
decorate,
decoration={
markings,
mark=at position 1 with {
\arrow[line width=5pt,blue]{>}
}
}
}
] at (0,0) {hello world};
\node[reversed ellipse,
draw,
postaction={
decorate,
decoration={
markings,
mark=at position 1 with {
\arrow[line width=5pt,blue]{>}
}
}
}
] at (0,-2) {hello world};
\end{tikzpicture}
\end{document}
Here's the result:
(the arrow got clipped, but you can see where it lies)
In my opinion, this is a bug in TikZ. As is well-known on this site (!) the PGF \foreach
command executes its innards inside a group (actually, two groups). This means that when something is meant to be remembered from one iteration to the next, there has to be an explicit mechanism for remembering it. When the \foreach
construction is used to build a path then there are some obvious things that need to be remembered, such as the last point constructed and the last "saved" point. So when TikZ encounters a \foreach
in the path construction then it actually calls a wrapper around the PGF \foreach
which includes this "saving" apparatus.
However, it would appear that the implementation forgot to remember something. When TikZ encounters a bare node coordinate, such as (p1)
, then it can't immediately construct the path at this point. The path will lead up to a point on the node's border, but will then hop to some other point on the node's border and that hop will be determined by the next bit of the path. So TikZ remembers that there is a moveto
which is "waiting" and figures it out when it knows what the next bit of the path will be.
Unfortunately, this waiting moveto is not part of what is remembered when it goes around the \foreach
iteration. So it reverts to the previously remembered moveto which was the one set up before all the grouping started, namely (p1)
.
A solution is to make TikZ remember that moveto between groups. Here's an adaptation of \tikz@foreach
which adds in this rememberence. I have a suspicion that there are a few other things that ought to be remembered between loops as well, such as \tikzlastnode
, but to avoid overcomplicating the code I haven't put them in.
\documentclass{article}
%\url{http://tex.stackexchange.com/q/75146/86}
\usepackage{tikz}
\makeatletter
\def\tikz@foreach{%
\def\pgffor@beginhook{%
\tikz@lastx=\tikz@foreach@save@lastx%
\tikz@lasty=\tikz@foreach@save@lasty%
\tikz@lastxsaved=\tikz@foreach@save@lastxsaved%
\tikz@lastysaved=\tikz@foreach@save@lastysaved%
\let\tikz@moveto@waiting=\tikz@foreach@moveto@waiting
\setbox\tikz@figbox=\box\tikz@tempbox\expandafter\tikz@scan@next@command\pgfutil@firstofone}%
\def\pgffor@endhook{\pgfextra{%
\xdef\tikz@foreach@save@lastx{\the\tikz@lastx}%
\xdef\tikz@foreach@save@lasty{\the\tikz@lasty}%
\xdef\tikz@foreach@save@lastxsaved{\the\tikz@lastxsaved}%
\xdef\tikz@foreach@save@lastysaved{\the\tikz@lastysaved}%
\global\let\tikz@foreach@moveto@waiting=\tikz@moveto@waiting
\global\setbox\tikz@tempbox=\box\tikz@figbox\pgfutil@gobble}}%
\def\pgffor@afterhook{%
\tikz@lastx=\tikz@foreach@save@lastx%
\tikz@lasty=\tikz@foreach@save@lasty%
\tikz@lastxsaved=\tikz@foreach@save@lastxsaved%
\tikz@lastysaved=\tikz@foreach@save@lastysaved%
\let\tikz@moveto@waiting=\tikz@foreach@moveto@waiting
\setbox\tikz@figbox=\box\tikz@tempbox\tikz@scan@next@command}%
\global\setbox\tikz@tempbox=\box\tikz@figbox%
\xdef\tikz@foreach@save@lastx{\the\tikz@lastx}%
\xdef\tikz@foreach@save@lasty{\the\tikz@lasty}%
\xdef\tikz@foreach@save@lastxsaved{\the\tikz@lastxsaved}%
\xdef\tikz@foreach@save@lastysaved{\the\tikz@lastysaved}%
\global\let\tikz@foreach@moveto@waiting=\tikz@moveto@waiting
\foreach}
\makeatother
\begin{document}
\begin{tikzpicture}[scale=.5]
\foreach [count=\x] \pt in {(0,5), (0,2), (2,2), (2,0), (6,0), (6,2), (8, 2), (8,6), (6,6), (6,8), (3,8)}
\node[black, circle, draw] (p\x) at \pt {};
\draw (p1)
\foreach \x in {2,...,11} {
-- (p\x)
};
\end{tikzpicture}
\end{document}
Result:
Best Answer
Do you mean something like this?