I'm trying to annotate a technical drawing made in tikz. Following the elegant work of Tarass, I needed to be able to annotate to the side like depicted below because my annotation text can be longer than the distance between annotations (I know in the test example here it is not):
I have come up with this solution which works well for the boundary cases horizontal and vertical:
% right hand side horizontal [node1 is left of node2]
\Cote[2 cm]{(node1)}{(node2)}{\phantom{$L_\text{test}$}}<h>[Cote node/.append style={pos=1, anchor=west, name=dummypos}];
\draw (node2|-dummypos) -- (dummypos.east);
\node at (dummypos)[above]{$L_\text{test}$};
% left hand side horizontal
\Cote[2 cm]{(node1)}{(node2)}{\phantom{$L_\text{test}$}}<h>[Cote node/.append style={pos=0,anchor=east,name=dummypos}];
\draw (node2|-dummypos) -- (dummypos.west);
\node at (dummypos)[above]{$L_\text{test}$};
% above vertical [node2 is above node3]
\Cote[2cm]{(node2)}{(node3)}{\phantom{$L_\text{test}$}}<v>[Cote node/.append style={pos=1,anchor=west,name=dummypos,rotate=90}];
\draw (node3-|dummypos) -- (dummypos.east);
\node at (dummypos)[above,rotate=90]{$L_\text{test}$};
% below vertical:
\Cote[2cm]{(node2)}{(node3)}{\phantom{$L_\text{test}$}}<v>[Cote node/.append style={pos=0,anchor=east,name=dummypos,rotate=90}];
\draw (node3-|dummypos) -- (dummypos.west);
\node at (dummypos)[above,rotate=90]{$L_\text{test}$};
I think this works quite well, and adapts the extended bar to the right dimension automatically. But of course it is wieldy to write this many times, so I thought I can integrate it in the \Cote command but here I fail.
- Anyone capable of integrating this to the
NewDocumentCommandof the originalCotecommand (see link above) as an option for the cases h, v to specify if one wants the annotation next to the dimension (r,l,t,b - right, left, top, bottom)? - As a plus, likely useful to some, it would be nice to have it also cover the oblique case (which I have not solved yet as I do not need that right now).
Update:
Just noticed, that I needed the arrow to start on the outside of the dimension lines in these cases, so please update the above examples with:
\Cote[2 cm]{(node1)}{(node2)}{\phantom{$L_\text{test}$}}<h>[Cote node/.append style={pos=1, anchor=west, name=dummypos, xshift=3pt}, Cote arrow/.append style={>-<, shorten >= -3pt,shorten <= -3pt}];
\draw (node2|-dummypos) -- (dummypos.east);
\node at (dummypos)[above]{$L_\text{test}$};
MWE
%! TEX root = standard_socket.tex
\documentclass[tikz,border=3pt 0 0 0]{standalone}
% taken from https://tex.stackexchange.com/a/180110/97536
\usepackage{xparse}
\usetikzlibrary{calc}
\RequirePackage{pdftexcmds}
\makeatletter
\let\pdfstrcmp\pdf@strcmp
\let\pdffilemoddate\pdf@filemoddate
\tikzset{%
Cote node/.style={%
midway,
%sloped,
fill=white,
inner sep=1.5pt,
outer sep=2pt
},
Cote arrow/.style={%
<->,
>=latex,
very thin
}
}
\makeatletter
\NewDocumentCommand{\Cote}{%
s % cotation avec les fleches a lexterieur
D<>{1.5pt} % offset des traits
O{.75cm} % offset de cotation
m % premier point
m % second point
m % etiquette
D<>{o} % () coordonnées -> angle
% h -> horizontal,
% v -> vertical
% o or what ever -> oblique
O{} % parametre du tikzset
}{%
{\tikzset{#8}
\coordinate (@1) at #4 ;
\coordinate (@2) at #5 ;
\if #7H % Cotation traits horizontaux
\coordinate (@0) at ($($#4!.5!#5$) + (#3,0)$) ;
\coordinate (@5) at ($#5+(#3,0)$) ;
\coordinate (@4) at ($#4+(#3,0)$) ;
\else
\if #7V % Cotation traits verticaux
\coordinate (@0) at ($($#4!.5!#5$) + (#3,0)$) ;
\coordinate (@5) at ($#5+(0,#3)$) ;
\coordinate (@4) at ($#4+(0,#3)$) ;
\else
\if #7v % Cotation verticale
\coordinate (@0) at ($($#4!.5!#5$) + (#3,0)$) ;
\coordinate (@4) at (@0|-@1) ;
\coordinate (@5) at (@0|-@2) ;
\else
\if #7h % Cotation horizontale
\coordinate (@0) at ($($#4!.5!#5$) + (0,#3)$) ;
\coordinate (@4) at (@0-|@1) ;
\coordinate (@5) at (@0-|@2) ;
\else % cotation encoche
\ifnum\pdfstrcmp{\unexpanded\expandafter{\@car#7\@nil}}{(}=\z@
\coordinate (@5) at ($#7!#3!#5$) ;
\coordinate (@4) at ($#7!#3!#4$) ;
\else % cotation oblique
\coordinate (@5) at ($#5!#3!90:#4$) ;
\coordinate (@4) at ($#4!#3!-90:#5$) ;
\fi\fi\fi\fi\fi
\draw[very thin,shorten >= 2*#2,shorten <= -2*#2] (@4) -- #4 ;
\draw[very thin,shorten >= 2*#2,shorten <= -2*#2] (@5) -- #5 ;
\IfBooleanTF #1 {% avec etoile
\draw[Cote arrow,-] (@4) -- (@5)
node[Cote node] {#6\strut};
\draw[Cote arrow,<-] (@4) -- ($(@4)!-6pt!(@5)$) ;
\draw[Cote arrow,<-] (@5) -- ($(@5)!-6pt!(@4)$) ;
}{% sans etoile
\ifnum\pdfstrcmp{\unexpanded\expandafter{\@car#7\@nil}}{(}=\z@
\draw[Cote arrow] (@5) to[bend right]
node[Cote node] {#6\strut} (@4) ;
\else
\draw[Cote arrow] (@4) -- (@5)
node[Cote node] {#6\strut};
\fi
}}
}
\makeatother
\begin{document}
\begin{tikzpicture}[]
% coordinates
\coordinate(node1) at (0,0);
\coordinate(node2) at (1,0);
\draw[red](node1) -- (node2);
%Connotations
% horizontal
\Cote[1cm]{(node1)}{(node2)}{\phantom{Ltest}}<h>[Cote node/.append style={pos=1, anchor=west, name=Llinext, xshift=3pt}, Cote arrow/.append style={>-<, shorten >= -3pt,shorten <= -3pt}];
\draw[Cote arrow, -] (node2|-Llinext) -- (Llinext.east);
\node at (Llinext)[above]{Ltest};
\end{tikzpicture}
\end{document}


