# TikZ 'midway' that knows about the 'to' opearation

This is sort of the same as/follow up to this: TikZ coordinate that refers to the last "current coordinate"

The answer to that question suggests me to use to instead of -- so that I can use \tikztostart to refer to the "current coordinate", which works fine until I try to use node[midway]. Expanding on the original example:

\documentclass[tikz,margin=1cm]{standalone}
\begin{document}
\begin{tikzpicture}
\coordinate (origin) at (0,0);
\draw (origin) -| (1,1) -- (1,1-|origin) % this uses --
node [midway,above] {hi};
\begin{scope}[xshift=2cm]
\coordinate (origin) at (0,0);
\draw (origin) -| (1,1) to (\tikztostart-|origin) % this uses to
node [midway,above] {hi};
\end{scope}
\end{tikzpicture}
\end{document}


outputs (the left one is correct):

It looks to me that midway doesn't know about to and takes the midway from the previous path segment.

How can I make midway know that it's supposed to attach the node to the to subpath? Or another answer to my previous question that copes with this case?

• Is \draw (origin) -| (1,1) to node [midway,above] {hi} (\tikztostart-|origin); sufficient? – M. Al Jumaily Nov 29 '20 at 3:39
• @M.AlJumaily Yes it is! Please add that as an answer (with a comment on why do I need to put the node before the second coordinate, if possible). – LaTeXer Nov 29 '20 at 3:43
• When you put it before the destination, it is clear just what part of the path it is midway on. If it works after the destination, consider yourself lucky. – John Kormylo Nov 29 '20 at 5:23
• @JohnKormylo My (wrong) reasoning, if I were TikZ, was "oh, look, a node [midway]; cool, let me take the last subpath I made and put a node in the middle". Putting it before the final coordinate makes little sense to me, as the end of the path is not known, if you are doing things as you read them (which is more or less what TikZ does). What it looks like is that a subpath made with to is sort of "forgotten" after the segment is drawn, and the "last subpath" from my reasoning above is taken as the last one with --. – LaTeXer Nov 29 '20 at 5:33
• Tikz uses \pgfnodepostsetupcode to create nodes now and position them later. – John Kormylo Nov 29 '20 at 15:36

The answer given by TikZling is great and furthermore, we can simply swap the order of the \node[]{...}: from

\draw (origin) -| (1,1) to (\tikztostart-|origin) node [midway,above] {hi};

to

\draw (origin) -| (1,1) to node [midway,above] {hi} (\tikztostart-|origin);

We can even take out the midway as TikZ will by default adjust it to the middle of the path, between the two coordinates its sandwiched between:

\draw (origin) -| (1,1) to node [above] {hi} (\tikztostart-|origin);

Another side tip is to use the pos key which accepts an decimal argument between 0.0 and 1.0 for precise placement.

\documentclass[tikz,margin=1cm]{standalone}
\begin{document}
\begin{tikzpicture}
\coordinate (origin) at (0,0);
\draw (origin) -| (1,1) -- (1,1-|origin)
node [midway,above] {hi};
\begin{scope}[xshift=2cm]
\coordinate (origin) at (0,0);
\draw (origin) -| (2,1) to node [above] {hi} (\tikztostart-|origin);
\end{scope}
\begin{scope}[xshift=5cm]
\coordinate (origin) at (0,0);
\draw (origin) -| (2,1) to node [pos=0.25, above] {hi} (\tikztostart-|origin);
\end{scope}
\end{tikzpicture}
\end{document}

• Thanks! I did know about pos, but as I'm labelling some paths, midway is good enough. Do you know/have an explanation about why to node [midway] (x,y) works but to (x,y) node [midway] doesn't? Is there a conceptual difference between them? – LaTeXer Nov 29 '20 at 4:12
• @LaTeXer TikZ will automatically adjust the node in (coordinate1) \node[]{...} (coordinate2) between the two coordinates. – M. Al Jumaily Nov 29 '20 at 4:26

Your observation is correct. There is already the comment by M. Al Jumaily, which shows one possible solution. Another solution, which is arguably more convenient because it places the edge labels nicely also for sloped paths, is to use the edge label key. Please note that the problem that you encounter has nothing to do with \tikztostart but really only with to.

\documentclass[tikz,margin=1cm]{standalone}
\begin{document}
\begin{tikzpicture}
\begin{scope}[local bounding box=1]
\coordinate (origin) at (0,0);
\draw (origin) -| (1,1) -- (1,1-|origin)
node [midway,above] {hi};
\end{scope}
\begin{scope}[xshift=2.5cm,local bounding box=2]
\coordinate (origin) at (0,0);
\draw (origin) -| (1,1) coordinate (tmp) to (tmp-|origin)
node [midway,above] {hi};
\end{scope}
\begin{scope}[xshift=5cm,local bounding box=3]
\coordinate (origin) at (0,0);
\draw (origin) -| (1,1) to[edge label'={hi}] (\tikztostart-|origin);
\end{scope}
%
\path foreach \X [count=\Y] in {--,to,to w/ edge label}
{(\Y.south) node[below=1em,font=\sffamily]{\X}};
\end{tikzpicture}
\end{document}


• Yes, I didn't mean to imply the problem was with \tikztostart. edge label seems promising. Is the purpose of ' the same as usually in tikz-cd, i.e., make the chicken cross the road? (sorry about the bad pun :) Also, do you know why in M. Al Jumaily's answer, moving the node earlier works? – LaTeXer Nov 29 '20 at 4:08
• @LaTeXer I did not want to imply that you implied that, this was a remark for general readers. ;-) Yes, M. Al Jumaily's syntax is the correct syntax, placing the node after the target coordinate works only for --. The one suggested by M. Al Jumaily's also works for other path constructions, e.g. arcs: \draw (-2,0) arc[start angle=135,end angle=45,radius=2] node[midway,above] {hi}; It also works for --, \draw (0,0) -- node[midway,above] {hi} ++(2,2);, but arguably an edge label looks better, \draw (2,0) to[edge label={hi}] ++ (2,2); for sloped paths. – user229669 Nov 29 '20 at 4:26
• And yes, the frame is the same syntax as in tikz-cd, simply because tikz-cd uses edge labels. It also loads quotes, which offer short cuts, e.g. to["hi"]. – user229669 Nov 29 '20 at 4:27
• Oh, so I've been using node after a path wrong for a long time now! (kinda worked fine so far because I mostly used --). Thanks a lot again! Though I'll give the checkmark to M. Al Jumaily because his answer is what I'll probably end up using because it's easier to remember (sorry)... – LaTeXer Nov 29 '20 at 4:35
• @LaTeXer For -- is is not wrong since it works. But the other syntax is more general. – user229669 Nov 29 '20 at 4:36