2

I am writing a macro definition that draws dimensions in the style of technical drawing. I need to perform an inequality of two of the arguments to know which is coordinate is higher up, but the console doesn't understand the condition I used as the \ifthenelse argument.

%   (#1,#2):    Starting coordinate
%   (#3,#4):    Ending coordinate
%   #5:         Vertical upwards distance from the body
%   #6:         Dimension text
\usetikzlibrary{calc}
\def\DimensionTop(#1,#2)(#3,#4)[#5,#6]{
    \ifthenelse{#2>#4}
    {   %   If point (#1,#2) is higher than (#3,#4)
        \coordinate (D1) at ($ (#1,#2) + (0,#5) $);
        \coordinate (D2) at ($ (#3,#4) + (0,#2-#4) + (0,#5) $);
    }
    {   %   If point (#3,#4) is higher than (#1,#2)
        \coordinate (D1) at ($ (#1,#2) + (0,#4-#2) + (0,#5) $);
        \coordinate (D2) at ($ (#3,#4) + (0,#5) $);
    }
    \draw (#1,#2) -- (D1) -- ++(0,0.2);
    \draw (#3,#4) -- (D2) -- ++(0,0.2);
    \draw[ , >=latex, thin ] (D1) -- (D2) node[ fill=white, midway ] {$\mathtt{#6}$};
}

The function is used like this in the main document

\documentclass[border=2pt,convert={outext=.png}]{standalone}
\usepackage{tikz}
\usetikzlibrary{patterns}
\usetikzlibrary{arrows}
\begin{document}
    \begin{tikzpicture}

        % Custom command for the background grid, ignore
        \GuideCartesian(-2,-2)(8,8);

        \draw[ very thick ] (0,4) -- (2,5);
        \DimensionTop(0,4)(2,5)[1,1.50];

    \end{tikzpicture}
\end{document}

Since the console doesn't understand the condition, it goes directly to the else case, which works when the point on the right is higher than the point on the left as in the picture.

Horizontal Dimension

What would be a correct way to express this condition?

1
  • 3
    Don't show only snippets. Make a complete example, that makes testing much easier. Commented Mar 11, 2020 at 16:43

1 Answer 1

4

You only need to compute the maximum of two y coordinates. Since you are using calc, you may just do

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}[pics/dimension top/.style={code={
     \tikzset{dimension top/.cd,#1}
     \def\pv##1{\pgfkeysvalueof{/tikz/dimension top/##1}}%  
     \draw
     let \p1=\pv{first},\p2=\pv{second},\n1={max(\y1,\y2)} in
     (\x1,\y1) -- (\x1,\n1+0.2cm) (\x2,\y2) -- (\x2,\n1+0.2cm) 
     (\x1,\n1) -- node[above,midway]{\pv{text}} (\x2,\n1) ;}},
     dimension top/.cd,first/.initial={(0,0)},second/.initial={(1,0)},
     text/.initial=]
  \draw (0,4) -- (2,5);
  \pic{dimension top={first={(0,4)},second={(2,5)},text=1.5}};
\end{tikzpicture}
\end{document}

enter image description here

You must log in to answer this question.

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