7

This question seems quite simple, so I feel a bit sheepish about asking it, but I've spent quite a while scouring both this site and the manual without any success, so here goes...

I found that

\draw (0,0) -- (1,0);
\draw (1,0) -- (1,1);

does not produce the same result as

\draw (0,0) -- (1,0);
\draw  -- (1,1);

and this surprises me. I would have thought that, in the second case, the path would begin where the previous path ended. That is my expectation based on Section 14.2 of the manual, concerning "The Line-To Operation" which says:

The line-to operation extends the current path from the current point in a straight line to the given coordinate. The "current point" is the endpoint of the previous drawing operation or the point specified by a prior move-to operation.

You use two minus signs followed by a coordinate in round brackets.

However, I'm clearly missing something. But what? Perhaps a clue is provided by the answer from Loop Space to Saving and reusing/combining paths in TikZ? that states:

TikZ does not blank the current soft path when it starts, so when a command such as \draw starts up then it simply appends its stuff to whatever was there already. This is usually the empty path because whenever a path is used (ie rendered) then the current soft path is set to "empty". But it needn't be. [emphasis mine]

I suspect that is related to what I'm observing here, but considering how simple my question is, the suggested solution there feels far more technical that I'd expect to be necessary. In some sense, my question is just:

How can I tell TikZ to begin a path from the end of a path that has already been drawn?

To clarify, the fact that two separate "draw" commands are mentioned in my question above is essential. That is, I realize that

\draw (0,0) -- (1,0) -- (1,1);

would produce the desired result, but the question, again deals with continuing a path that has already been drawn. I have rephrased the question above to try to make this more clear.

  • 1
    Compare these two commands: \draw (1,0) -- (0,1) -- (1,1) -- (2,1); \draw (1,0) -- (0,1) (1,1) -- (2,1); – Sigur Jul 24 '15 at 16:15
  • 1
    @Sigur: I see that those two commands produce different outputs, and it makes sense to me why they do. What I don't see is how this helps answer my question. Sorry. – Louis Deaett Jul 24 '15 at 16:25
10

As percusse points out, after a path is used, it is forgotten and is no longer the "current path". However, the intersections library provides a key to name a path and save it so can be "remembered". In the following a from end of path key is defined which can be used to start a new path from the last point of the end of a remembered (i.e., named) path.

\documentclass[tikz,border=5]{standalone}
\usetikzlibrary{intersections}
\tikzset{%
  from end of path/.style={
    insert path={
      \pgfextra{%
        \expandafter\pgfprocesspathextractpoints%
          \csname tikz@intersect@path@name@#1\endcsname%
        \pgfpointlastonpath%
        \pgfgetlastxy\lastx\lasty
      }
      (\lastx,\lasty)
}}}

\begin{document}
\begin{tikzpicture}
\draw [name path=A] (0,0) -- (1,1);
\draw [red, from end of path=A, name path=B] arc (180:0:1);
\draw [green, from end of path=B, name path=C] -- ++(0,-1);
\draw [blue, from end of path=C] .. controls ++(0,-1) and ++(0,-2) .. (0,0);
\end{tikzpicture}
\end{document}

enter image description here

  • Wow, that is pretty cool. Despite the dipping into PGF, I get the sense from the answers here that this is just about the best possible solution. – Louis Deaett Jul 27 '15 at 16:06
4

Loop Space' quote is not meant for the current implementation. His context is what if we didn't flush out the path as is the current implementation of TikZ. He is, if I'm not wrong building a story that led to spath package. Not the actual description of TikZ implementation.

Every draw command resets the last known coordinate to the origin. In fact your second command of your second example shouldn't draw anything because the pen is not put down which is required by the PostScript/PDF specs by an explicit move-to operation.

At every succesful parsing of the path until a ; is encountered is stored and then converted into soft path syntax and then rendered. After this current path, last point known to TikZ and PGF is reset.

Hence, your quoted part from the manual is valid within the same path command. Or if you are building with the lower PGF level syntax until a \pgfusepath is encountered.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.