# Understanding etoolbox macro \deflength

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}
• 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 '16 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 – user31729 Jan 5 '16 at 20:08
• @ChristianHupfer the example from the calc documentation does not work with \deflength. – StrongBad Jan 5 '16 at 20:17
• @DavidCarlisle I didn't realize (but probably should have) that calc redefined \setlength. – StrongBad Jan 5 '16 at 20:17
• 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 '16 at 20:32

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}

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}
• There is \dim_min:nn – egreg Jan 5 '16 at 20:51
• @egreg: Thanks, I overlooked that in the manual – user31729 Jan 5 '16 at 20:52