5

MWE:

\documentclass[tikz, border=1cm]{standalone}
\usepackage{tikz-dimline}
\begin{document}
\begin{tikzpicture}[
    plotmark/.style = {%
    draw, fill=red, circle, inner sep=0pt, minimum size=4pt
  }
]
    \coordinate (A) at (0,0);
    \coordinate (B) at (5,3);

    \draw (A)--(B);
    \dimline {(A)}{(B)}{label};

    \node[plotmark, label={above:$A$}] at (A) {};
    \node[plotmark, label={above:$B$}] at (B) {};
\end{tikzpicture}  
\end{document}

enter image description here

Question: How do I position the label and its two arrow lines, to the other end of the gray lines?

10

This is a pure calc alternative in case dimline doesn't allow you to do that. (I do not know what dimline does.) This is an updated answer in which I try to address your comments. It comes with a decoration indicate dimensions, which has one argument, the label. All distances and other parameters are stored in pgfkeys. Drawing the line plus all the dimension lines and perpendicular lines boils down to

 \draw[postaction={indicate dimensions={label}}] (A)--(B);

Here is the code with many examples.

\documentclass[tikz, border=1cm]{standalone}
\usetikzlibrary{calc,decorations.pathreplacing,arrows.meta}
\newif\ifdrawdimlineleft
\newif\ifdrawdimlineright
\tikzset{dimlabel distance/.initial=5mm,
vertical lines extend/.initial=5mm,
vertical dim line/.style={gray,thin},
dim arrow line/.style={latex-latex,thin},
dim label/.style={},
left dimline/.is if=drawdimlineleft,
left dimline=true,
right dimline/.is if=drawdimlineright,
right dimline=true,
indicate dimensions/.style={decorate,decoration={
show path construction,
lineto code={
\draw[dim arrow line]
    ($ (\tikzinputsegmentfirst)!\pgfkeysvalueof{/tikz/dimlabel distance}!-90:(\tikzinputsegmentlast) $) -- ($ (\tikzinputsegmentlast)!\pgfkeysvalueof{/tikz/dimlabel distance}!90:(\tikzinputsegmentfirst) $)
 \ifx#1\empty
 \else
 node[midway,sloped,fill=white,dim label]{#1}
 \fi;
\ifdrawdimlineleft
\draw[vertical dim line] (\tikzinputsegmentfirst) -- 
($ (\tikzinputsegmentfirst)!\pgfkeysvalueof{/tikz/vertical lines
extend}!-90:(\tikzinputsegmentlast) $);
\fi
\ifdrawdimlineright
\draw[vertical dim line]    (\tikzinputsegmentlast) -- 
    ($ (\tikzinputsegmentlast)!\pgfkeysvalueof{/tikz/vertical lines extend}!90:(\tikzinputsegmentfirst) $); 
\fi 
}}}}

\begin{document}
\begin{tikzpicture}[font=\sffamily,
    plotmark/.style = {%
    draw, fill=red, circle, inner sep=0pt, minimum size=4pt
  }
]
  \begin{scope}[local bounding box=basic]
    \coordinate (A) at (0,0);
    \coordinate (B) at (5,3);

    \draw[postaction={indicate dimensions={label}}] (A)--(B);

    \node[plotmark, label={above:$A$}] at (A) {};
    \node[plotmark, label={above:$B$}] at (B) {};
  \end{scope}
  \node at (basic.north) {basic application};   
  %
  \begin{scope}[xshift=6cm,local bounding box=no left]
    \coordinate (A) at (0,0);
    \coordinate (B) at (5,3);
    \draw[postaction={indicate dimensions={label},left dimline=false}] (A)--(B);
    \node[plotmark, label={above:$A$}] at (A) {};
    \node[plotmark, label={above:$B$}] at (B) {};
  \end{scope}
  \node at (no left.north) {omit a vertical line};  
  %
  \begin{scope}[yshift=-6cm,local bounding box=no label]
    \coordinate (A) at (0,0);
    \coordinate (B) at (5,3);
    \draw[postaction={indicate dimensions=\empty}] (A)--(B);
    \node[plotmark, label={above:$A$}] at (A) {};
    \node[plotmark, label={above:$B$}] at (B) {};
  \end{scope}
  \node at (no label.north) {omit label};   
  %
  \begin{scope}[xshift=6cm,yshift=-6cm,local bounding box=change arrow]
    \coordinate (A) at (0,0);
    \coordinate (B) at (5,3);
    \draw[postaction={indicate dimensions={label},
        dim arrow line/.style={Bar-Circle,thin}}] (A)--(B);
    \node[plotmark, label={above:$A$}] at (A) {};
    \node[plotmark, label={above:$B$}] at (B) {};
  \end{scope}
  \node at (change arrow.north) {change arrow appearance};  
  %
  \begin{scope}[yshift=-12cm,local bounding box=no line]
    \coordinate (A) at (0,0);
    \coordinate (B) at (5,3);
    \draw[postaction={indicate dimensions={label},dim arrow
    line/.style={opacity=0},dim label/.style={opacity=1}}] (A)--(B);
    \node[plotmark, label={above:$A$}] at (A) {};
    \node[plotmark, label={above:$B$}] at (B) {};
  \end{scope}
  \node at (no line.north) {omit arrow};    
  %
  \begin{scope}[xshift=6cm,yshift=-12cm,local bounding box=no slope]
    \coordinate (A) at (0,0);
    \coordinate (B) at (5,3);
    \draw[postaction={indicate dimensions={label},
        dim label/.style={sloped=false}}] (A)--(B);
    \node[plotmark, label={above:$A$}] at (A) {};
    \node[plotmark, label={above:$B$}] at (B) {};
  \end{scope}
  \node at (no slope.north) {don't slope the label};    
\end{tikzpicture}  
\end{document}

enter image description here

6
  • @blackened For this there exists the angles library, which is described in detail in section 39 Angle Library of the pgfmanual. – user121799 Jan 1 '19 at 17:54
  • 1
    @blackened \path[postaction={indicate dimensions={label},left dimline=false}] (A)--(B);, i.e. \path instead of \draw. BTW, I think I have an idea how to fix the dimension too large errors with xfp. Could you please indicate which codes have caused trouble? – user121799 Feb 8 '19 at 15:04
  • 1
    I have mentioned two of them (and you said one of them is gone) here: tex.stackexchange.com/q/466803/48787 – blackened Feb 8 '19 at 15:12
  • 1
    @blackened I edited the answer. Why do I not want to add all these examples? (1) Because the list of possible modifications is sheer endless. What if someone wants the arrow to become blue, say? (2) Because it is always the same trick: change some styles that are in the pgf keys. That is, already the original answer was rather general, you only need to modify the respective styles. – user121799 Feb 8 '19 at 19:29
  • @marmot: Awesome! Have you considered making a package out of this? Also, is the latest version? – Peter Grill Jul 13 '19 at 5:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.