Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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!

share|improve this question
1  
Try the solution that Jake suggests in this question: tex.stackexchange.com/questions/11813/… (I don't know if it will work or not as I haven't tried it yet, which is why I'm posting this as a comment) –  Loop Space Feb 28 '11 at 10:40
    
Thanks for the quick response, @Andrew Stacey! Funny, I was just looking at that question before I decided to post, and I thought at first it doesn't apply to me.. But I'm taking a second look now.. Cheers! –  sdaau Feb 28 '11 at 10:57
add comment

1 Answer

up vote 13 down vote accepted

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}
share|improve this answer
    
Hi @Jake, thanks a lot and +1 for your answer! However, I forgot to mention I want to keep 'decorations', because if I recall correctly, that is the only way to have a scaled (smaller/bigger) arrowhead than the default one? Yup, \pgfarrowsdeclarecombine{|<}{>|}{|}{|}\arrow[scale=0.5]{<}\arrow[scale=0.5]{>} fails.. –  sdaau Feb 28 '11 at 11:03
    
@sdaau: Good point with the scaling. I've added a solution that uses your decorations approach. A neater version would probably be to dig into the source files of the arrow library and redefine proper new arrow tips using pgfarrowsdeclare based on the original arrows. This even allows you to parametrise the tips, e.g. if you want longer end lines (see pp. 610 in the pgf manual‌​) –  Jake Feb 28 '11 at 11:28
1  
Thanks a million, @Jake, it works now :) First, sorry for not exactly reading your answer at first - you were using markings as well! However, the scaling was the main issue for me ... I was trying myself to cook up something - but I can see from your answer, I would have failed at the \arrowreversed step :) Fantastic - thanks again, wish I could +1 that answer once more :) Cheers! –  sdaau Feb 28 '11 at 11:32
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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