Here is a solution, via to path
, that allows to use the second required syntax:
\documentclass[tikz,margin=2mm]{standalone}
\usetikzlibrary{calc}
\tikzset{
dim above/.style={to path={\pgfextra{
\pgfinterruptpath
\draw[>=latex,|<->|] let
\p1=($(\tikztostart)!2mm!90:(\tikztotarget)$),
\p2=($(\tikztotarget)!2mm!-90:(\tikztostart)$)
in(\p1) -- (\p2) node[pos=.5,sloped,above]{#1};
\endpgfinterruptpath
}(\tikztostart) -- (\tikztotarget) \tikztonodes
}
},
dim below/.style={to path={\pgfextra{
\pgfinterruptpath
\draw[>=latex,|<->|] let
\p1=($(\tikztostart)!2mm!90:(\tikztotarget)$),
\p2=($(\tikztotarget)!2mm!-90:(\tikztostart)$)
in (\p1) -- (\p2) node[pos=.5,sloped,below]{#1};
\endpgfinterruptpath
}(\tikztostart) -- (\tikztotarget) \tikztonodes
}
},
}
\begin{document}
\begin{tikzpicture}
\draw (0,0) to[dim above=text] (4,-2) to[dim below=other text](0,-4);
\end{tikzpicture}
\end{document}
Improved version by removing (\tikztostart)
To get a continuous path (to fill it for example), you can use:
\documentclass[tikz,margin=2mm]{standalone}
\usetikzlibrary{calc}
\tikzset{
dim above/.style={to path={\pgfextra{
\pgfinterruptpath
\draw[line width=.4pt,>=latex,|<->|] let
\p1=($(\tikztostart)!2mm!90:(\tikztotarget)$),
\p2=($(\tikztotarget)!2mm!-90:(\tikztostart)$)
in (\p1) -- (\p2) node[text=,pos=.5,sloped,above]{#1};
\endpgfinterruptpath
} -- (\tikztotarget) \tikztonodes
}
},
dim below/.style={to path={\pgfextra{
\pgfinterruptpath
\draw[line width=.4pt,>=latex,|<->|] let
\p1=($(\tikztostart)!2mm!90:(\tikztotarget)$),
\p2=($(\tikztotarget)!2mm!-90:(\tikztostart)$)
in (\p1) -- (\p2) node[text=,pos=.5,sloped,below]{#1};
\endpgfinterruptpath
} -- (\tikztotarget) \tikztonodes
}
}
}
\begin{document}
\begin{tikzpicture}
\path[draw=blue,text=red,line width=1pt,fill=orange!50,line cap=round,line join=round]
(0,0)
to[dim above=first text] (4,-1) node[right] {A}
to[dim below=second text](-3,-4) node[below left] {B}
to[dim above=third text] (0,0) node[above=1mm]{C};
\end{tikzpicture}
\end{document}
dim
option might be possible, but having adim
path command is not possible (as long you don't rewrite TikZ internal path parser).|-|
type of arrows. See this question "Line segments with text in the middle in TikZ" and answers for different solutions.