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 would like to implement a command in LaTeX which would sum two numbers before comparing them to a third number as follows:

\ifnum #1+45>0
    above
\else 
    left
\fi

where #1 is an angle in degrees.

Unfortunately it doesn't work.

Please, could you help me how to fix that?

Many thanks.

The whole code after advice from Werner:

\documentclass{memoir}
\usepackage{tikz}
\usetikzlibrary{arrows}

\makeatletter
\newcommand{\compare}[1]{%
  \ifdim\dimexpr#1pt+45pt\relax>\z@\relax
    above%
  \else 
    left%
  \fi
}
\makeatother

% define arrows
% general right to left double harpoon
% define four connection position #5 and connect the arrows in +-5degrees
% first parameter define origin node 
% second parameter define destination node,
% third parameter label of top arrow
% fourth parameter label of bottom arrow
% fifth parameter define connection point - angle in degrees on the origin node
\newcommand{\grldh}[5]{
    \draw[-left to,thick] ({#1}.{#5+5}) -- node[\compare{#5}] {\scriptsize #3} ({#2}.{#5+175});
    \draw[left to-,thick] ({#1}.{#5+355}) -- node[below] {\scriptsize #4} ({#2}.{#5+185}); 
}

\begin{document}
\pagenumbering{gobble}
  \begin{tikzpicture}[
    state/.style={
    % The shape:
    circle,minimum size=3mm,rounded corners=3mm,
    },
     scale=1.5]
    % draw nodes
    \path
          (0,5)  node (N05) [state] {$C$}
          (1,5)  node (N15) [state] {$O$}
          (1,4)  node (N14) [state] {$I$};
    % draw paths
    \grldh{N05}{N15}{$\alpha$}{$\beta$}{0};
    \grldh{N05}{N14}{$\gamma$}{$\delta$}{-45};
    \grldh{N15}{N14}{$\gamma$}{$\delta$}{-90};

    \end{tikzpicture}    
 \end{document}
share|improve this question

migrated from stackoverflow.com Feb 10 '13 at 12:42

This question came from our site for professional and enthusiast programmers.

    
Your question was migrated here from another stackexchange site. Please register on this site, too, and make sure that both accounts are associated with each other, otherwise you won't be able to comment on or accept answers or edit your question. –  percusse Feb 10 '13 at 13:12
1  
Also have you seen the tikz-cd package? –  percusse Feb 10 '13 at 13:12
add comment

1 Answer

up vote 6 down vote accepted

One would assume that you're dealing with real numbers that you want to compare. In that sense, a comparison of numbers is not appropriate, since they do not work with fractional components. Dimensions (or lengths) on the other hand do. You can trick a function to work with lengths rather than numbers in the following way:

enter image description here

\documentclass{article}
\makeatletter
\newcommand{\compare}[1]{%
  \ifdim\dimexpr#1pt+45pt>\z@
    above%
  \else 
    left%
  \fi
}
\makeatother
\begin{document}
$1$ is \compare{1}, % 1 + 45 = 46 > 0 -> above
while $-90$ is \compare{-90} % -90 + 45 = -45 < 0 -> left
and $-45$ is \compare{-45}. % -45 + 45 = 0 ... -> left
\end{document}

The function \compare{<num>} uses <num> in a "dimension" form as <num>pt, adds 45pt to that, checks if this is greater than \z@ (or 0pt) and conditions accordingly.

Note the use of % to avoid spurious spaces. See What is the use of percent signs (%) at the end of lines?

share|improve this answer
    
Thanks a lot, your example works perfectly. In my case, I intend to include the \compare command in definition of other command (please see the updated code attached), which does not work. Do you know what could be the problem? –  Tomas Feb 9 '13 at 10:02
    
@Tomas: Remove both \relax commands within \compare. Give some feedback once you've tested. –  Werner Feb 10 '13 at 0:34
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.