Consider the following:
\documentclass{article}
\usepackage{expl3}
\ExplSyntaxOn
\cs_new_eq:NN \calc \fp_eval:n
\ExplSyntaxOff
\def\valueA{7}
\def\valueB{19}
\def\valueC{41}
\def\valueD{31}
\def\valueE{25}
\def\valueF{17}
\def\valueG{7}
\def\valueH{3}
\begin{document}
\noindent I have the list
\[
\valueA,\valueB,\valueC,\valueD,\valueE,\valueF,\valueG,\valueH
\]
in which the largest element is $\calc{max(\valueA,\valueB,\valueC,\valueD,\valueE,\valueF,\valueG,\valueH)}$ (but that is of no importance to me).\\[\baselineskip]
How can I get \LaTeX{} to extract the position number of the largest element in a list? (In the example above, the answer is of course `$3$'.)
\end{document}
Note
It doesn't have to be a LaTeX 3 solution, but I need to compile the original document (i.e., the one I need the solution for) via latex
–> dvips
–> ps2pdf
.
Also, the list is generated via the \def
method.
Best Answer
Just using
expl3
we can do this using one loop or two. A one loop version needs to work out the maximum value and track the position at the same time:whereas a two loop version first finds the maximum then the position so there are fewer things to track in one go
The idea is first to find the largest entry using one mapping, then to find the position of this entry using a second mapping. Everything is expandable at the code level, so you could use
\DeclareExpandableDocumentCommand
here if you wanted.