如何计算特定列中 M 和 F 的出现次数,男性和女性死亡人数?

如何计算特定列中 M 和 F 的出现次数,男性和女性死亡人数?

如何命令 LaTeX 计算表格中特定列中 M 和 F 的出现次数(从第一行到最后一行)?还有男性死亡人数和女性死亡人数,我知道如果行数较少,我们可以手动进行目视计数,但如果行数较多,目视计数可能无法进行。我在这里附上了我的 latex 表格代码。提前谢谢您。

\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}

在此处输入图片描述

答案1

使用的一个变体datatool和一些计数器。

在此处输入图片描述

\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}

答案2

在这里,如果您将表格数据放在 中\def,则可以使用创建的宏\analyze来查找模式。此宏使用listofitems包来解析表格中的数据并计算结果。由于模式是精确指定的,因此它依赖于您(用户)在性别和死亡列周围应用空格填充(即& M & Y不会被解释为&M&Y)。

话虽如此,的结果\analyze存储在\males、、和\females中,以后可以调用。\maledeaths\femaledeaths

\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}

在此处输入图片描述

相关内容