I am trying to setup an command to create technical drawing dimensions (inspiration from Dimensioning of a technical drawing in TikZ).
I would like to use pgfkeys
for that, in order to pass multiple optional arguments in the same spirit of TikZ
.
I works pretty fine (at least I can get what I need) except within an axis
environment (from pgfplots
). I seems that all pgfkeys
from all dimline
calls are overridden by the last one.
I am using last MacTex for MacOSX.
I am compiling the source using:
/usr/texbin/xelatex -interaction nonstopmode -shell-escap -output-directory . test3.tex
Any idea why the axis
environment zaps all dimline
options?
Here is my code:
\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{calc}
\pgfkeys{
dimline/.is family,
dimline,
dimcolor/.initial = black,
start line/.initial=1cm,
start line width/.initial=0.01,
end line/.initial=1cm,
end line width/.initial=0.01,
label pos/.initial={},
}
% #1: optional keys parameters
% #2: start point
% #3: end point
% #4: text
\newcommand{\dimline}[4][]{%
\pgfkeys{
dimline,
#1,
start line width/.get=\dimlinestartlinewidth,
start line/.get=\dimlinestartline,
end line/.get=\dimlineendline,
end line width/.get=\dimlineendlinewidth,
label pos/.get=\labelpos,
}
\begin{scope}[
dimrecall/.style={\pgfkeysvalueof{/dimline/dimcolor}},
dimline/.style={\pgfkeysvalueof{/dimline/dimcolor}},
]
\node (a) at #2 {};
\node (b) at #3 {};
\draw [/tikz/dimrecall, line width=\dimlinestartlinewidth]
(a.center) -- ($(a)!\dimlinestartline!-90:(b)$);
\draw [dimrecall, line width=\dimlineendlinewidth]
(b.center) -- ($(b)!\dimlineendline!90:(a)$);
\draw[|<->|, dimline,] (a.center) -- (b.center)
node[fill=white, sloped, midway] {\tiny{#4}};
\end{scope}
}
\begin{document}
Correct without \texttt{axis} environment:
\begin{tikzpicture}
\draw (0,0) -- (0,4) -- (.5,4) -- (.5,5) -- (1.,5) -- (1.,4) -- (1.5,4) -- (1.5,0) -- cycle;
\dimline[dimcolor=blue,start line=-0.25, end line=-0.25]{(3,0)}{(3,4)}{4.0};
\dimline[dimcolor=red,start line=0.85, end line=0.85]{(0,6)}{(1.5,6)}{1.5};
\end{tikzpicture}
Incorrect within \texttt{axis} environment \texttt{pgfkeys} are overriden:
\begin{tikzpicture}
\begin{axis} [
xmin = -1,
xmax = 8,
ymin = -1,
ymax = 8]
\addplot[mark=none, color=black] coordinates {
(0,0) (0,4) (.5,4) (.5,5) (1.,5) (1.,4) (1.5,4) (1.5,0) (0,0)
};
\dimline[dimcolor=blue,start line=-0.25, end line=-0.25]{(axis cs: 3,0)}{(axis cs:3,4)}{4.0};
\dimline[dimcolor=red,start line=0.85, end line=0.85]{(axis cs: 0,6)}{(axis cs: 1.5,6)}{1.5};
\end{axis}
\end{tikzpicture}
\end{document}
and the result