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.
\draw (0,0) -- node{a} (2,0) -- node{b}  (2,1)  -- node{c}  (0,1)  -- cycle;

I can't find a simple way to make a label on the last edge. What I want is something like

\draw (0,0) -- node{a} (2,0) -- node{b}  (2,1)  -- node{c}  (0,1)  -- node{d} cycle;

but it doesn't work.

\draw (0,0) coordinate(A) -- node{a} (2,0) -- node{b}  (2,1)  -- 
                             node{c}  (0,1) coordinate(D) -- cycle;
\draw (A) -- node{d} (D);

This works, but seems too complicated for such a simple task and I think I'm missing something.

share|improve this question
    
Welcome to TeX.SX. –  Claudio Fiandrino Feb 10 '13 at 19:19
3  
I would probably use \draw (0,0) -- node{a} (2,0) -- node{b} (2,1) -- node{c} (0,1) -- node {d} (0,0) -- cycle ; (so repeating the first coordinate at the end of the path, and using cycle only to get the final corner right). –  Jake Feb 10 '13 at 19:25
    
This method is much neater than mine, thank you. Still not as elegant as I hoped though... –  Lester Feb 11 '13 at 11:55
    
But isn't there a way to treat the last -- as an edge and label it? –  Dror Feb 22 '13 at 12:54
add comment

1 Answer

up vote 2 down vote accepted

The problem is that the cycle option short circuits the system for putting nodes on paths. The usual path operations have a system whereby they record their path component so that TikZ can figure out where to put nodes afterwards. The -- (lineto) component is no exception to this. The problem is that the cycle interrupts the lineto before it can record the necessary information.

A fix would be to put this information back in. I don't know how robust this fix is, but it works for your example.

\documentclass{article}
%\url{http://tex.stackexchange.com/q/97602/86}
\usepackage{tikz}

\makeatletter
\def\tikz@@close cycle{%
  \edef\tikz@timer@start{\noexpand\pgfqpoint{\the\tikz@lastx}{\the\tikz@lasty}}
  \tikz@flush@moveto%
  \tikz@path@close{\expandafter\pgfpoint\pgfsyssoftpath@lastmoveto}%
  \edef\tikz@timer@end{\noexpand\pgfpoint\pgfsyssoftpath@lastmoveto}%
  \def\pgfstrokehook{}%
  \let\tikz@timer=\tikz@timer@line%
  \tikz@scan@next@command%
}
\makeatother


\begin{document}
\begin{tikzpicture}
\draw (0,0) -- node{a} (2,0) -- node{b}  (2,1)  -- node[pos=.3]{c}  (0,1)  -- node[pos=.3]{d} cycle;
\end{tikzpicture}
\end{document}

closed path with labels

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.