[Tex/LaTex] Tikz – move/reposition arrow decorations (arrowhead length/size)

arrowsdecorationslengthstikz-pgf

I'm trying to build a 'dimension line' macro for Tikz:

\documentclass{article}

% note: with \documentclass{minimal}, this example fails with:
% ! Undefined control sequence.
% \pgf@lib@dec@computed@action ...t (0,0.25) {\tiny 
%                                                   {'test'}};}
% l.31 ...ne[($(nA)+(0,1)$)][($(nB)+(0,1)$)]['test']
%                                                    ;
% ? 
% ... but if Enter is pressed at prompt, a PDF is generated anyways by pdflatex.. 


\usepackage{tikz}
\usetikzlibrary{arrows,snakes,backgrounds,patterns,matrix,shapes,fit,calc,shadows,plotmarks,chains,positioning,scopes,decorations.markings}
\usepackage[graphics,tightpage,active]{preview}
\PreviewEnvironment{tikzpicture}

\begin{document}

\def\Dimline[#1][#2][#3]{
    %\node at (0,0) {"test: #1 - #2 ..."};
    \begin{scope}[>=latex] % redef arrow for dimension lines
        \draw[|-|,
            decoration={markings, % switch on markings
                mark=at position 0 with {\arrow[scale=0.5]{<}};,
                mark=at position .5 with {\node[gray] at (0,0.25) {\tiny{#3}};},
                mark=at position 1 with {\arrow[scale=0.5]{>}};,
            },
        postaction={decorate},
        %shorten <=1pt,
        ] #1 -- #2 ;
    \end{scope}
}

\begin{tikzpicture}

    \node at (0,0) (nA) {A};
    \node at (3,0) (nB) {B};
    \Dimline[($(nA)+(0,1)$)][($(nB)+(0,1)$)]['test'] ;

\end{tikzpicture}

\end{document}

… and almost everything works, except the left arrow gets extended to the left (I guess its reference point is at the right end?!).

example rendering

Obviously, I'd like the arrow moved to the right by the length of the arrow, but I cannot find anywhere how I could retrieve the size/length of the arrow?!

Other than that, are there any other ways to get the arrow to 'fit' inside the dimension line?

EDIT: Just to note: I'd like to keep decoration={markings, because I think only that allows me to scale the arrowhead, as in \arrow[scale=0.5]?!

Thanks,
Cheers!

Best Answer

For this application, you can just define a new combined arrowhead using \pgfarrowsdeclarecombine{name left}{name right}{outer element left}{outer element right}{inner element left}{inner element right}:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{arrows,calc,decorations.markings}

\begin{document}
\pgfarrowsdeclarecombine{|<}{>|}{|}{|}{latex}{latex}
\def\Dimline[#1][#2][#3]{
    %\node at (0,0) {"test: #1 - #2 ..."};
    \begin{scope}[>=latex] % redef arrow for dimension lines
        \draw[|<->|,
        decoration={markings, % switch on markings
                mark=at position .5 with {\node[gray] at (0,0.25) {\tiny{#3}};},
        },
        postaction=decorate] #1 -- #2 ;
    \end{scope}
}

\begin{tikzpicture}

    \node at (0,0) (nA) {A};
    \node at (3,0) (nB) {B};
    \Dimline[($(nA)+(0,1)$)][($(nB)+(0,1)$)]['test'] ;

\end{tikzpicture}

\end{document}

measurement lines with tikz


Or, in order to be able to scale the arrow tips at will, here's your approach with the decorations. The latex tips are combined with "empty arrows" to fix the protrusion over the end of the line:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{arrows,calc,decorations.markings}

\begin{document}
\pgfarrowsdeclarecombine{dimarrow}{dimarrow}{latex}{latex}{}{}
\def\Dimline[#1][#2][#3]{
        \draw[|-|,
        decoration={markings, % switch on markings
                mark=at position 0 with {\arrowreversed[scale=0.5]{dimarrow}};,
                mark=at position .5 with {\node[gray] at (0,0.25) {\tiny{#3}};},
                mark=at position 1 with {\arrow[scale=0.5]{dimarrow}};,
            },
        postaction=decorate] #1 -- #2 ;
}

\begin{tikzpicture}

    \node at (0,0) (nA) {A};
    \node at (3,0) (nB) {B};
    \Dimline[($(nA)+(0,1)$)][($(nB)+(0,1)$)]['test'] ;

\end{tikzpicture}

\end{document}