0

I am defining a \newcommand called \eventsperiodslist which call some other commands: \event[]{Digits}{} and \period[]{Digits}{Digits}{}

The arguments called "Digits" in \event and \period contains digits that I am trying to extract to find the minimum and maximum values of all numbers within \eventsperiodslist.

I do not know where to start:

  • With a \foreach, I would need to know how many \event and \period are in \eventsperiodslist which I don't know.
  • With functions from the xstring package, I have problems with the curly brackets.

Any suggestions?

EDIT @egreg's comment From the values in the MWE below, I expect to be able to call \findminmax{\eventsperiodslist} with the expected output being something like \newcommand{\myminvalue}{-6} and \newcommand{\mymaxvalue}{23}.

EDIT: I do not mind defining my events and periods from an array and later build the commands \event and \period from this array.

MWE (at least of the \eventsperiodslist command)

\documentclass{article}

\begin{document}

\newcommand{\event}[3][non-dated]{*Some command*}

\newcommand{\period}[4][right]{*Some other command*}

\newcommand{\findminmax}[1]{
    *Some code to identify the minimum and maximum values*
    \newcommand{\myminvalue}{*calculated minimum value*}% Here: minimum is -6
    \newcommand{\mymaxvalue}{*calculated maximum value*}% Here: maximum is 23
}

\newcommand{\eventsperiodslist}{
    \event[dated]{-6}{Some text}
    \period{1}{5}{Nothing}
    \period[center]{2}{7}{Something else}
    \period[left]{4}{10}{Random text}
    \event[dated]{23}{Making a test}

\findminmax{\eventsperiodslist}

\end{document}
}

AlMa
  • 560
  • Can you please add an example call with the expected output? – egreg Nov 16 '21 at 11:19
  • What are your data? Usually this type of problem is best solved by a scripting language like Perl, PHP, Python etc. (preprocessing), or even a database. Latex wouldn't be my best choice, unless I miss something important about your situation. – MS-SPO Nov 16 '21 at 11:22
  • 1
    @MS-SPO I know how to get the result I want with Perl but since this is to be integrated in a larger LaTeX document, I was trying to find a LaTeX only solution. – AlMa Nov 16 '21 at 18:42
  • @egreg The requested info has been added. – AlMa Nov 16 '21 at 18:47
  • The sagetex package gives you access to Python and a CAS, so you can easily integrate your data into a LaTeX document. See, for example, my answer here where the min, max, median, etc of data is calculated and put into the LaTeX document. – DJP Nov 16 '21 at 23:10

1 Answers1

1

With the input as you show it you could simply redefine \event and \period and execute the contents of \eventsperiodslist inside a box. This way any possible output is only typeset into a box (so never shows up), and by the redefinition we can just compare the correct argument to the current minimum and maximum.

Limitations: If your list contains input that might have a global effect this will not be safe.

\documentclass{article}

\newcommand{\event}[3][non-dated]{Some command}

\newcommand{\period}[4][right]{Some other command}

\makeatletter \newcount\AlMa@min \newcount\AlMa@max \newcommand\AlMa@period[4][] {% \AlMa@maxmin{#2}% \AlMa@maxmin{#3}% } \newcommand\AlMa@event[3][]{\AlMa@maxmin{#2}} \newcommand\AlMa@maxmin[1] {% \ifnum\numexpr#1\relax>\AlMa@max \global\AlMa@max=\numexpr#1\relax \fi \ifnum\numexpr#1\relax<\AlMa@min \global\AlMa@min=\numexpr#1\relax \fi } \newcommand\findminmax[3] {% \begingroup \global\AlMa@max=-\number\maxdimen \global\AlMa@min=\number\maxdimen \let\period\AlMa@period \let\event\AlMa@event \sbox0{#1}% \edef\AlMa@tmp {% \endgroup \def\unexpanded{#2}{\the\AlMa@min}% \def\unexpanded{#3}{\the\AlMa@max}% }% \AlMa@tmp } \makeatother

\newcommand{\eventsperiodslist} {% \event[dated]{-6}{Some text}% \period{1}{5}{Nothing}% \period[center]{2}{7}{Something else}% \period[left]{4}{10}{Random text}% \event[dated]{23}{Making a test}% }

% just allocating macro names \newcommand\mymin{0} \newcommand\mymax{0}

\findminmax{\eventsperiodslist}\mymin\mymax

\begin{document} Minimum was \mymin, and maximum was \mymax. \end{document}

enter image description here

Skillmon
  • 60,462
  • It works nicely. Would it be possible to define (or redefine the content of \eventsperiodslist within the document? – AlMa Nov 16 '21 at 20:36
  • 1
    @AlMa sure, but with a changed definition you'd have to run \findminmax again if you need the new values. – Skillmon Nov 17 '21 at 09:11
  • It would be fine for me to run \findminmax again. It would make things easier to define \eventsperiodslist within the document for my complete code. – AlMa Nov 17 '21 at 18:48
  • @AlMa might be, and it really doesn't matter where you define it. Just for this MWE it seems cleaner to do this in the preamble. TeX doesn't really differentiate between the preamble and the document body, that's just a LaTeX concept to make things simpler/more clear. – Skillmon Nov 18 '21 at 16:10
  • Okay, I will accept this answer as it makes sense in the MWE. In my complete code however, I create several lists of events and periods (with different command names) and they will not be defined in the preamble. – AlMa Nov 18 '21 at 18:18
  • 1
    @AlMa the solution (the macro \findminmax) will work no matter when the list was defined, the only thing that matters is that it is defined when you use \findminmax. This will work after \begin{document} without any issues. Nowhere does my answer implicate that it has to be used in the preamble or that the list has to be defined in the preamble. – Skillmon Nov 18 '21 at 18:27
  • Sorry for the misunderstanding – AlMa Nov 18 '21 at 18:48