1

How I can change the distance dimension like the picture below. In the \dim={add dim, distance} have two parameter I want to add 1 parameter for distance of dimesion like picture 1 , and rolate the text in picture 2. enter image description here

enter image description here

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations,decorations.markings,decorations.text}

\begin{document}
 \pgfkeys{/pgf/decoration/.cd,
      distance/.initial=10pt, 
      distance2/ .initial=10pt
}  

\pgfdeclaredecoration{add dim}{final}{
\state{final}{% 
\pgfmathsetmacro{\dist}{45pt*\pgfkeysvalueof{/pgf/decoration/distance}
            /abs(\pgfkeysvalueof{/pgf/decoration/distance})}    
          \pgfpathmoveto{\pgfpoint{0pt}{0pt}}             
          \pgfpathlineto{\pgfpoint{0pt}{\dist}}   
          \pgfpathmoveto{\pgfpoint{\pgfdecoratedpathlength}{0pt}} 
          \pgfpathlineto{\pgfpoint{(\pgfdecoratedpathlength}{\dist}}     
          \pgfsetarrowsstart{latex}
          \pgfsetarrowsend{latex}
          \pgfpathmoveto{\pgfpoint{0pt}{\dist}}
          \pgfpathlineto{\pgfpoint{\pgfdecoratedpathlength}{\dist}} 
          \pgfusepath{stroke} 
          \pgfpathmoveto{\pgfpoint{0pt}{0pt}}
          \pgfpathlineto{\pgfpoint{\pgfdecoratedpathlength}{0pt}}
}}

\tikzset{dim/.style args={#1,#2,#3}{decoration={add dim,distance=#2},
                decorate,
                postaction={decorate,decoration={text along path,
                                                 raise=#2,
                                                 text align={align=center},
                                                 text={#1}}}}}

\begin{tikzpicture}
\coordinate (A) at (0,0);
\coordinate (B) at (4,2);

\draw[dim={35 cm,34pt,34pt}]  (A) --  (B); 

\draw[fill=gray] (A) circle(2pt); 
\draw[fill=gray] (B) circle(2pt);

\end{tikzpicture}

\end{document}
3
  • What is the difference between #2 and #3.
    – Symbol 1
    Jul 20 '17 at 12:00
  • And what is distance2 for?
    – cfr
    Jul 20 '17 at 18:43
  • distance2 is #3 it's dist in the code. Jul 21 '17 at 7:38
2

You need to transform a little your code. The distance is given by the second parameter #2

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations,decorations.markings,decorations.text}

\begin{document}
\pgfkeys{/pgf/decoration/.cd, distance/.initial = 10pt}  

\pgfdeclaredecoration{add dim}{final}{
\state{final}{% 
\pgfmathsetmacro{\dist}{\pgfkeysvalueof{/pgf/decoration/distance}}
          \pgfpathmoveto{\pgfpoint{0pt}{0pt}}             
          \pgfpathlineto{\pgfpoint{0pt}{1.2*\dist}}   
          \pgfpathmoveto{\pgfpoint{\pgfdecoratedpathlength}{0pt}} 
          \pgfpathlineto{\pgfpoint{(\pgfdecoratedpathlength}{1.2*\dist}}     
          \pgfsetarrowsstart{latex}
          \pgfsetarrowsend{latex}
          \pgfpathmoveto{\pgfpoint{0pt}{\dist}}
          \pgfpathlineto{\pgfpoint{\pgfdecoratedpathlength}{\dist}} 
          \pgfusepath{stroke} 
          \pgfpathmoveto{\pgfpoint{0pt}{0pt}}
          \pgfpathlineto{\pgfpoint{\pgfdecoratedpathlength}{0pt}}
}}

\tikzset{
    dim/.style args={#1,#2,#3}{%
                decoration = {add dim,distance=\ifx&#2&0pt\else#2\fi},
                decorate,
                postaction = {%
                   decorate,
                   decoration={%
                        raise=#2,
                        markings,
                        mark=at position .5 with {%
                        \node[inner sep=0pt,
                              font=\footnotesize,
                              fill=white,
                              #3] at (0,0) {#1};}
                   }
                }
    },
    dim/.default={,0pt,}
}

\begin{tikzpicture}
\coordinate (A) at (0,0);
\coordinate (B) at (4,2);

\draw[dim={35 cm,10pt,}]  (A) --  (B); 

\draw[fill=gray] (A) circle(2pt); 
\draw[fill=gray] (B) circle(2pt);

\end{tikzpicture}

\begin{tikzpicture}
\coordinate (A) at (0,0);
\coordinate (B) at (4,2);

\draw[dim={35 cm,20pt,transform shape}]  (A) --  (B); 

\draw[fill=gray] (A) circle(2pt); 
\draw[fill=gray] (B) circle(2pt);

\end{tikzpicture}
\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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