10

Assume I have a list with quite a few entries (all integer values, in case it's relevant), e.g.,

1 2 4 0 3 1 0 0 2 1 7 1 4 2 3 0 0 6 5 2 1 2 3 0

Can LaTeX somehow count the number of 0's, 1's, ..., 7's in the list above?

P.S. All solutions are acceptable but a LaTeX 3 version is preferred.

Update

Using egreg's code, why are the following returning only 0's and how can I (if possible) still use all the \defs and get the correct result?

\documentclass{article}

\usepackage{
  xparse,
  l3regex
}

\ExplSyntaxOn
\NewDocumentCommand{\countoccurrences}{ O{} m }
 {
  \group_begin:
  \keys_set:nn { svend/occurrences } { #1 }
  % if the self key appeared, make the list name equal to the sequence name
  \bool_if:NT \l_svend_occurrences_self_bool
   {
    \tl_set_eq:NN \l__svend_occurrences_list_tl \l__svend_occurrences_name_tl
   }
  \svend_count_occurrences:n { #2 }
  \group_end:
 }
\NewDocumentCommand{\setsearchlist}{ O{default} m }
 {
  \svend_occurrences_set_searchlist:nn { #1 } { #2 }
 }
\DeclareExpandableDocumentCommand{\getoccurrences}{ O{default} m }
 {
  \prop_item:cn { g_svend_occurrences_#1_prop } { #2 }
 }

\keys_define:nn { svend/occurrences }
 {
  name .tl_set:N   = \l__svend_occurrences_name_tl,
  name .initial:n  = default,
  list .tl_set:N   = \l__svend_occurrences_list_tl,
  list .initial:n  = default,
  self .bool_set:N = \l_svend_occurrences_self_bool,
  self .default:n  = true,
  self .initial:n  = false,
 }

\prop_new:N \g_svend_occurrences_default_prop
\seq_new:N \g_svend_occurrences_default_list_seq
\int_new:N \l__svend_occurrences_matches_int

\cs_new_protected:Npn \svend_count_occurrences:n #1
 {
  % create a new property list for storing the values for the current string
  % or clear an existing one
  \prop_gclear_new:c { g_svend_occurrences_ \l__svend_occurrences_name_tl _prop }
  % populate the search list, if self is used
  \bool_if:NT \l_svend_occurrences_self_bool
   {
    \svend_occurrences_set_searchlist:Vn \l__svend_occurrences_name_tl { #1 }
   }
  % map through the search list
  \seq_map_inline:cn { g_svend_occurrences_ \l__svend_occurrences_list_tl _list_seq }
   {
    % store the current search item; using the token list in
    % the regex search expression should avoid problems with
    % special characters
    \tl_set:Nn \l__svend_occurrences_temp_tl { ##1 }
    % count the number of times “search item/space” appears
    % (a trailing space is added) and store it in an integer variable
    % (in a regex, \u{tl variable name} stands for the contents of the variable
    \regex_count:nnN { \u{l__svend_occurrences_temp_tl}\s } { #1~ } \l__svend_occurrences_matches_int
    % put the number of matches in the property list, corresponding to
    % the current item as key
    \prop_gput:cnx { g_svend_occurrences_ \l__svend_occurrences_name_tl _prop }
     { ##1 }
     { \int_to_arabic:n { \l__svend_occurrences_matches_int } }
   }
 }

% populate a search list (with duplicates removed)
\cs_new_protected:Npn \svend_occurrences_set_searchlist:nn #1 #2
 {
  \seq_gclear_new:c { g_svend_occurrences_#1_list_seq }
  \seq_gset_split:cnn { g_svend_occurrences_#1_list_seq } { ~ } { #2 }
  \seq_gremove_duplicates:c { g_svend_occurrences_#1_list_seq }
 }
\cs_generate_variant:Nn \seq_gset_split:Nnn { c }
\cs_generate_variant:Nn \svend_occurrences_set_searchlist:nn { V }

\ExplSyntaxOff

\begin{document}

\def\fravaerElevEn{1}
\def\fravaerElevTo{2}
\def\fravaerElevTre{4}
\def\fravaerElevFire{0}
\def\fravaerElevFem{3}
\def\fravaerElevSeks{1}
\def\fravaerElevSyv{0}
\def\fravaerElevOtte{0}
\def\fravaerElevNi{2}
\def\fravaerElevTi{1}
\def\fravaerElevElleve{7}
\def\fravaerElevTolv{1}
\def\fravaerElevTretten{4}
\def\fravaerElevFjorten{2}
\def\fravaerElevFemten{3}
\def\fravaerElevSeksten{0}
\def\fravaerElevSytten{0}
\def\fravaerElevAtten{6}
\def\fravaerElevNitten{5}
\def\fravaerElevTyve{2}
\def\fravaerElevEnogtyve{1}
\def\fravaerElevToogtyve{2}
\def\fravaerElevTreogtyve{3}
\def\fravaerElevFireogtyve{0}

\setsearchlist{0 1 2 3 4 5 6 7}

\countoccurrences{
  \fravaerElevEn
  \fravaerElevTo
  \fravaerElevTre
  \fravaerElevFire
  \fravaerElevFem
  \fravaerElevSeks
  \fravaerElevSyv
  \fravaerElevOtte
  \fravaerElevNi
  \fravaerElevTi
  \fravaerElevElleve
  \fravaerElevTolv
  \fravaerElevTretten
  \fravaerElevFjorten
  \fravaerElevFemten
  \fravaerElevSeksten
  \fravaerElevSytten
  \fravaerElevAtten
  \fravaerElevNitten
  \fravaerElevTyve
  \fravaerElevEnogtyve
  \fravaerElevToogtyve
  \fravaerElevTreogtyve
  \fravaerElevFireogtyve
}

\begin{tabular}{cc}
  0 & \getoccurrences{0} \\
  1 & \getoccurrences{1} \\
  2 & \getoccurrences{2} \\
  3 & \getoccurrences{3} \\
  4 & \getoccurrences{4} \\
  5 & \getoccurrences{5} \\
  6 & \getoccurrences{6} \\
  7 & \getoccurrences{7}
\end{tabular}

\end{document}

output

3
  • 1
    You didn't specify you wanted to allow macros in the argument. – egreg Dec 15 '14 at 22:34
  • @egreg Sorry; apologies for being unclear at first. (I didn't think it made a difference, which was why I didn't mention it.) Can I somehow alter the input (still with the use of \defs) to make the code work or can I make you give a solution that allows me to use \defs in the way I've shown? – Svend Tveskæg Dec 15 '14 at 23:06
  • 1
    Already done. ;-) – egreg Dec 15 '14 at 23:18
9

This exploits the fact that the tokens to count are digits:

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\countoccurrences}{ O{default} m }
 {
  \svend_count_occurrences:nn { #1 } { #2 }
 }
\DeclareExpandableDocumentCommand{\getoccurrences}{ O{default} m }
 {
  % just retrieve the property corresponding to the requested digit
  \prop_item:cn { g_svend_occurrences_#1_prop } { #2 }
 }

\prop_new:N \g_svend_occurrences_default_prop
\tl_new:N \l__svend_input_tl
\tl_new:N \l__svend_temp_tl

\cs_new_protected:Npn \svend_count_occurrences:nn #1 #2
 {
  % make a new property list, if necessary, or clear an existing one
  \prop_gclear_new:c { g_svend_occurrences_#1_prop }
  % store the string
  \tl_set:Nn \l__svend_input_tl { #2 }
  % the items are octal digits so do a loop on them
  \int_step_inline:nnnn { 0 } { 1 } { 7 }
   {
    % make a copy of the given string
    \tl_set_eq:NN \l__svend_temp_tl \l__svend_input_tl
    % remove all items that are the same as the current digit
    \tl_remove_all:Nn \l__svend_temp_tl { ##1 }
    % put in the property list the current digit as property
    % and the difference between the number of items of the
    % full list and the temporary one (count the number of removed items)
    \prop_gput:cnx { g_svend_occurrences_#1_prop }
     { ##1 }
     { \int_eval:n { \tl_count:N \l__svend_input_tl - \tl_count:N \l__svend_temp_tl } }
   }
 }
\ExplSyntaxOff

\begin{document}

\countoccurrences[new]{1 1 1 1 2 2 2 2}
\getoccurrences[new]{0}\quad
\getoccurrences[new]{1}\quad
\getoccurrences[new]{2}

\bigskip

\countoccurrences{1 2 4 0 3 1 0 0 2 1 7 1 4 2 3 0 0 6 5 2 1 2 3 0}

\begin{tabular}{cc}
  0 & \getoccurrences{0} \\
  1 & \getoccurrences{1} \\
  2 & \getoccurrences{2} \\
  3 & \getoccurrences{3} \\
  4 & \getoccurrences{4} \\
  5 & \getoccurrences{5} \\
  6 & \getoccurrences{6} \\
  7 & \getoccurrences{7} \\
\end{tabular}

\end{document}

For arbitrary items more complex code is needed.

enter image description here


Arbitrary sequences of characters can be coped with regular expressions. Note that here \countoccurrences takes as optional argument a key-value pair list: name sets a name for the list (default is default) and list the list of sequences to count. In \setsearchlist only the list should be specified as optional argument and in \getoccurrences the name.

If the optional argument to \countoccurrences contains self (that implies self=true), then list should not appear and the list of items will be constructed from the mandatory argument and given the same name as the token list. In particular

\countoccurrences[self]{0 1 2 3}

would set up the list in the same way as issuing

\setsearchlist{0 1 2 3}

beforehand.

Complete example:

\documentclass{article}
\usepackage{xparse,l3regex}

\ExplSyntaxOn
\NewDocumentCommand{\countoccurrences}{ O{} m }
 {
  \group_begin:
  \keys_set:nn { svend/occurrences } { #1 }
  % if the self key appeared, make the list name equal to the sequence name
  \bool_if:NT \l_svend_occurrences_self_bool
   {
    \tl_set_eq:NN \l__svend_occurrences_list_tl \l__svend_occurrences_name_tl
   }
  \svend_count_occurrences:n { #2 }
  \group_end:
 }
\NewDocumentCommand{\setsearchlist}{ O{default} m }
 {
  \svend_occurrences_set_searchlist:nn { #1 } { #2 }
 }
\DeclareExpandableDocumentCommand{\getoccurrences}{ O{default} m }
 {
  \prop_item:cn { g_svend_occurrences_#1_prop } { #2 }
 }

\keys_define:nn { svend/occurrences }
 {
  name .tl_set:N   = \l__svend_occurrences_name_tl,
  name .initial:n  = default,
  list .tl_set:N   = \l__svend_occurrences_list_tl,
  list .initial:n  = default,
  self .bool_set:N = \l_svend_occurrences_self_bool,
  self .default:n  = true,
  self .initial:n  = false,
 }

\prop_new:N \g_svend_occurrences_default_prop
\seq_new:N \g_svend_occurrences_default_list_seq
\int_new:N \l__svend_occurrences_matches_int

\cs_new_protected:Npn \svend_count_occurrences:n #1
 {
  % create a new property list for storing the values for the current string
  % or clear an existing one
  \prop_gclear_new:c { g_svend_occurrences_ \l__svend_occurrences_name_tl _prop }
  % populate the search list, if self is used
  \bool_if:NT \l_svend_occurrences_self_bool
   {
    \svend_occurrences_set_searchlist:Vn \l__svend_occurrences_name_tl { #1 }
   }
  % map through the search list
  \seq_map_inline:cn { g_svend_occurrences_ \l__svend_occurrences_list_tl _list_seq }
   {
    % store the current search item; using the token list in
    % the regex search expression should avoid problems with
    % special characters
    \tl_set:Nn \l__svend_occurrences_temp_tl { ##1 }
    % count the number of times “search item/space” appears
    % (a trailing space is added) and store it in an integer variable
    % (in a regex, \u{tl variable name} stands for the contents of the variable
    \regex_count:nnN { \u{l__svend_occurrences_temp_tl}\s } { #1~ } \l__svend_occurrences_matches_int
    % put the number of matches in the property list, corresponding to
    % the current item as key
    \prop_gput:cnx { g_svend_occurrences_ \l__svend_occurrences_name_tl _prop }
     { ##1 }
     { \int_to_arabic:n { \l__svend_occurrences_matches_int } }
   }
 }

% populate a search list (with duplicates removed)
\cs_new_protected:Npn \svend_occurrences_set_searchlist:nn #1 #2
 {
  \seq_gclear_new:c { g_svend_occurrences_#1_list_seq }
  \seq_gset_split:cnn { g_svend_occurrences_#1_list_seq } { ~ } { #2 }
  \seq_gremove_duplicates:c { g_svend_occurrences_#1_list_seq }
 }
\cs_generate_variant:Nn \seq_gset_split:Nnn { c }
\cs_generate_variant:Nn \svend_occurrences_set_searchlist:nn { V }

\ExplSyntaxOff

\begin{document}

\setsearchlist{0 1 2 3 4 5 6 7}

\countoccurrences[name=new]{1 1 1 1 2 2 2 2}
\getoccurrences[new]{0}\quad
\getoccurrences[new]{1}\quad
\getoccurrences[new]{2}

\bigskip

\countoccurrences{1 2 4 0 3 1 0 0 2 1 7 1 4 2 3 0 0 6 5 2 1 2 3 0}

\begin{tabular}{cc}
  0 & \getoccurrences{0} \\
  1 & \getoccurrences{1} \\
  2 & \getoccurrences{2} \\
  3 & \getoccurrences{3} \\
  4 & \getoccurrences{4} \\
  5 & \getoccurrences{5} \\
  6 & \getoccurrences{6} \\
  7 & \getoccurrences{7} \\
\end{tabular}

\bigskip

\setsearchlist[alph]{a b c ce}
\countoccurrences[name=letters,list=alph]{a b c a d b b ce f}

\begin{tabular}{cc}
  a & \getoccurrences[letters]{a} \\
  b & \getoccurrences[letters]{b} \\
  c & \getoccurrences[letters]{c} \\
  ce & \getoccurrences[letters]{ce} \\
\end{tabular}

\bigskip

\countoccurrences[name=letters,self]{a b c a d b b ce f}

\begin{tabular}{cc}
  a & \getoccurrences[letters]{a} \\
  b & \getoccurrences[letters]{b} \\
  c & \getoccurrences[letters]{c} \\
  ce & \getoccurrences[letters]{ce} \\
  d & \getoccurrences[letters]{d} \\
  f & \getoccurrences[letters]{f} \\
\end{tabular}


\end{document}

enter image description here


A version that allows also control sequences, so long as they expand to characters. When control sequences are expected in the list to count in, a separator must be specified with the key sep= (a semicolon in the example), because spaces are ignored after control sequence (unless you want to input it as

\countoccurrences{
  \fravaerElevEn\space
  \fravaerElevTo\space
  \fravaerElevTre\space
  ...
}

Here's the example

\documentclass{article}
\usepackage{xparse,l3regex}

\ExplSyntaxOn
\NewDocumentCommand{\countoccurrences}{ O{} m }
 {
  \group_begin:
  \keys_set:nn { svend/occurrences } { #1 }
  % if the self key appeared, make the list name equal to the sequence name
  \bool_if:NT \l__svend_occurrences_self_bool
   {
    \tl_set_eq:NN \l__svend_occurrences_list_tl \l__svend_occurrences_name_tl
   }
  \svend_count_occurrences:x { #2 }
  \group_end:
 }
\NewDocumentCommand{\setsearchlist}{ O{default} m }
 {
  \svend_occurrences_set_searchlist:nn { #1 } { #2 }
 }
\DeclareExpandableDocumentCommand{\getoccurrences}{ O{default} m }
 {
  \prop_item:cn { g_svend_occurrences_#1_prop } { #2 }
 }

\keys_define:nn { svend/occurrences }
 {
  name .tl_set:N   = \l__svend_occurrences_name_tl,
  name .initial:n  = default,
  list .tl_set:N   = \l__svend_occurrences_list_tl,
  list .initial:n  = default,
  self .bool_set:N = \l__svend_occurrences_self_bool,
  self .default:n  = true,
  self .initial:n  = false,
  sep  .tl_set:N   = \l__svend_occurrences_sep_tl,
  sep  .initial:n  = {~},
 }

\prop_new:N \g_svend_occurrences_default_prop
\seq_new:N \g_svend_occurrences_default_list_seq
\int_new:N \l__svend_occurrences_matches_int
\tl_new:N \l__svend_occurrences_input_tl

\cs_new_protected:Npn \svend_count_occurrences:n #1
 {
  \tl_set:Nn \l__svend_occurrences_input_tl { #1~ } % add a trailing space
  \tl_replace_all:NVn \l__svend_occurrences_input_tl \l__svend_occurrences_sep_tl { ~ }
  % create a new property list for storing the values for the current string
  % or clear an existing one
  \prop_gclear_new:c { g_svend_occurrences_ \l__svend_occurrences_name_tl _prop }
  % populate the search list, if self is used
  \bool_if:NT \l__svend_occurrences_self_bool
   {
    \svend_occurrences_set_searchlist:Vn \l__svend_occurrences_name_tl { #1 }
   }
  % map through the search list
  \seq_map_inline:cn { g_svend_occurrences_ \l__svend_occurrences_list_tl _list_seq }
   {
    % store the current search item; using the token list in
    % the regex search expression should avoid problems with
    % special characters
    \tl_set:Nn \l__svend_occurrences_temp_tl { ##1 }
    % count the number of times “search item/space” appears
    % (a trailing space is added) and store it in an integer variable
    % (in a regex, \u{tl variable name} stands for the contents of the variable
    \regex_count:nVN
     { \u{l__svend_occurrences_temp_tl}\s } % search expression
     \l__svend_occurrences_input_tl % token list to search in
     \l__svend_occurrences_matches_int % store the result here
    % put the number of matches in the property list, corresponding to
    % the current item as key
    \prop_gput:cnx { g_svend_occurrences_ \l__svend_occurrences_name_tl _prop }
     { ##1 }
     { \int_to_arabic:n { \l__svend_occurrences_matches_int } }
   }
 }
\cs_generate_variant:Nn \tl_replace_all:Nnn { NV }
\cs_generate_variant:Nn \regex_count:nnN { nV }
\cs_generate_variant:Nn \svend_count_occurrences:n { x }

% populate a search list (with duplicates removed)
\cs_new_protected:Npn \svend_occurrences_set_searchlist:nn #1 #2
 {
  \seq_gclear_new:c { g_svend_occurrences_#1_list_seq }
  \seq_gset_split:cnn { g_svend_occurrences_#1_list_seq } { ~ } { #2 }
  \seq_gremove_duplicates:c { g_svend_occurrences_#1_list_seq }
 }
\cs_generate_variant:Nn \seq_gset_split:Nnn { c }
\cs_generate_variant:Nn \svend_occurrences_set_searchlist:nn { V }

\ExplSyntaxOff

\begin{document}

\setsearchlist{0 1 2 3 4 5 6 7}

\countoccurrences[name=new]{1 1 1 1 2 2 2 2}
\getoccurrences[new]{0}\quad
\getoccurrences[new]{1}\quad
\getoccurrences[new]{2}

\bigskip

\countoccurrences{1 2 4 0 3 1 0 0 2 1 7 1 4 2 3 0 0 6 5 2 1 2 3 0}

\begin{tabular}{cc}
  0 & \getoccurrences{0} \\
  1 & \getoccurrences{1} \\
  2 & \getoccurrences{2} \\
  3 & \getoccurrences{3} \\
  4 & \getoccurrences{4} \\
  5 & \getoccurrences{5} \\
  6 & \getoccurrences{6} \\
  7 & \getoccurrences{7} \\
\end{tabular}

\bigskip

\setsearchlist[alph]{a b c ce}
\countoccurrences[name=letters,list=alph]{a b c a d b b ce f}

\begin{tabular}{cc}
  a & \getoccurrences[letters]{a} \\
  b & \getoccurrences[letters]{b} \\
  c & \getoccurrences[letters]{c} \\
  ce & \getoccurrences[letters]{ce} \\
\end{tabular}

\bigskip

\countoccurrences[name=letters,self]{a b c a d b b ce f}

\begin{tabular}{cc}
  a & \getoccurrences[letters]{a} \\
  b & \getoccurrences[letters]{b} \\
  c & \getoccurrences[letters]{c} \\
  ce & \getoccurrences[letters]{ce} \\
  d & \getoccurrences[letters]{d} \\
  f & \getoccurrences[letters]{f} \\
\end{tabular}

\bigskip

\def\fravaerElevEn{1}
\def\fravaerElevTo{2}
\def\fravaerElevTre{4}
\def\fravaerElevFire{0}
\def\fravaerElevFem{3}
\def\fravaerElevSeks{1}
\def\fravaerElevSyv{0}
\def\fravaerElevOtte{0}
\def\fravaerElevNi{2}
\def\fravaerElevTi{1}
\def\fravaerElevElleve{7}
\def\fravaerElevTolv{1}
\def\fravaerElevTretten{4}
\def\fravaerElevFjorten{2}
\def\fravaerElevFemten{3}
\def\fravaerElevSeksten{0}
\def\fravaerElevSytten{0}
\def\fravaerElevAtten{6}
\def\fravaerElevNitten{5}
\def\fravaerElevTyve{2}
\def\fravaerElevEnogtyve{1}
\def\fravaerElevToogtyve{2}
\def\fravaerElevTreogtyve{3}
\def\fravaerElevFireogtyve{0}

\setsearchlist{0 1 2 3 4 5 6 7}

\countoccurrences[sep={;}]{
  \fravaerElevEn;
  \fravaerElevTo;
  \fravaerElevTre;
  \fravaerElevFire;
  \fravaerElevFem;
  \fravaerElevSeks;
  \fravaerElevSyv;
  \fravaerElevOtte;
  \fravaerElevNi;
  \fravaerElevTi;
  \fravaerElevElleve;
  \fravaerElevTolv;
  \fravaerElevTretten;
  \fravaerElevFjorten;
  \fravaerElevFemten;
  \fravaerElevSeksten;
  \fravaerElevSytten;
  \fravaerElevAtten;
  \fravaerElevNitten;
  \fravaerElevTyve;
  \fravaerElevEnogtyve;
  \fravaerElevToogtyve;
  \fravaerElevTreogtyve;
  \fravaerElevFireogtyve;
}

\begin{tabular}{cc}
  0 & \getoccurrences{0} \\
  1 & \getoccurrences{1} \\
  2 & \getoccurrences{2} \\
  3 & \getoccurrences{3} \\
  4 & \getoccurrences{4} \\
  5 & \getoccurrences{5} \\
  6 & \getoccurrences{6} \\
  7 & \getoccurrences{7}
\end{tabular}

\end{document}

The last table is the same as the first one.

8
  • When I try to compile the code for dealing with arbitrary sequences of characters, I get the error The sequence \g_svend_occurrences_default_list_seq contains the items (without outer braces): > {0} > {1} > {2} > {3} > {4} > {5} > {6} > {7}. <recently read> } l.73 \countoccurrences[name=new]{1 1 1 1 2 2 2 2}. – Svend Tveskæg Dec 15 '14 at 14:21
  • @SvendTveskæg Sorry, I forgot to remove the debugging bit I left. Fixed. – egreg Dec 15 '14 at 14:28
  • I have a problem with your code; please see my updated question. :) – Svend Tveskæg Dec 15 '14 at 18:51
  • @SvendTveskæg Didn't read the whole code but using \cs_generate_variant:Nn \svend_count_occurrences:n { x } and using the \svend_count_occurrences:x in \countoccurrences may do it. – Manuel Dec 15 '14 at 19:57
  • @Manuel Okay. You are (more than) welcome to change it for me in my updated question. :) – Svend Tveskæg Dec 15 '14 at 20:02
7

The count values are saved to \csname c:<token>\endcsname and <token> can be aritrary token or token string. The \countthis macro counts the occurrences and the \showcount macro expands to the result.

Compare the compactness of the code when we are using TeX primitives:

\newcount\tmpnum
\def\countthis#1{\countthisA #1 {} }
\def\countthisA#1 {\ifx^#1^\else \countthisB{#1}\expandafter\countthisA\fi}
\def\countthisB#1{%
   \tmpnum=0\csname c:#1\endcsname\relax
   \advance\tmpnum by1
   \expandafter\edef\csname c:#1\endcsname{\the\tmpnum}%
}
\def\showcount#1{%
   \expandafter\ifx\csname c:#1\endcsname\relax 0\else \csname c:#1\endcsname\fi}

\countthis{1 2 4 0 3 1 0 0 2 1 7 1 4 2 3 0 0 6 5 2 1 2 3 0}

test:
0:\showcount0, 1:\showcount1, 2:\showcount2, 3:\showcount3, 4:\showcount4,
5:\showcount5, 6:\showcount6, 7:\showcount7, 8:\showcount8, 9:\showcount9,
5

Here's something rather crude that will do the trick provided you don't have too many integer values you want to test:

\documentclass{article}
\usepackage{etoolbox}
\makeatletter

\newcommand\countoccurances[1]{\ae@count@occurances #1 \relax\@nil}

\def\ae@count@occurances#1 #2\@nil{%%
  \def\ae@continue{}%%
  \ifx\relax#2
    \ae@increment@item{#1}%%
  \else
    \def\ae@continue{\ae@count@occurances#2\@nil}%%
    \ae@increment@item{#1}%%
  \fi
  \ae@continue
  }

\def\ae@increment@item#1{%%
  \ifcsundef{ae@num@#1}
    {\expandafter\edef\csname ae@num@#1\endcsname{1}}
    {\expandafter\edef\csname ae@num@#1\endcsname{\number\numexpr\csname ae@num@#1\endcsname+1}}
  }

\newcommand\aegetcount[1]{\csname ae@num@#1\endcsname}

\makeatother

\begin{document}

\countoccurances{1 2 4 0 3 1 0 0 2 1 7 1 4 2 3 0 0 6 5 2 1 2 3 0}

\begin{tabular}{cc}
  1 & \aegetcount{1} \\
  2 & \aegetcount{2} \\
  3 & \aegetcount{3} \\
  4 & \aegetcount{4} \\
  5 & \aegetcount{5} \\
  6 & \aegetcount{6} \\
  7 & \aegetcount{7} \\
\end{tabular}

\end{document}

Here is a LaTeX3 version of the above:

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn

\NewDocumentCommand\countoccurances{m}
  {
    \ae_count_occurences:n {#1}
  }

\seq_new:N \l_count_integer_seq
\seq_new:N \l_count_integer_values_traced_seq

\cs_new_protected:Npn \ae_count_occurences:n #1 
  {
    \seq_set_split:Nnn \l_count_integer_seq {~} {#1}
    \seq_map_inline:Nn \l_count_integer_seq 
      {
        \cs_if_free:cTF { __ae_count_integer_##1: }
          { 
            \cs_new:cn { __ae_count_integer_##1: } { 1 } 
            \seq_put_right:Nn \l_count_integer_values_traced_seq
                              { __ae_count_integer_##1: }
          }
          { \cs_set:cx { __ae_count_integer_##1: }
                       { \int_eval:n 
                           { \use:c { __ae_count_integer_##1: } + 1 } } }
      }
  }

\NewDocumentCommand\aegetcount{m}
  {
    \cs_if_free:cF { __ae_count_integer_#1: } 
      { \use:c { __ae_count_integer_#1: }}
  }

\NewDocumentCommand\aeclearcounts{}
  { 
    \seq_map_inline:Nn \l_count_integer_values_traced_seq
      { 
        \cs_undefine:c { ##1 }
      }
    \seq_clear:N \l_count_integer_values_traced_seq
  }

\ExplSyntaxOff

\begin{document}

\countoccurances{1 2 4 0 3 1 0 0 2 1 7 1 4 2 3 0 0 6 5 2 1 2 3 0}

\begin{tabular}{cc}
  1 & \aegetcount{1} \\
  2 & \aegetcount{2} \\
  3 & \aegetcount{3} \\
  4 & \aegetcount{4} \\
  5 & \aegetcount{5} \\
  6 & \aegetcount{6} \\
  7 & \aegetcount{7} \\
\end{tabular}

\vspace{1in}

\aeclearcounts
\countoccurances{1 2 4 0 3 1 0 0 2 1 7 1 4 2 3 0 0 6 5 2 1 2 3 0}

\begin{tabular}{cc}
  1 & \aegetcount{1} \\
  2 & \aegetcount{2} \\
  3 & \aegetcount{3} \\
  4 & \aegetcount{4} \\
  5 & \aegetcount{5} \\
  6 & \aegetcount{6} \\
  7 & \aegetcount{7} \\
\end{tabular}

\end{document}

For the LaTeX3 version, I've added the ability to clear the counts for the various integers. I've made it a second command so that the user can control when this is desirable to do.

1
  • Really nice solutions! Thank you very much for the help. – Svend Tveskæg Dec 15 '14 at 14:22
4

For the fun of it an expandable way, only for digits:

\input xint.sty

\catcode`_ 11
\def\countthis #1{\romannumeral0\_countthis 0#1^}
\def\_countthis #1#2{\ifcat ^#2\_countend {#1}\fi
                     \expandafter\_countthis\expandafter
                     {\romannumeral0\xintiiadd{#1}{\xintiiE{1}{#2}}}}
\def\_countend #1\fi #2#3#4#5{\fi\expandafter\_countdisplay
                              \romannumeral0\xintreverseorder {#1}0000000000x}
% too bad D. Knuth forgot the #0 !
\def\_countdisplay #1#2#3#4#5#6#7#8#9{\__countdisplay 
  {0: #1\endgraf
   1: #2\endgraf
   2: #3\endgraf
   3: #4\endgraf
   4: #5\endgraf
   5: #6\endgraf
   6: #7\endgraf
   7: #8\endgraf
   8: #9\endgraf }}
\def\__countdisplay #1#2#3x{\space #19: #2\endgraf}

\catcode`_ 8

\countthis{1 2 4 0 3 1 0 0 2 1 7 1 4 2 3 0 0 6 5 2 1 2 3 0}

\bye

counting

To allow up to 99 occurrences for each digit:

\input xint.sty

\catcode`_ 11
\def\countthis #1{\romannumeral0\_countthis 0#1^}
\def\_countthis #1#2{\ifcat ^#2\_countend {#1}\fi
                     \expandafter\_countthis\expandafter
                     {\romannumeral0\xintiiadd{#1}{\xintiiE{1}{#2+#2}}}}
\def\_countend #1\fi #2#3#4#5{\fi\expandafter\_countdisplay
           \romannumeral0\xintreverseorder {#1}00000000000000000000x}
\def\_countdisplay #1#2#3#4#5#6#7#8{\__countdisplay 
  {0: #2#1\endgraf
   1: #4#3\endgraf
   2: #6#5\endgraf
   3: #8#7\endgraf
   }}
\def\__countdisplay #1#2#3#4#5#6#7#8#9{\__countdisplay_
  {#14: #3#2\endgraf
   5: #5#4\endgraf
   6: #7#6\endgraf
   7: #9#8\endgraf
  }}
\def\__countdisplay_ #1#2#3#4#5#6x{\space #1%
   8: #3#2\endgraf
   9: #5#4\endgraf 
}


\catcode`_ 8


\countthis{1 2 4 0 3 1 0 0 2 1 7 1 4 2 3 0 0 6 5 2 1 2 3 0
           1 2 4 0 3 1 0 0 2 1 7 1 4 2 3 0 0 6 5 2 1 2 3 0
           1 2 4 0 3 1 0 0 2 1 7 1 4 2 3 0 0 6 5 2 1 2 3 0
           1 2 4 0 3 1 0 0 2 1 7 1 4 2 3 0 0 6 5 2 1 2 3 0
           1 2 4 0 3 1 0 0 2 1 7 1 4 2 3 0 0 6 5 2 1 2 3 0
           1 2 4 0 3 1 0 0 2 1 7 1 4 2 3 0 0 6 5 2 1 2 3 0
           1 2 4 0 3 1 0 0 2 1 7 1 4 2 3 0 0 6 5 2 1 2 3 0
           1 2 4 0 3 1 0 0 2 1 7 1 4 2 3 0 0 6 5 2 1 2 3 0
           1 2 4 0 3 1 0 0 2 1 7 1 4 2 3 0 0 6 5 2 1 2 3 0
           1 2 4 0 3 1 0 0 2 1 7 1 4 2 3 0 0 6 5 2 1 2 3 0
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
}

\bye

counting more


Still for only digits, and dropping expandability, but allowing arbitrarily many occurrences. And more efficient as we don't do all these crazy additions of (possibly long) integers, only digit per digit counts here (alike other proposed answers).

\input xinttools.sty

\def\countthis #1{%
    %
    \xintFor* ##1 in {0123456789}\do
      {\expandafter\def\csname _count##1\endcsname {0}}%
    %
    \xintFor* ##1 in {#1} \do
    {\expandafter\def\csname _count##1\expandafter\endcsname
     \expandafter{\the\numexpr\csname _count##1\endcsname + 1}}%
    %
    \xintFor* ##1 in {0123456789}\do
      {##1: \csname _count##1\endcsname\endgraf }%
}%

\countthis {%
27182818284590452353602874713526624977572470936999
59574966967627724076630353547594571382178525166427
42746639193200305992181741359662904357290033429526
05956307381323286279434907632338298807531952510190
11573834187930702154089149934884167509244761460668
0822648001684774118} 

\bye

counting even more

1
  • but quite dispendious with all these additions of powers of 10 or 100... see updated answer for a completely different approach more like the other answers. – user4686 Dec 15 '14 at 22:39
1

Here is a solution using pgffor :

\documentclass[varwidth,border=7]{standalone}
\usepackage{pgffor}

% the occurence macro using foreach
\newcounter{founded}
\def\occurence[#1]#2{%
  \setcounter{founded}{0}%
  \foreach \element in #2 {\ifnum \element = #1 \stepcounter{founded}\fi}%
  \thefounded%
}

\begin{document}
  % store the data
  \newcommand{\data}{
    3,4,0,3,0,4,3,7,7,3,
    4,4,7,4,1,1,4,7,8,2,
    9,3,4,1,3,3,5,7,9,5,
    8,1,8,5,1,9,1,2,9,8,
    8,7,0,0,9,1,9,5,7,8,
    5,8,1,7,7,1,1,7,4,2,
    7,9,0,5,4,2,5,9,4,8,42}

  % count the data
  \foreach \j in {0,...,9} {      
    \j : \occurence[\j]\data\par
  }
  42 : \occurence[42]\data    
\end{document}

enter image description here

The idea is similar to https://tex.stackexchange.com/a/216376/9335 .

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.