A more complicated approach, showing how giving more structure to your macros can give less problems.
Instead of several \elevXY
macros, I define just one that takes the letters as argument. The corresponding values are stored in a property list.
The standard behavior of \elev
is to print the corresponding number (in this incarnation it is fully expandable). However, after the \countappearances
declaration, besides printing the number it will update another property list storing the number of appearances until then. This property list is initialized to store 0 for each number. The declaration is local, so its effects will vanish as soon as the table
(or frame
environment, if table
is not used) ends.
Finally, \HowMany
accesses the property corresponding to the number. The values are stored in the counting property list in a global fashion, so they'll be accessible until the next \countappearances
declaration.
\documentclass{beamer}
\usepackage[danish]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{booktabs}
\usepackage{siunitx}
\usepackage{xparse}
\ExplSyntaxOn
\prop_new:N \g_svend_elev_prop
\prop_gput:Nnn \g_svend_elev_prop {A} {6}
\prop_gput:Nnn \g_svend_elev_prop {B} {0}
\prop_gput:Nnn \g_svend_elev_prop {C} {0}
\prop_gput:Nnn \g_svend_elev_prop {D} {3}
\prop_gput:Nnn \g_svend_elev_prop {E} {0}
\prop_gput:Nnn \g_svend_elev_prop {F} {2}
\prop_gput:Nnn \g_svend_elev_prop {G} {1}
\prop_gput:Nnn \g_svend_elev_prop {H} {1}
\prop_gput:Nnn \g_svend_elev_prop {I} {1}
\prop_gput:Nnn \g_svend_elev_prop {J} {5}
\prop_gput:Nnn \g_svend_elev_prop {K} {0}
\prop_gput:Nnn \g_svend_elev_prop {L} {3}
\prop_gput:Nnn \g_svend_elev_prop {M} {7}
\prop_gput:Nnn \g_svend_elev_prop {N} {3}
\prop_gput:Nnn \g_svend_elev_prop {O} {1}
\prop_gput:Nnn \g_svend_elev_prop {P} {1}
\prop_gput:Nnn \g_svend_elev_prop {Q} {0}
\prop_gput:Nnn \g_svend_elev_prop {R} {0}
\prop_gput:Nnn \g_svend_elev_prop {S} {0}
\prop_gput:Nnn \g_svend_elev_prop {T} {2}
\prop_gput:Nnn \g_svend_elev_prop {U} {1}
\prop_gput:Nnn \g_svend_elev_prop {V} {2}
\prop_gput:Nnn \g_svend_elev_prop {W} {0}
\prop_gput:Nnn \g_svend_elev_prop {X} {2}
\prop_gput:Nnn \g_svend_elev_prop {Y} {1}
\prop_gput:Nnn \g_svend_elev_prop {Z} {1}
\prop_gput:Nnn \g_svend_elev_prop {Aa} {4}
\prop_gput:Nnn \g_svend_elev_prop {Ab} {6}
\prop_gput:Nnn \g_svend_elev_prop {Ac} {1}
\prop_gput:Nnn \g_svend_elev_prop {Ad} {1}
\prop_gput:Nnn \g_svend_elev_prop {Ae} {6}
\prop_gput:Nnn \g_svend_elev_prop {Af} {2}
\prop_gput:Nnn \g_svend_elev_prop {Ag} {0}
\prop_gput:Nnn \g_svend_elev_prop {Ah} {3}
\prop_gput:Nnn \g_svend_elev_prop {Ai} {2}
\prop_gput:Nnn \g_svend_elev_prop {Aj} {1}
\prop_gput:Nnn \g_svend_elev_prop {Ak} {0}
\prop_gput:Nnn \g_svend_elev_prop {Al} {0}
\prop_gput:Nnn \g_svend_elev_prop {Am} {0}
\prop_gput:Nnn \g_svend_elev_prop {An} {0}
\prop_gput:Nnn \g_svend_elev_prop {Ao} {0}
\prop_gput:Nnn \g_svend_elev_prop {Ap} {4}
\prop_new:N \g_svend_count_prop
\prop_new:N \g_svend_count_zero_prop
\prop_gput:Nnn \g_svend_count_zero_prop { 0 } { 0 }
\prop_gput:Nnn \g_svend_count_zero_prop { 1 } { 0 }
\prop_gput:Nnn \g_svend_count_zero_prop { 2 } { 0 }
\prop_gput:Nnn \g_svend_count_zero_prop { 3 } { 0 }
\prop_gput:Nnn \g_svend_count_zero_prop { 4 } { 0 }
\prop_gput:Nnn \g_svend_count_zero_prop { 5 } { 0 }
\prop_gput:Nnn \g_svend_count_zero_prop { 6 } { 0 }
\prop_gput:Nnn \g_svend_count_zero_prop { 7 } { 0 }
\prop_gset_eq:NN \g_svend_count_prop \g_svend_count_zero_prop
\tl_new:N \l_svend_number_tl
\tl_new:N \l_svend_count_tl
\DeclareExpandableDocumentCommand{\elev}{m}
{
\svend_get_item:n { #1 }
}
\NewDocumentCommand{\countappearances}{}
{
% now \elev will also count
\cs_set_eq:NN \elev \svend_get_item_count:n
% reinitialize the counter property list
\prop_set_eq:NN \g_svend_count_prop \g_svend_count_zero_prop
}
\DeclareExpandableDocumentCommand{\HowMany}{m}
{
\prop_item:Nn \g_svend_count_prop { #1 }
}
\cs_new:Npn \svend_get_item:n #1
{
\prop_item:Nn \g_svend_elev_prop { #1 }
}
\cs_new_protected:Npn \svend_get_item_count:n #1
{
\tl_set:Nx \l_svend_number_tl { \svend_get_item:n { #1 } }
% print the entry
\tl_use:N \l_svend_number_tl
% get the current count
\tl_set:Nx \l_svend_count_tl
{
\prop_item:NV \g_svend_count_prop \l_svend_number_tl
}
% advance the count by 1
\tl_set:Nx \l_svend_count_tl { \int_to_arabic:n { \l_svend_count_tl + 1 } }
% update the property
\prop_gput:NVV \g_svend_count_prop \l_svend_number_tl \l_svend_count_tl
}
\cs_generate_variant:Nn \prop_item:Nn { NV }
\cs_generate_variant:Nn \prop_gput:Nnn { NVV }
\ExplSyntaxOff
\begin{document}
\begin{frame}
\begin{table}
\centering
\small
\countappearances
\begin{tabular}{*{14}{c}}
\toprule
\elev{A} & \elev{B} & \elev{C} & \elev{D} & \elev{E} & \elev{F} & \elev{G} &
\elev{H} & \elev{I} & \elev{J} & \elev{K} & \elev{L} & \elev{M} & \elev{N} \\[0.5ex]
\elev{O} & \elev{P} & \elev{Q} & \elev{R} & \elev{S} & \elev{T} & \elev{U} &
\elev{V} & \elev{W} & \elev{X} & \elev{Y} & \elev{Z} & \elev{Aa} & \elev{Ab} \\[0.5ex]
\elev{Ac} & \elev{Ad} & \elev{Ae} & \elev{Af} & \elev{Ag} & \elev{Ah} & \elev{Ai} &
\elev{Aj} & \elev{Ak} & \elev{Al} & \elev{Am} & \elev{An} & \elev{Ao} & \elev{Ap} \\
\bottomrule
\end{tabular}
\bigskip
\begin{tabular}{*{8}{c}}
\toprule
\multicolumn{8}{c}{How many} \\
\midrule
0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 \\
\HowMany{0} &
\HowMany{1} &
\HowMany{2} &
\HowMany{3} &
\HowMany{4} &
\HowMany{5} &
\HowMany{6} &
\HowMany{7} \\
\bottomrule
\end{tabular}
\end{table}
\end{frame}
\end{document}

\def
... macros? How about a 'real' list? And are the values integers only? – user31729 Mar 15 '15 at 11:42