7

how to command LaTeX to count occurence of M and F in a particular column of a table from first row to last row of a particular column? And also the number of Male Deaths and Number of Female Deaths, I know we can do manually counting visually if number of rows is small, but in the case of large number of rows visually counting may not be possible here I am ataching my table codes in latex. Thanking you in advance.

\documentclass[11pt]{report}
\usepackage[english]{babel}
\usepackage{graphicx} %for including eps graphics
\usepackage{booktabs}
\usepackage{spreadtab}
\begin{document}
$
\begin{tabular}{c|c|l|c|c|}
\toprule 
 Sl. & Unique ID & Name & Gender& Death \\
 \midrule
 1  & DBC0001 & Mr. John       & M & Y \\
 2  & DBC0002 & Miss Elizabeth & F & Y \\
 3  & DBC0003 & Mr. Thomas     & M & N \\
 4  & DBC0006 & Miss Maya      & F & N\\
 5  & DBC0005 & Mr. Gilbert    & M & N\\
 6  & DBC0004 & Mr. Dinesh     & M & N\\
 7  & DBC0007 & Miss Shanti    & F & Y\\
 8  & DBC0009 & Mr. Rajesh     & M & N\\
 9  & DBC0008 & Mr. Pinku      & M & N\\
 10 & DBC0010 & Mr. Virendra   & M & N\\
\end{tabular}$

\vspace{4mm}
  \hrule
  \vspace{4mm}
  \noindent Occurence of M:7\\
  Occurence of F :3\\
  \rule[-0.5ex]{3cm}{1.0pt}\\
  \noindent Total : 10
  \vspace{2mm}
  \hrule

  \vspace{4mm}
  \noindent No. of Male Death ~~:1 \\
  No. of Female Death:2\\
  \rule[-0.5ex]{3cm}{1.0pt}\\
  \noindent Total : 3

 \end{document}

enter image description here

  • 3
    Unrelated: Why have you put the tabular in math mode? – Torbjørn T. May 11 '17 at 9:53
  • Sorry for putting tabular mode, I donot know much about the commands of the desired packages which command will be suitable for here to perform calculation. @TorbjørnT. – Biki Teron May 11 '17 at 9:56
8

A variant using datatool and some counters.

enter image description here

\documentclass[11pt]{report}
\usepackage{filecontents}
% this just saves the data to a text file
\begin{filecontents*}{deathdata.txt}
Sl.,Unique ID,Name,Gender,Death
1,DBC0001,Mr. John,M,Y
2,DBC0002,Miss Elizabeth,F,Y
3,DBC0003,Mr. Thomas,M,N
4,DBC0006,Miss Maya,F,N
5,DBC0005,Mr. Gilbert,M,N
6,DBC0004,Mr. Dinesh,M,N
7,DBC0004,Miss Shanti,F,Y
8,DBC0004,Mr. Rajesh,M,N
9,DBC0004,Mr. Pinku,M,N
10,DBC0004,Mr. Virendra,M,N
\end{filecontents*}

\usepackage[english]{babel}
\usepackage{graphicx} %for including eps graphics
\usepackage{booktabs}
\usepackage{xstring} % for string comparison
\usepackage{datatool}
\renewcommand{\dtldisplaystarttab}{\toprule}
\renewcommand{\dtldisplayafterhead}{\midrule}
\renewcommand{\dtldisplayendtab}{\\\bottomrule}

% read in text file to database
\DTLloaddb{death}{deathdata.txt}

% set up counters
\newcounter{Female}
\newcounter{Male}
\newcounter{FemaleD} % D for dead
\newcounter{MaleD}
\newcounter{Total}
\newcounter{TotalD}

\begin{document}

\begin{center}
\begin{tabular}{cclcc}
\toprule
 Sl. & Unique ID & Name & Gender& Death 
\DTLforeach*{death}{\sl=Sl.,\id=Unique ID,\name=Name,\g=Gender,\d=Death}{% build table
% add \midrule before first data row
\DTLiffirstrow{\\\midrule}{\\}%
%
\IfStrEq{\g}{F}{% check if female
  \stepcounter{Female}% add 1 to counter
  \IfStrEq{\d}{Y}{\stepcounter{FemaleD}}{}% if also dead, add 1 to that counter
}{%
  \stepcounter{Male}% similar for male
  \IfStrEq{\d}{Y}{\stepcounter{MaleD}}{}%
}%
 \sl & \id & \name & \g & \d % print table row
}
\\ 
\bottomrule
\end{tabular}
\end{center}
% set total values
\setcounter{Total}{\numexpr\value{Female}+\value{Male}\relax}
\setcounter{TotalD}{\numexpr\value{FemaleD}+\value{MaleD}\relax}


% print results
\begin{tabular}{ll}
Occurence of M: & \theMale \\
Occurence of F: & \theFemale \\
\midrule
Total: & \theTotal
\end{tabular}

\bigskip

\begin{tabular}{ll}
No. of Male Death: & \theMaleD \\
  No. of Female Death: & \theFemaleD\\
\midrule
Total: & \theTotalD 
\end{tabular}

\end{document}
  • Thank you so much from the bottom of my heart .@Torbjørn T. – Biki Teron May 11 '17 at 10:51
7

Here, if you place your table data in a \def, you can use the created macro \analyze to look for patterns. This macro employs the listofitems package to parse the data in your table, and count the results. Because the patterns are precisely specified, it relies on you the user applying space padding around the gender and death columns (that is & M & Y will not be interpreted the same as &M&Y).

That being said, the results of \analyze are stored in \males, \females, \maledeaths, and \femaledeaths, which can be later recalled.

\documentclass[11pt]{report}
\usepackage[english]{babel}
\usepackage{graphicx} %for including eps graphics
\usepackage{booktabs}
\usepackage{spreadtab}
\usepackage{listofitems}
\newcommand\analyze[1]{%
  \setsepchar{& M &}%
  \readlist*\males{#1}%
  \xdef\males{\the\numexpr\listlen\males[]-1\relax}%
  \setsepchar{& F &}%
  \readlist*\females{#1}%
  \xdef\females{\the\numexpr\listlen\females[]-1\relax}%
  \setsepchar{& M & Y}%
  \readlist*\maledeaths{#1}%
  \xdef\maledeaths{\the\numexpr\listlen\maledeaths[]-1\relax}%
  \setsepchar{& F & Y}%
  \readlist*\femaledeaths{#1}%
  \xdef\femaledeaths{\the\numexpr\listlen\femaledeaths[]-1\relax}
}
\begin{document}
\def\mydata{
 1  & DBC0001 & Mr. John       & M & Y \\
 2  & DBC0002 & Miss Elizabeth & F & Y \\
 3  & DBC0003 & Mr. Thomas     & M & N \\
 4  & DBC0006 & Miss Maya      & F & N \\
 5  & DBC0005 & Mr. Gilbert    & M & N \\
 6  & DBC0004 & Mr. Dinesh     & M & N \\
 7  & DBC0007 & Miss Shanti    & F & Y \\
 8  & DBC0009 & Mr. Rajesh     & M & N \\
 9  & DBC0008 & Mr. Pinku      & M & N \\
 10 & DBC0010 & Mr. Virendra   & M & N \\
}

\analyze\mydata
\begin{tabular}{c|c|l|c|c|}
\toprule 
 Sl. & Unique ID & Name & Gender& Death \\
 \midrule
 \mydata
\end{tabular}

\vspace{4mm}
  \hrule
  \vspace{4mm}
  \noindent Occurence of M: \males\\
  Occurence of F : \females\\
  \rule[-0.5ex]{3cm}{1.0pt}\\
  \noindent Total : \the\numexpr\males+\females\relax
  \vspace{2mm}
  \hrule

  \vspace{4mm}
  \noindent No. of Male Death ~~: \maledeaths \\
  No. of Female Death: \femaledeaths\\
  \rule[-0.5ex]{3cm}{1.0pt}\\
  \noindent Total : \the\numexpr\maledeaths+\femaledeaths\relax

 \end{document}

enter image description here

  • Thank you so much from the bottom of my heart @Steven B. Segletes – Biki Teron May 11 '17 at 10:50

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.