3

Is there a way to personalize the lines constructed with dim? Make them dotted and red, for instance?

\documentclass[tikz,border=2cm]{standalone}

\usepackage{tkz-euclide}

\begin{document}

    \begin{tikzpicture}
        \tkzDefPoints{0/3/A, 0/-3/B}
        \tkzDrawPoints(A,B) 
        \tkzDrawSegment[dim={\(l\),2cm,right=2mm}](A,B)
        \tkzLabelPoints[left](A,B)
    \end{tikzpicture}
\end{document}

enter image description here

2

2 Answers 2

3

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}

enter image description here

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}

enter image description here

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,
  },
}
9
  • 1
    Alain Matthes (tkz-euclide maintainer) gave the code to have dotted lines here : Draw dimension of a line as a decoration in TikZ
    – AndréC
    Jul 14, 2020 at 6:37
  • @AndréC That answer hardcoded \pgfsetdash{{0.1cm}{0.1cm}{0.1cm}{0.1cm}}{0cm}, which is even less flexible than mine. Jul 14, 2020 at 12:53
  • All right, let's hope that Alain Matthes will include your code in his package.
    – AndréC
    Jul 14, 2020 at 16:50
  • Code adaptation is not obvious. The options must be optional. tkz-euclide is designed to make geometrical figures and I think it is better to use TikZ for this kind of problem. Dec 3, 2021 at 14:50
  • 1
    @AlainMatthes I've fixed the initial value of dim color, so it's now optional as well. dim provides a far better interface than tikz codes, and using colors in geometrical figures really enhances expression. A combined style key like dim style would be better, but then manually communication between tikz options and pgf drawing codes is unavoidable. Dec 3, 2021 at 22:45
3

Here is an easy way with plain TikZ. In case you need a lot of such dimline, then simly make a new command.

enter image description here

\documentclass[tikz,border=5mm]{standalone}
\begin{document}
\begin{tikzpicture}
\path 
(0,3)  coordinate (A) node[left]{$A$}
(0,-3) coordinate (B) node[left]{$B$};
\draw[red,dotted] (A)--+(0:2.5) (B)--+(0:2.5);
\draw[red,<->] ([shift={(0:2)}]A)--([shift={(0:2)}]B) node[midway,right]{$l$};
\draw[thick,blue] (A)--(B);
\fill[blue] (A) circle(1.5pt) (B) circle(1.5pt);
\end{tikzpicture}
\end{document}
1
  • A new command ? I'm not sure but an option like the last code of muzimuzhi Z yes Jan 16, 2022 at 17:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .