![表格,如何自动添加两个单元格的值](https://linux22.com/image/470590/%E8%A1%A8%E6%A0%BC%EF%BC%8C%E5%A6%82%E4%BD%95%E8%87%AA%E5%8A%A8%E6%B7%BB%E5%8A%A0%E4%B8%A4%E4%B8%AA%E5%8D%95%E5%85%83%E6%A0%BC%E7%9A%84%E5%80%BC.png)
我想知道是否有任何方法可以让 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}