4

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

4

News, Oct 7 2023

Since v4.03 (2022-01-19), tkz-euclide learnt two new options dim style and dim fence style in the way introduced in (the first example in) part "Update, Dec 6 2021" below.

Currently, the two new options are documented in texdoc tkz-euclide (v5.02c, 2023-02-03), sec. 23.1.3. "Adding dimensions with option dim new code from Muzimuzhi Z".


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. (archive on web.archive.org, seems the GitHub user tkz-sty was deleted.)

Activities not archived, all by GitHub user Alter Mundus:

  • on 2021-12-03, new comment

    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.

  • on 2022-01-19, issue closed
  • on 2022-01-19, new comment

    Hello, I added your last code in the next version 4.01

Update, Dec 6 2021 (Oct 7 2023, added \IfPackageAtLeastTF conditionals)

Rework option dim to support styles dim style and dim fence style which accept general tikz options.

\documentclass{article}
\usepackage{tkz-euclide}

\makeatletter
% absorbed by tkz-euclide v4.00 (2022-01-04)
% see diff
%  https://tug.org/svn/texlive/trunk/Master/texmf-dist/tex/latex/tkz-euclide/tkz-obj-eu-draw-lines.tex?r1=61495&r2=61496&pathrev=61668&
\IfPackageAtLeastTF{tkz-euclide}{2022-01-04}{}{
  % 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
  }
}

% absorbed by tkz-euclide v4.03 (2022-01-19)
% (actually absorbed since 4.01, but the least released one is 4.03)
% see diff
% - https://tug.org/svn/texlive/trunk/Master/texmf-dist/tex/latex/tkz-euclide/tkz-obj-eu-draw-lines.tex?r1=61667&r2=61668&pathrev=64473&
\IfPackageAtLeastTF{tkz-euclide}{2022-01-19}{}{
  % 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,
  },
}
11
  • 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
    Commented 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. Commented Jul 14, 2020 at 12:53
  • All right, let's hope that Alain Matthes will include your code in his package.
    – AndréC
    Commented Jul 14, 2020 at 16: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. Commented Dec 3, 2021 at 22:45
  • 1
    @AndréC I added an archive link and pasted all the activities from my email boxes. The two new options dim style and dim fence style are available since tkz-euclide v4.03. See answer update for more info. Commented Oct 7, 2023 at 11:03
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 Commented 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 .