是否有任何包或宏可以实现从给定的一行数字中获取最小的数字?使用 ifthen 包。
我没能得到它。这是我的 MWE。
\documentclass[a4paper]{article}
\usepackage{datatool}
\usepackage{ifthen,fp}
\begin{filecontents*}{data.csv}
Name, EL-1, EL-2, EL-3, EL-4
Biki, 60, 40, 50, 80
\end{filecontents*}
\DTLloaddb{data}{data.csv}
\begin{document}
\DTLforeach{data}{\a=EL-1,\b=EL-2,\c=EL-3,\d=EL-4}{
\ifthenelse{\a<\b}{
\ifthenelse{\b<\c}{
\ifthenelse{\c<\d}{
The smallest number is \a
}{}
}{}
}{}
\ifthenelse{\b<\a}{
\ifthenelse{\a<\c}{
\ifthenelse{\c<\d}{
The smallest number is \b
}{}
}{}
}{}
\ifthenelse{\c<\a}{
\ifthenelse{\a<\b}{
\ifthenelse{\b<\d}{
The smallest number is \c
}{}
}{}
}{}
\ifthenelse{\d<\a}{
\ifthenelse{\a<\b}{
\ifthenelse{\b<\c}{
The smallest number is \d
}{}
}{}
}{}
}
\end{document}
答案1
可能像这样吗?
\begin{filecontents*}{\jobname.csv}
Name, EL-1, EL-2, EL-3, EL-4
Biki, 60, 40, 50, 80
Don Giovanni, 640, 231, 100, 91
\end{filecontents*}
\documentclass[a4paper]{article}
\usepackage{datatool}
\usepackage{xparse,xfp}
\NewExpandableDocumentCommand{\minimum}{m}{%
\fpeval{min(#1)}%
}
\DTLloaddb{data}{\jobname.csv}
\begin{document}
\DTLforeach{data}{\a=EL-1,\b=EL-2,\c=EL-3,\d=EL-4,\n=Name}{%
The smallest number for \n\ is \minimum{\a,\b,\c,\d}\par
}
\end{document}
具有标准整数比较的(不可扩展)版本:
\begin{filecontents*}{\jobname.csv}
Name, EL-1, EL-2, EL-3, EL-4
Biki, 60, 40, 50, 80
Don Giovanni, 640, 231, 100, 91
\end{filecontents*}
\documentclass[a4paper]{article}
\usepackage{datatool}
\makeatletter
\newcommand{\minimum}[1]{%
\def\biki@found{\maxdimen}%
\@for\next:=#1\do{%
\biki@minimum{\biki@found}{\next}%
}
\biki@found
}
\newcommand{\biki@minimum}[2]{%
\ifnum#2<#1\relax
\edef\biki@found{#2}%
\fi
}
\makeatother
\DTLloaddb{data}{\jobname.csv}
\begin{document}
\DTLforeach{data}{\a=EL-1,\b=EL-2,\c=EL-3,\d=EL-4,\n=Name}{%
The smallest number for \n\ is \minimum{\a,\b,\c,\d}\par
}
\end{document}
递归首先将最大整数\maxdimen
与列表中的第一个项目进行比较;在每一步中,如果列表中的下一个整数小于存储的整数,则该项目存储在中\biki@found
。最后,\biki@found
交付。
答案2
以下代码将打印一行中最小的数字:
\documentclass[a4paper]{article}
\usepackage{datatool,expl3}
\begin{filecontents*}{data.csv}
Name, EL-1, EL-2, EL-3, EL-4
Biki, 60, 40, 50, 80
\end{filecontents*}
\DTLloaddb{data}{data.csv}
\begin{document}
\ExplSyntaxOn
\DTLforeach{data}{\a=EL-1,\b=EL-2,\c=EL-3,\d=EL-4}{
\clist_set:Nx \l_tmpa_clist { \a, \b, \c, \d }
\clist_sort:Nn \l_tmpa_clist
{
\int_compare:nNnTF { #1 } > { #2 }
{ \sort_return_swapped: }
{ \sort_return_same: }
}
\clist_item:Nn \l_tmpa_clist { 1 }
}
\ExplSyntaxOff
\end{document}