评论:以下问题有点类似于“列表中最大元素的位置“但我(不幸的是)不熟悉 LaTeX 3 编程,所以我不得不问。:(
代码:请考虑以下示例:
\documentclass{beamer}
\usepackage[danish]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{booktabs}
\usepackage{siunitx}
\begin{document}
\def\elevA{6}
\def\elevB{0}
\def\elevC{0}
\def\elevD{3}
\def\elevE{0}
\def\elevF{2}
\def\elevG{1}
\def\elevH{1}
\def\elevI{1}
\def\elevJ{5}
\def\elevK{0}
\def\elevL{3}
\def\elevM{7}
\def\elevN{3}
\def\elevO{1}
\def\elevP{1}
\def\elevQ{0}
\def\elevR{0}
\def\elevS{0}
\def\elevT{2}
\def\elevU{1}
\def\elevV{2}
\def\elevW{0}
\def\elevX{2}
\def\elevY{1}
\def\elevZ{1}
\def\elevAa{4}
\def\elevAb{6}
\def\elevAc{1}
\def\elevAd{1}
\def\elevAe{6}
\def\elevAf{2}
\def\elevAg{0}
\def\elevAh{3}
\def\elevAi{2}
\def\elevAj{1}
\def\elevAk{0}
\def\elevAl{0}
\def\elevAm{0}
\def\elevAn{0}
\def\elevAo{0}
\def\elevAp{4}
\begin{frame}
\begin{table}
\centering
\small
\begin{tabular}{*{14}{c}}
\toprule
\elevA & \elevB & \elevC & \elevD & \elevE & \elevF & \elevG &
\elevH & \elevI & \elevJ & \elevK & \elevL & \elevM & \elevN \\[0.5ex]
\elevO & \elevP & \elevQ & \elevR & \elevS & \elevT & \elevU &
\elevV & \elevW & \elevX & \elevY & \elevZ & \elevAa & \elevAb \\[0.5ex]
\elevAc & \elevAd & \elevAe & \elevAf & \elevAg & \elevAh & \elevAi &
\elevAj & \elevAk & \elevAl & \elevAm & \elevAn & \elevAo & \elevAp \\
\bottomrule
\end{tabular}
\end{table}
\end{frame}
\end{document}
输出:
问题:如何让 LaTeX 计算每个值 (这里是 0、1、...、7) 的条目数?我的意思是,列表中出现了多少次0
,列表中出现了多少次1
等等。
如果可以创建类似的宏\HowMany
,然后使用\HowMany{0}
返回值14
,那就太好了!
笔记:
- 它不一定是 LaTeX 3 解决方案,但我需要通过
latex
-->dvips
-->编译原始文档(即我需要解决方案的文档)ps2pdf
。 - 输入数字是通过
\def
方法进行的。 - 我可以自己找到最小值(这里是 0)和最大值(这里是 7),但如果 LaTeX 可以做到,那(当然)会更好。
答案1
一种更复杂的方法,展示了如何为宏提供更多结构可以减少问题。
我没有定义多个\elevXY
宏,而是只定义一个以字母为参数的宏。相应的值存储在属性列表中。
的标准行为\elev
是打印相应的数字(在此版本中它是完全可扩展的)。但是,在声明之后\countappearances
,除了打印数字之外,它还将更新另一个存储出现次数的属性列表。此属性列表初始化为为每个数字存储 0。声明是本地的,因此其效果将在table
(或frame
环境,如果table
未使用)结束后立即消失。
最后,\HowMany
访问与数字对应的属性。这些值以全局方式存储在计数属性列表中,因此在下一次\countappearances
声明之前它们都是可访问的。
\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}
答案2
\documentclass{beamer}
\usepackage[danish]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{booktabs}
\usepackage{siunitx}
\usepackage{array}
\def\zz\ignorespaces#1{%
\expandafter\xdef\csname zz#1\endcsname{%
\expandafter\ifx\csname zz#1\endcsname\relax
1%
\else
\the\numexpr\csname zz#1\endcsname+1\relax
\fi}%
#1}
\begin{document}
\def\elevA{6}
\def\elevB{0}
\def\elevC{0}
\def\elevD{3}
\def\elevE{0}
\def\elevF{2}
\def\elevG{1}
\def\elevH{1}
\def\elevI{1}
\def\elevJ{5}
\def\elevK{0}
\def\elevL{3}
\def\elevM{7}
\def\elevN{3}
\def\elevO{1}
\def\elevP{1}
\def\elevQ{0}
\def\elevR{0}
\def\elevS{0}
\def\elevT{2}
\def\elevU{1}
\def\elevV{2}
\def\elevW{0}
\def\elevX{2}
\def\elevY{1}
\def\elevZ{1}
\def\elevAa{4}
\def\elevAb{6}
\def\elevAc{1}
\def\elevAd{1}
\def\elevAe{6}
\def\elevAf{2}
\def\elevAg{0}
\def\elevAh{3}
\def\elevAi{2}
\def\elevAj{1}
\def\elevAk{0}
\def\elevAl{0}
\def\elevAm{0}
\def\elevAn{0}
\def\elevAo{0}
\def\elevAp{4}
\begin{frame}
\begin{table}
\centering
\small
\begin{tabular}{
*{14}{>\zz c}
}
\toprule
\elevA & \elevB & \elevC & \elevD & \elevE & \elevF & \elevG &
\elevH & \elevI & \elevJ & \elevK & \elevL & \elevM & \elevN \\[0.5ex]
\elevO & \elevP & \elevQ & \elevR & \elevS & \elevT & \elevU &
\elevV & \elevW & \elevX & \elevY & \elevZ & \elevAa & \elevAb \\[0.5ex]
\elevAc & \elevAd & \elevAe & \elevAf & \elevAg & \elevAh & \elevAi &
\elevAj & \elevAk & \elevAl & \elevAm & \elevAn & \elevAo & \elevAp \\
\bottomrule
\end{tabular}
\end{table}
{\count0=0
\loop
\the\count0:\csname zz\the\count0\endcsname\endgraf
\ifnum\count0<8
\advance\count0 1
\repeat
}
\end{frame}
\end{document}