6

"\ifdimless" can compare two real numbers. But, how can we feed the result of a calculation to this function ?

\documentclass[]{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{etoolbox} 

\begin{document}

% This comparison works well: small versus specified 
\newlength{\SmallLength}
\setlength{\SmallLength}{1.2 pt}
\newlength{\SpecifiedLength}
\setlength{\SpecifiedLength}{3.4 pt}
\ifdimless{\SpecifiedLength}{\SmallLength}{specified smaller}{specified higher}%

% Second is divided by First, and the result works well (\DividedNumbers)
\def\FirstNumber{2.3}
\def\SecondNumber{4.5}
\def\DividedNumbers{\pgfmathparse{div(\SecondNumber,\FirstNumber)}\pgfmathresult}
\DividedNumbers

%\Divided is taken from \DividedNumbers: doesn't work...
\newlength{\DividedLength}
%\setlength{\DividedLength}{\DividedNumbers pt}
%\ifdimless{\DividedLength}{\SmallLength}{divided smaller}{divided higher}%

\end{document}
  • Have you considered using the TiKZ math library? – cfr Apr 4 '15 at 3:44
  • Yes I had, and it didn't solve the problem. The Answer #2 addresses specifically my issue: "Store the result rather than the calculation" – SDrolet Apr 4 '15 at 15:25
  • The answer of @kpym uses the TIKZ math library, and works well too. – SDrolet Oct 10 '15 at 14:44
4

Store the result rather than the calculation:

specified higher
1
divided smaller

\documentclass[]{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{etoolbox} 

\begin{document}

% This comparison works well: small versus specified 
\newlength{\SmallLength}
\setlength{\SmallLength}{1.2 pt}
\newlength{\SpecifiedLength}
\setlength{\SpecifiedLength}{3.4 pt}
\ifdimless{\SpecifiedLength}{\SmallLength}{specified smaller}{specified higher}%

% Second is divided by First, and the result works well (\DividedNumbers)
\def\FirstNumber{2.3}
\def\SecondNumber{4.5}
\pgfmathparse{div(\SecondNumber,\FirstNumber)}
\edef\DividedNumbers{\pgfmathresult}% Store result in \DividedNumbers
\DividedNumbers

% \Divided is taken from \DividedNumbers:
\newlength{\DividedLength}
\setlength{\DividedLength}{\DividedNumbers pt}
\ifdimless{\DividedLength}{\SmallLength}{divided smaller}{divided higher}%

\end{document}
  • Or you can use \pgfmathsetlength{\DividedLength}{div(\SecondNumber,\FirstNumber)}. – Kpym Jul 18 '15 at 6:02
8

I suggest you a more powerful approach, where computations can be directly input. The interface3.pdf manual of expl3 will tell you the syntax for the “dimension expressions” in the arguments to \xifdimless and the “floating point expressions” in the argument to \eval.

\documentclass[]{article}
\usepackage{xparse}

\ExplSyntaxOn
\DeclareExpandableDocumentCommand{\xifdimless}{mmmm}
 {
  \dim_compare:nTF { #1 < #2 } { #3 } { #4 }
 }
\DeclareExpandableDocumentCommand{\eval}{m}
 {
  \fp_eval:n { #1 }
 }
\ExplSyntaxOff

\begin{document}

% This comparison works well: small versus specified 
\newlength{\SmallLength}
\setlength{\SmallLength}{1.2pt}
\newlength{\SpecifiedLength}
\setlength{\SpecifiedLength}{3.4pt}

% first test A
\xifdimless{\SpecifiedLength}{\SmallLength}{specified smaller}{specified higher}

% first test B
\xifdimless{1.2pt+2.2pt}{4pt-2.8pt}{specified smaller}{specified higher}

% second test
\xifdimless{\SmallLength}{\SpecifiedLength}{specified smaller}{specified higher}

% third test
\xifdimless{ \eval{4.5/2.3} pt }{\SmallLength}{divided smaller}{divided higher}

% fourth test
\xifdimless{ \eval{2.3/4.5} pt }{\SmallLength}{divided smaller}{divided higher}

\end{document}

enter image description here

  • Indeed, a much more powerful approach: wow ! Grazie ! – SDrolet Apr 4 '15 at 15:27
3

As suggested by @cfr in the comment you can use tikzmath to achieve this:

\documentclass[preview, border=7mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{math}

\begin{document}

\newlength{\SmallLength}
\setlength{\SmallLength}{1.2 pt}

\tikzmath{
  if 4.5/2.3 < \SmallLength  then {
    {divided smaller};
  } else {
    {divided higher or equal};
  };
}

\end{document}

enter image description here

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.