11

How do you add dimension labels to TikZ pictures? Ideally, it would look something like this:

|<----- dimension ----->|

I would like to do this to show the measurement of both straight lines (i.e. sides of a polygon) and curved lines (i.e. circumference/arc length on a circle). Thanks in advance for your help!

m0nhawk
  • 9,664

1 Answers1

13

For educational and show-off purposes only!

\documentclass[tikz]{standalone}
\usetikzlibrary{decorations.markings}

\tikzset{measure length/.style={
              decoration={markings,
              mark connection node=a,
              mark =at position 0.5 with {
                 \node[transform shape,fill=white,scale=0.5,#1] (a) 
                        {\pgfmathparse{\pgfdecoratedpathlength/2.845274}%
                        \pgfmathprintnumber[fixed,precision=1]\pgfmathresult mm};},
             },
             postaction=decorate
       }
}
\begin{document}

\begin{tikzpicture}
\draw[measure length] (-2,-2cm) -- (2cm,-2cm);
\draw[measure length={rotate=180}] (0,0) arc (0:270:1cm) ;
\draw[measure length={rotate=180}] (0,3) arc (0:270:1cm) -- ++(10mm,0);

\foreach \x in {0,...,5}{
\draw[measure length] (2,2) +(\x*60:1cm) -- +({(\x+1)*60)}:1cm);
}

\end{tikzpicture}

\end{document}

enter image description here

percusse
  • 157,807