Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I am typesetting a book, which has got more number of plants. While entering the text, I have to record the total number of plants recorded in my book.

In fact, I have created an index of all these plants, but the question is should I count manually, or LaTeX can count itself.

Here is my simple document.

\documentclass[a4paper,10pt]{article}

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

\begin{document}

This is my test docment. It contains total 5 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}

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

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

\end{document}
share|improve this question
    
How are you inputting the plants' names? –  egreg Jan 26 '12 at 13:13
    
are you entering plants in the document using some macro? That is, at every occurence of a word you want to count is there in fact \macro{word I want to count}? Where macros can be different? Theny ou could just increase a counter whenever the macro is called. –  Roelof Spijker Jan 26 '12 at 13:14
    
Just use an own \plant macro to format all plant names and which increases a count which can be displayed at the very end. –  Martin Scharrer Jan 26 '12 at 13:50

4 Answers 4

up vote 7 down vote accepted

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].

share|improve this answer
    
1. clarification of \makeindex comment. I could not understand. 2. Same logic can i apply to count chemicals also? –  Anil Jan 26 '12 at 14:32
1  
1. If there's no \index{something} command, so that you don't have a "main index", then \makeindex and \printindex are useless. 2. Yes, of course. I'll add it. –  egreg Jan 26 '12 at 14:35

LaTeX provides built-in macros to handle counters and that is how the sectioning numbering, page numbers and floats are handled. In http://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.

share|improve this answer

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).

share|improve this answer
    
i have included the MWE -Anil –  Anil Jan 26 '12 at 14:13

If you are using xetex, you could use the xesearch package:

\documentclass{article}
\usepackage{xesearch}
\newcounter{plants}
\setcounter{plants}{0}
\def\plantlist{tree?,grass,flower?}
\SearchList{list1}{#1\stepcounter{plants}}{\plantlist}
\begin{document}
    ``What is your favorite kind of plant? Trees, grass, or flowers?''
    ``I like trees because they are much taller than grass and flowers.''
\end{document}

This code creates a counter called plants, then searches for a word found in \plantlist and increments the counter each time a word from the list is found.

  • Add any word that you want to have counted to the list in line 5. E.g. tree,grass,flower.
  • If you will be writing plural forms, add those to the list too, or use "?" or "*" to match those forms. E.g. tree? will match "tree" and "trees" and cact* will match "catcus" and "cacti".
  • To display the final number of matching words, place \arabic{plants} at the end of your document.
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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