我想知道是否有任何方法可以让 Latex 针对我的表格的每一行计算两个单元格的总和并将其写入新单元格中。
使用下面的代码,我可以自动将两列的值相加,但是对于第一行,我需要将 375 和 182 的总和写入下一个单元格(现在是?)。
谢谢
\documentclass[10pt,a4paper]{article}
\usepackage{tabularx}
\usepackage{array}
\usepackage{collcell}
%% Ignore as many rows I need %%%%
\newtoggle{inTableHeader}% Track if still in header of table
\toggletrue{inTableHeader}% Set initial value
\newcommand*{\StartTableHeader}{\global\toggletrue{inTableHeader}}
\newcommand*{\EndTableHeader}{\global\togglefalse{inTableHeader}}
% Redefine tabular to initialize \StartTableHeader at start and end
\let\OldTabular\tabular%
\let\OldEndTabular\endtabular%
\renewenvironment{tabular}{\StartTableHeader\OldTabular}{\OldEndTabular\StartTableHeader}%
% define new columns for tabular
\newcounter{SumMain}
\newcommand*{\Smacro}[1]{\iftoggle{inTableHeader}{#1}{#1\addtocounter{SumMain}{#1}}}
\newcolumntype{M}{>{\collectcell\Smacro}r<{\endcollectcell}}
\newcounter{SumInfo}
\newcommand*{\Smacr}[1]{\iftoggle{inTableHeader}{#1}{#1\addtocounter{SumInfo}{#1}}}
\newcolumntype{I}{>{\collectcell\Smacr}{r}<{\endcollectcell}}
\usepackage[textheight=26cm, textwidth=18cm,headheight=0cm, marginparsep=0cm]{geometry}
%\usepackage{spreadtab} % for authomatic sum
\begin{document}
\begin{tabular}{|l|l||M|I|l||l| }
\hline & & Main & Info box & Total & Year \EndTableHeader \\
\hline\hline
1 & Preface & 375 & 182 & ? & 1946 \\ \hline
2 & Investigation & 906 & 950 & ? & 1920 \\ \hline
\hline
\multicolumn{2}{|c|}{Total} & \theSumMain & \theSumInfo & - \\ \hline
\end{tabular}
\end{document}
答案1
您已经拥有了基础架构。只需使用新计数器添加一些额外的代码即可SumRow
。只需使用的内容定义它Main
,并使用的内容对其进行扩充即可Info Box
。
\documentclass[10pt,a4paper]{article}
\usepackage{tabularx}
\usepackage{array}
\usepackage{collcell}
%% Ignore as many rows I need %%%%
\newtoggle{inTableHeader}% Track if still in header of table
\toggletrue{inTableHeader}% Set initial value
\newcommand*{\StartTableHeader}{\global\toggletrue{inTableHeader}}
\newcommand*{\EndTableHeader}{\global\togglefalse{inTableHeader}}
% Redefine tabular to initialize \StartTableHeader at start and end
\let\OldTabular\tabular%
\let\OldEndTabular\endtabular%
\renewenvironment{tabular}{\StartTableHeader\OldTabular}{\OldEndTabular\StartTableHeader}%
% define new columns for tabular
\newcounter{SumRow}
\newcounter{SumMain}
\newcommand*{\Smacro}[1]{\iftoggle{inTableHeader}{#1}{#1\addtocounter{SumMain}{#1}%
\setcounter{SumRow}{#1}}}
\newcolumntype{M}{>{\collectcell\Smacro}r<{\endcollectcell}}
\newcounter{SumInfo}
\newcommand*{\Smacr}[1]{\iftoggle{inTableHeader}{#1}{#1\addtocounter{SumInfo}{#1}%
\addtocounter{SumRow}{#1}}}
\newcolumntype{I}{>{\collectcell\Smacr}{r}<{\endcollectcell}}
\usepackage[textheight=26cm, textwidth=18cm,headheight=0cm, marginparsep=0cm]{geometry}
%\usepackage{spreadtab} % for authomatic sum
\begin{document}
\begin{tabular}{|l|l||M|I|l||l| }
\hline & & Main & Info box & Total & Year \EndTableHeader \\
\hline\hline
1 & Preface & 375 & 182 & \theSumRow & 1946 \\ \hline
2 & Investigation & 906 & 950 & \theSumRow & 1920 \\ \hline
\hline
\multicolumn{2}{|c|}{Total} & \theSumMain & \theSumInfo & - &\\ \hline
\end{tabular}
\end{document}