It seems the style of \tkzDrawSegment[dim={...}]
is hardcoded, and is written directly in pgf codes. Currently I am not capable of extending it and providing flexible user options, and below is my first try.
- Update, Dec 4 2021:
Fix initial value of
dim color
, restrict scope
- Update, Dec 6 2021:
Rework
dim
option in terms of decoration show path construction
. New styles dim style
and dim fence style
.
\documentclass[tikz,border=2cm]{standalone}
\usepackage{tkz-euclide}
\makeatletter
% originally defined in tkz-obj-eu-draw-lines.tex
\def\tkz@DrawSegment[#1](#2,#3){%
% replace \(begin|end)group with \(|end)scope
\scope
\pgfqkeys{/tkzdraws}{#1}
\draw[line style,add=0 and 0,#1] (#2) to (#3);
\endscope
}%
\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}}
% start of patch
\pgfusepath{stroke}
\pgfsetarrowsstart{latex}
\pgfsetarrowsend{latex}
\expandafter\pgfsetdash\tkz@dim@dashpattern
\pgfsetstrokecolor{\tkz@dim@color}
% end of patch
\pgfpathmoveto{\pgfpoint{0pt}{\dist}}
\pgfpathlineto{\pgfpoint{\pgfdecoratedpathlength}{\dist}}
\pgfusepath{stroke}
\pgfpathmoveto{\pgfpoint{0pt}{0pt}}
\pgfpathlineto{\pgfpoint{\pgfdecoratedpathlength}{0pt}}
}}
\tikzset{
dim color/.store in=\tkz@dim@color,
dim color=black,
% similar to code for \tikzoption{dash pattern}{...}
dim dash pattern/.code={%
\def\tikz@temp{#1}%
\ifx\tikz@temp\pgfutil@empty%
\def\tkz@dim@dashpattern{{}{0pt}}%
\else%
\def\tikz@dashpattern{}%
\expandafter\tikz@scandashon\pgfutil@gobble#1o\@nil%
\edef\tkz@dim@dashpattern{{\tikz@dashpattern}{\noexpand\tikz@dashphase}}%
\fi
},
dim dash pattern=,
}
\makeatother
\begin{document}
\begin{tikzpicture}
\tkzDefPoints{0/3/A, 0/-3/B}
\tkzDrawPoints(A,B)
\tkzDrawSegment[dim={\(l_0\),1cm,right=2mm}, dim color=red, dim dash pattern={on 2pt off 2pt}](A,B)
\tkzDrawSegment[dim={\(l_1\),2cm,right=2mm}, dim color=red](A,B)
\tkzDrawSegment[dim={\(l_2\),3cm,right=2mm}](A,B)
\tkzLabelPoints[left](A,B)
\end{tikzpicture}
\end{document}

I've opened a feature request here: https://github.com/tkz-sty/tkz-euclide/issues/5.
Update, Dec 6 2021
Rework option dim
to support styles dim style
and dim fence style
which accept general tikz options.
\documentclass{article}
\usepackage{tkz-euclide}
\makeatletter
% originally defined in tkz-obj-eu-draw-lines.tex
\def\tkz@DrawSegment[#1](#2,#3){%
\scope
\pgfqkeys{/tkzdraws}{#1}
\draw[line style,add=0 and 0, #1] (#2) to (#3);
\endscope
}
% rework `dim` option using decoration `show path construction`
\tikzset{
dim/.style args={#1,#2,#3}{%
postaction={
decoration={
show path construction,
lineto code={
% dim fence
\draw[dim fence style/.try]
(\tikzinputsegmentfirst) --
($ (\tikzinputsegmentfirst)!1.2*(#2)!90:(\tikzinputsegmentlast) $)
(\tikzinputsegmentlast) --
($ (\tikzinputsegmentlast)!1.2*(#2)!-90:(\tikzinputsegmentfirst) $);
% dim
\draw[dim style/.try]
($ (\tikzinputsegmentfirst)!#2!90:(\tikzinputsegmentlast) $) --
node[inner sep=0pt, font=\footnotesize, fill=\tkz@fillcolor, pos=.5, #3] {#1}
($ (\tikzinputsegmentlast)!#2!-90:(\tikzinputsegmentfirst) $);
}
},
decorate,
}
},
dim/.default={,0pt,},
dim style/.style={
latex-latex,
},
}
\makeatother
\begin{document}
\begin{tikzpicture}
\tkzDefPoints{0/3/A, 1/-3/B}
\tkzDrawPoints(A,B)
% test new style `dim style`
\tkzDrawSegment[dim={\(l_0\),1cm,right=2mm},
dim style/.append style={red, dash pattern={on 2pt off 2pt}}](A,B)
\tkzDrawSegment[dim={\(l_1\),2cm,right=2mm},
dim style/.append style={blue}](A,B)
\tkzDrawSegment[dim={\(l_2\),3cm,right=2mm}](A,B)
% test new style `dim fence style`
\tkzDrawSegment[dim={\(l_3\),-2cm,right=2mm},
dim style/.append style={orange},
dim fence style/.style={dashed}](A,B)
\tkzLabelPoints[left](A,B)
\end{tikzpicture}
\end{document}

It's possible to rework dim
in terms of execute at end to
but there's currently an issue about this option, see pgf-tikz/pgf#1086
.
% rework option `dim`, using `execute at end to` instead of `decorate`
% FIXME: `execute at end to` will affect the graphics state of original `to`
% see https://github.com/pgf-tikz/pgf/issues/1086
\tikzset{
dim/.style args={#1,#2,#3}{%
execute at end to={
\draw[dim fence style/.try] % is "fence" an appropriate name?
(\tikztostart) -- ($ (\tikztostart)!1.2*(#2)!90:(\tikztotarget) $)
(\tikztotarget) -- ($ (\tikztotarget)!1.2*(#2)!-90:(\tikztostart) $);
\draw[dim style/.try]
($ (\tikztostart)!#2!90:(\tikztotarget) $)
-- node[black, inner sep=0pt, font=\footnotesize, fill=\tkz@fillcolor, #3] {#1}
($ (\tikztotarget)!#2!-90:(\tikztostart) $);
}
},
% init
dim/.default={,0pt,},
dim style/.style={
latex-latex,
},
}