1

I found this code in datatool group by?

\documentclass{article}

\usepackage{filecontents}
\begin{filecontents*}{ff14.txt}
ClassID; Schueler_Name; Schueler_Vorname; Subject
5a; Stiner; Markus; Big Band
3c; Meier; Stefan; Latein
5b; Muggli; Elena; Instrument
3b; Strang; Flurin; Band
3b; Nomer; Ulrike; Big Band
\end{filecontents*}

\usepackage{datatool, etoolbox}

\DTLsetseparator{;} 
\DTLloaddb{anmeldungen}{ff14.txt}

\begin{document}

% Taken from https://tex.stackexchange.com/q/108712/5764
\newcommand*{\uniqueclass}{}
\newcommand{\csvlistsep}{\renewcommand{\csvlistsep}{,}}
\DTLforeach*{anmeldungen}{\Class=ClassID}{%
  \expandafter\DTLifinlist\expandafter{\Class}{\uniqueclass}%
  {}% do nothing, already in list
  {% add to list
    \ifdefempty{\uniqueclass}%
    {\let\uniqueclass\Class}% first element of list
    {% append to list
      \eappto\uniqueclass{,\Class}%
    }%
  }%
}

\newcommand{\nextrow}{}
\renewcommand{\do}[1]{%
  \subsection*{Klasse #1}
  \renewcommand{\nextrow}{}
  \begin{tabular}{ll}
    \hline
    \textbf{Name} & \textbf{Vorname} \\ \hline
    \DTLforeach{anmeldungen}
      {\class=ClassID, \fn=Schueler_Vorname, \ln=Schueler_Name}
      {\ifnum\pdfstrcmp{#1}{\class}=0 
         \eappto\nextrow{\ln \noexpand& \fn \noexpand\\}% Collect rows
       \fi}%
      \nextrow % Print table rows
    \hline \hline
  \end{tabular}
}

\expandafter\docsvlist\expandafter{\uniqueclass}

\end{document}

I'm now struggling to create a "dynamic command" for this.

What I want would be a command like this:

\dyntemplate{databasename}{keyname}{
    \begin{tabular}{ll}
        Groupheader & Groupheader2...
        \DTLforeach*{currentgroup}{\propone=PropOne,\proptwo=PropTwo}{
            \propone & \proptwo
        }
    \end{tabular}
}

so I could ultimately also use it for item lists like so:

\dyntemplate{databasename}{keyname}{
    \subsection{Groupname}
    \begin{itemize}
        \DTLforeach*{currentgroup}{\propone=PropOne,\proptwo=PropTwo}{
            \item \propone, \proptwo
        }
    \end{itemize}
}

where databasename is the name of the database I define earlier and keyname is the key to group by.

I'm pretty new to all that macro stuff, I don't even get the static code from above a 100% so I'm struggling creating my own macro for this. Haven't found any solutions, though it seems it would be something needed quite often, no?

  • Command names can't include digits, so \prop1 is actually \prop followed by 1. Try \propi and \propii instead. – Nicola Talbot Apr 14 '18 at 21:32
  • Well, yes, just wanted to give an example. I edited my post for clarification, thanks. – Kevin Suppan Apr 15 '18 at 0:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.