Counting of selected words

LaTeX provides built-in macros to handle counters and that is how the sectioning numbering, page numbers and floats are handled. In https://tex.stackexchange.com/a/19761/963 I posted a reply which is very similar to what you want to do and which I have adapted to include counters.

\documentclass{article}
\usepackage{lstdoc,booktabs}
\begin{document}
\makeatletter

\newcounter{cnt}
\setcounter{cnt}{0}
\let\alist\@empty

\def\addtolist#1#2{%
  \lst@lAddTo#1{#2}
}

\def\RA#1|#2|#3|#4;{%
  \addtolist{\alist}{#1#2,}%
  \expandafter\gdef\csname#1#2\endcsname{\textit{#1}&#2&#3&#4\cr\relax}
  \lst@BubbleSort{\alist}
  \stepcounter{cnt}
}

\def\RB#1|#2|#3|#4;{%
   \addtolist{\alist}{#2#1,}%
   \expandafter\gdef\csname#2#1\endcsname{\textit{#1}&#2&#3&#4\cr\relax}
   \lst@BubbleSort{\alist}
}

%% adding the data now
\RA Lactarius fallax      | velvety milk cap |edible |potentially risky;  
\RA Lactarius camphoratus | candy cap        |edible |aromatic qualities;
\RA Suillus pungens       | slippery Jack    |edible |poor taste;
\RA Lactarius affinis     | kindred milk     |edible |unpalatable;
\RA Calocybe carnea       | pink fairhead    |edible |potentially risky;
\RA Amateta ocreata       | death angel      |inedible |highly poisonous;   

%% typesetting the table
\newsavebox{\tempbox}
\savebox{\tempbox}{
\centering
\begin{tabular}{llll}
  \toprule[1pt]
  Species & Common name & Edibility & Remarks\\
  \midrule
  \@for\i:=\alist \do{\csname\i\endcsname}
  \vspace{-14pt}\\\bottomrule
\end{tabular}
}

\begin{table}
\usebox{\tempbox}
\caption{Some mushrooms from Wikipedia}
\end{table}

Number of mushrooms in database is \textbf{\thecnt}

\end{document}

The idea is that everytime you add a plant (in my examples is mushrooms), you first add it to a database macro (just a comma delimited list really) and you increment the counter. Pretty much similar to when you adding something to an index. Depending on the structure of the document, the code can be adapted to hold an image, increment a number, sort alphabetically etc. In the example we just display the names in a table.


LateX provides the command \newcounter and \stepcounter and some other for counting.

\documentclass{article}

\newcounter{plants}
\newcommand{\plant}[1]{%
    % step counter
    \stepcounter{plants}% use \refstepcounter if you want
        % to use the number with the \ref-\label-mechanism
    % new line
    \par
    % optionally print counter
%   \theplants. 
    % print name
    #1
}


\begin{document}
\plant{Foo}
\plant{Bar}
\plant{Baz}

Sum: \theplants
\end{document}

To specify my answer I need more information about you project. Please provide a minimal working example (MWE).


You can use the .idx file for counting:

\documentclass[a4paper,10pt]{article}

%For multiple index
\usepackage{imakeidx}
\makeindex[name=ch, title=Chemical index]
\makeindex[name=pl,title=Plant Index]

\begin{document}

This is my test document. It contains a total of \ref{plants-number} plants. Plant 
1\index[pl]{plant 1} has chemicals 1\index[ch]{chemical 1} and 2\index[ch]{chemical 2}. 
Another plant\index[pl]{plant 2} continas the chemicals 2\index[ch]{chemical 2} and 
3\index[ch]{chemical 3}.

List of plants:
Plant 3\index[pl]{plant 3}
plant 4\index[pl]{plant 4}
plant 5\index[pl]{plant 5}

\indexprologue{\small Index of plants}
\printindex[pl]

\indexprologue{\small Index of chemicals}
\printindex[ch]

% We'll count the number of plants and of chemicals
\newcounter{items}
\makeatletter
\begingroup
% Define suitably \indexentry
\newcommand{\indexentry}[2]{%
  \@ifundefined{#1}
    {\refstepcounter{items}\expandafter\let\csname#1\endcsname\@empty}
    {}}

% Let's count the plants
\begingroup
\setcounter{items}{0}
\input{pl.idx}\label{plants-number}
\endgroup

% Let's count the chemicals
\begingroup
\setcounter{items}{0}
\input{ch.idx}\label{chemicals-number}
\endgroup

\endgroup

\end{document}

The final code will produce no text, but only a reference to the last counted plant; this reference may be used anywhere (but, of course, this requires at least two runs of LaTeX).

Note: there's no need to give a \makeindex command and \printindex command if you have only entries marked \index[pl] and \index[ch].