繰り返しコードには配列/リスト/シーケンスを使用しますか?

繰り返しコードには配列/リスト/シーケンスを使用しますか?

当大学の博士論文カバーテンプレートには、次のようなコードがあります。

%%% Jury member n1 (Président) %%%
\newcommand{\jurynameA}{M. Cabot}
\newcommand{\juryadressA}{Someplace}
\newcommand{\jurygradeA}{Somerank}
\newcommand{\juryroleA}{Président du jury}
%%% Jury member n2 (Rapporteur) %%%
\newcommand{\jurynameB}{M. Blonde}
\newcommand{\juryadressB}{Anotherplace}
\newcommand{\jurygradeB}{Anotherrank}
\newcommand{\juryroleB}{Rapporteur} % 
%%% Jury member n3 (Rapporteur) %%%
\newcommand{\jurynameC}{M. Blue}
\newcommand{\juryadressC}{Someotherplace}
\newcommand{\jurygradeC}{Someotherrank}
\newcommand{\juryroleC}{Rapporteur}
%%% Jury member n4 (Examinateur) %%%
\newcommand{\jurynameD}{M. Brown}
\newcommand{\juryadressD}{Yetanotherplace}
\newcommand{\jurygradeD}{Yetanotherrank}
\newcommand{\juryroleD}{Examinateur}
%%% Jury member n5 (Examinateur) %%%
\newcommand{\jurynameE}{M. Orange}
\newcommand{\juryadressE}{Onemoreplace}
\newcommand{\jurygradeE}{One more rank}
\newcommand{\juryroleE}{Examinateur}
%%% Jury member n6 (Directeur) %%%
\newcommand{\jurynameF}{M. Pink}
\newcommand{\juryadressF}{Yourplace}
\newcommand{\jurygradeF}{HDR}
\newcommand{\juryroleF}{Directeur de thèse}
%%% Jury member n7 (Co-Directeur) %%%
\newcommand{\jurynameG}{M. White}
\newcommand{\juryadressG}{Yourotherplace}
\newcommand{\jurygradeG}{HDR?}
\newcommand{\juryroleG}{Co-Directeur de thèse}
\begin{tabular}{lll}

    \textsc{\jurynameA}  & \jurygradeA & (\juryroleA) \\
    \null  & \textit{\juryadressA} & \\

    \textsc{\jurynameB}  & \jurygradeB & (\juryroleB) \\
    \null  & \textit{\juryadressB} & \\

    \textsc{\jurynameC}  & \jurygradeC & (\juryroleC) \\
    \null  & \textit{\juryadressC} & \\

    \textsc{\jurynameD}  & \jurygradeD & (\juryroleD) \\
    \null  & \textit{\juryadressD} & \\

    \textsc{\jurynameE}  & \jurygradeE & (\juryroleE) \\
    \null  & \textit{\juryadressE} & \\

    \textsc{\jurynameF}  & \jurygradeF & (\juryroleF) \\
    \null  & \textit{\juryadressF} & \\

    \textsc{\jurynameG}  & \jurygradeG & (\juryroleG) \\
    \null  & \textit{\juryadressG} & \\

\end{tabular}

もっと簡単にデータを入力して( のような\addmember{M. Pink}{Yourplace}{HDR}{Directeur de thèse})、陪審員の数を気にせずに(4 人の場合もあれば、8 人までの場合もあります)、テーブルを自動的に生成することはできないかと考えていました。

たとえば、Python では、dict のリスト (または、意味を気にしない場合は単なるリスト) を使用し、各 dict で陪審員を説明し、そのリストを反復処理してテーブルを作成します。LaTeX でも同様のことを行うことができますか?

答え1

etoolboxおよびの標準 LaTeX メソッドを使用してこれを行う 1 つの方法を次に示しますpgffor

\documentclass{article}
\usepackage{pgffor,etoolbox,array}
\newcounter{members}
\newcommand{\addmember}[4]{%
\ifnum\value{members}=8
    \typeout{Warning: your committee has too many members! Extra members ignored.}
\else
    \stepcounter{members}
    \csgdef{juryname\themembers}{#1}
    \csgdef{juryadress\themembers}{#2}
    \csgdef{jurygrade\themembers}{#3}
    \csgdef{juryrole\themembers}{#4}
\fi
}
\newcommand{\thecommitee}{}
\makeatletter
\newcommand{\makecommittee}{%
\foreach \x in {1,...,\value{members}}{
\protected@xappto{\thecommittee}{%
\csuse{juryname\x} &
\csuse{jurygrade\x} &
(\csuse{juryrole\x})\tabularnewline
&\itshape\csuse{juryadress\x} &\tabularnewline
}
}
\begin{tabular}{>{\scshape}lll}
\thecommittee
\end{tabular}
}
\makeatother
\begin{document}
\addmember{M. Cabot}{Montréal}{PhD}{Président du jury}
\addmember{M. Blonde}{Trois Rivières}{PhD}{Rapporteur}
\addmember{M. Brun}{Québec}{PhD}{Examinateur}
\addmember{M. Vert}{Paris}{PhD}{Examinateur}
\addmember{M. Gris}{Montréal}{PhD}{Directeur de thèse}
\makecommittee
\end{document}

コードの出力

関連情報