# Get error when I add “pos=x” to the node[] command for the last edge using cycle

Why this code works

\documentclass{standalone}
\usepackage{tikz}
\begin{document}

\begin{tikzpicture}
\draw (pi,.5)--(pi,-.5)--(-pi,-.5)--(-pi,.5)--cycle node[above]{vv} ;
\end{tikzpicture}

\end{document}


But when I change the node[above] to node[above,pos=.5] I get error?

-

It's because cycle makes the path timer undefined. This is a soft spot of TikZ that it cannot move over corners when computing the percentage of the path for node placement. It automatically looks for the last input segment to interpolate e.g. in your example the left side of the rectangle. Here is the problem removed but the functionality is still limited. Also there might be another reason why the developers of TikZ decided to make it as such so better to stay away.

\documentclass{standalone}
\usepackage{tikz}

\makeatletter
\def\tikz@close c{%
\pgfutil@ifnextchar o{\tikz@collect@coordinate@onpath\tikz@lineto@mid c}% oops, a coordinate
{\tikz@@close c}}%
\def\tikz@@close cycle{%
\tikz@flush@moveto%
\tikz@path@close{\expandafter\pgfpoint\pgfsyssoftpath@lastmoveto}%
\def\pgfstrokehook{}%
%  \let\tikz@timer=\@undefined% <-- This is the problem ! Uncomment to have the problem again.
\tikz@scan@next@command%
}
\makeatother

\begin{document}
\begin{tikzpicture}
\draw (pi,.5)--(pi,-.5)--(-pi,-.5)--(-pi,.5)--cycle node[above,pos=0.5]{vv} ;
\end{tikzpicture}
\end{document}


You also need to avoid such constructions and place the node in the corresponding input segment. For example:

\begin{tikzpicture}
\draw (pi,.5)--(pi,-.5) node[pos=0.5]{vv} --(-pi,-.5)--(-pi,.5)--cycle ;
\end{tikzpicture}


Lastly, it might be the case that you wanted to demonstrate this problem but just for the sake of completeness, use rectangle instead of going around four corners and cycle.

-