TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

The etoolbox package documentation says about \deflength:

This command may be used as a drop-in replacement for \setlength.

I am having trouble getting it to work with the \minof macro from the calc package. The following MWE gives me a

"missing number, treated as zero" 

error.

\documentclass{article}
\usepackage{calc}
\usepackage{etoolbox}
\newlength{\foo}
\begin{document}
\deflength{\foo}{\minof{1in}{2in}}
\setlength{\foo}{\minof{1in}{2in}}
\end{document}
share|improve this question
4  
looks like it is a drop in replacement for the standard \setlength not the version from the calc package (which is quite different) – David Carlisle Jan 5 at 20:08
    
calc documentation: If \minof and \maxof are used with dimensions or glue `these macros are allowed only as part of addition and subtraction – Christian Hupfer Jan 5 at 20:08
    
@ChristianHupfer the example from the calc documentation does not work with \deflength. – StrongBad Jan 5 at 20:17
    
@DavidCarlisle I didn't realize (but probably should have) that calc redefined \setlength. – StrongBad Jan 5 at 20:17
4  
minof is (or could be) just \def\minof#1#2{\ifdim#1<#2#1\else#2\fi} you don't need 300 lines of scary calc macro definitions for that:-) – David Carlisle Jan 5 at 20:32
up vote 10 down vote accepted

The calc package does a lot of redefinitions of \setlength as well as \stepcounter etc. It won't work with addition etc. or with \deflength. Here's a \minof wrapper that works with \deflength since it uses the etoolbox macro \ifdimgreater.

\ifdimgreater and \ifdimless are wrappers fro \ifdimcomp.

Both macros work this way \ifdim...{first dim}{second dim}{true code}{false code}

Please note that \deflength{} can work with additions and subtractions of lengths in its argument.

\documentclass{article}
\usepackage{etoolbox}
%\usepackage{calc}

\newcommand{\etminof}[2]{%
  \ifdimgreater{#1}{#2}{%
    #2%
  }{%
    #1% Is smaller or equal
  }%
}


\newcommand{\etmaxof}[2]{%
  \ifdimgreater{#1}{#2}{%
    #1%
  }{%
    #2% Is greater or equal
  }%
}


\newlength{\foo}

\newlength{\foobar}

\begin{document}


\deflength{\foo}{\etminof{1in}{2in}}

\deflength{\foobar}{\etmaxof{5in}{10in}}

% Outputting the whole stuff
\the\foo

\the\foobar

\end{document}

enter image description here

Here's some edition with \ifdim and expl3 version as well

\documentclass{article}
\usepackage{etoolbox}

\usepackage{expl3}

\newcommand{\etminof}[2]{%
  \ifdimgreater{#1}{#2}{%
    #2%
  }{%
    #1% Is smaller or equal
  }%
}


\newcommand{\etmaxof}[2]{%
  \ifdimgreater{#1}{#2}{%
    #1%
  }{%
    #2% Is greater or equal
  }%
}

\newcommand{\texminof}[2]{%
  \ifdim#1<#2
  #1%
  \else
  #2%
  \fi
}

\ExplSyntaxOn
\newcommand{\explminof}[2]{%
  \dim_min:nn {#1} {#2}
% or
%  \dim_compare:nNnTF {#1} < {#2}
%  {#1} % True 
%  {#2} % False
}
\ExplSyntaxOff


\newlength{\foo}

\begin{document}

\texttt{etoolbox}
\deflength{\foo}{\etminof{5in}{10in}}

\the\foo

Now \LaTeXe

\deflength{\foo}{\texminof{5in}{10in}}

\the\foo



Now \texttt{expl3}

\deflength{\foo}{\explminof{5in}{10in}}


\the\foo



\end{document}
share|improve this answer
1  
There is \dim_min:nn – egreg Jan 5 at 20:51
    
@egreg: Thanks, I overlooked that in the manual – Christian Hupfer Jan 5 at 20:52

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.