Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer

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}

enter image description here

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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