矩阵元素/表格单元格的总和

矩阵元素/表格单元格的总和

使用以下代码

\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{verbatim}
\usepackage{tikz}
\usetikzlibrary{calc, fit, matrix}
\begin{document}
%begin matrix autofit width
\tikzset{matrix rows/.initial=1,matrix cols/.initial=1,matrix name/.initial=x,
vline/.style={/utils/exec=\foreach \XX in {1,...,\pgfkeysvalueof{/tikz/matrix rows}}
{\ifnum\XX=1
\xdef\MatLstA{(\pgfkeysvalueof{/tikz/matrix name}-\XX-#1)}
\xdef\MatLstB{(\pgfkeysvalueof{/tikz/matrix name}-\XX-\the\numexpr1+#1\relax)}
\else
\xdef\MatLstA{\MatLstA (\pgfkeysvalueof{/tikz/matrix name}-\XX-#1)}
\xdef\MatLstB{\MatLstB (\pgfkeysvalueof{/tikz/matrix name}-\XX-\the\numexpr1+#1\relax)}
\fi},
insert path={node[fit=\MatLstA,inner sep=0pt] (fitA) {}
node[fit=\MatLstB,inner sep=0pt] (fitB) {} ($(fitA.east)!0.5!(fitB.west)$) coordinate (aux) 
(\pgfkeysvalueof{/tikz/matrix name}.north-|aux) -- (\pgfkeysvalueof{/tikz/matrix name}.south-|aux)}},
hline/.style={/utils/exec=\foreach \XX in {1,...,\pgfkeysvalueof{/tikz/matrix cols}}
{\ifnum\XX=1
\xdef\MatLstA{(\pgfkeysvalueof{/tikz/matrix name}-#1-\XX)}
\xdef\MatLstB{(\pgfkeysvalueof{/tikz/matrix name}-\the\numexpr1+#1\relax-\XX)}
\else
\xdef\MatLstA{\MatLstA (\pgfkeysvalueof{/tikz/matrix name}-#1-\XX)}
\xdef\MatLstB{\MatLstB (\pgfkeysvalueof{/tikz/matrix name}-\the\numexpr1+#1\relax-\XX)}
\fi},
insert path={node[fit=\MatLstA,inner sep=0pt] (fitA) {}
node[fit=\MatLstB,inner sep=0pt] (fitB) {} ($(fitA.south)!0.5!(fitB.north)$) coordinate (aux) 
(\pgfkeysvalueof{/tikz/matrix name}.west|-aux) -- (\pgfkeysvalueof{/tikz/matrix name}.east|-aux)}},
full matrix grid/.style={
vline/.list={1,...,\the\numexpr\pgfkeysvalueof{/tikz/matrix cols}-1},
hline/.list={1,...,\the\numexpr\pgfkeysvalueof{/tikz/matrix rows}-1},
insert path={(\pgfkeysvalueof{/tikz/matrix name}.south west) rectangle (\pgfkeysvalueof{/tikz/matrix name}.north east)}}}
%end matrix autofit width
\begin{frame}[t,fragile]
\frametitle{matrix summation}
\begin{tikzpicture}
\matrix (x) [matrix of nodes, row sep=10pt, column sep=10pt] {%
a & v & w & x & y & z & Total\\
b & 18.6 & 12.9 & 20.3 & 13.7 & 21.45 & \\
c & 26.1 & 15.7 & 14.0 & 17.8 & 33.8 & \\
d & 12.9 & 29.8 & 18.3 & 14.2 & 20.8 & \\
Total &  &  &  &  &  & \\};
\end{tikzpicture}
\end{frame}
\begin{frame}[t,fragile]
\frametitle{table summation}
\begin{tabular}{|c|c|c|c|c|c|c|c|}
\hline
a & v & w & x & y & z & Total\\
\hline
b & 18.6 & 12.9 & 20.3 & 13.7 & 21.45 & \\
\hline
c & 26.1 & 15.7 & 14.0 & 17.8 & 33.8 & \\
\hline
d & 12.9 & 29.8 & 18.3 & 14.2 & 20.8 & \\
\hline
Total &  &  &  &  &  & \\
\hline
\end{tabular}
\end{frame}
\end{document}

从答案这个问题

是否可以在最后一列/行显示矩阵元素/表格单元格的总和

在此处输入图片描述

答案1

是的,可以做到。不过,我在这里不使用 tikz 矩阵,而是使用spreadtab。我从以下网址了解到这个包的存在Ulrike Fischer 在德国网站上的回答,一直想尝试一次,所以我们就开始吧。

\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{verbatim}
\usepackage{spreadtab}
\begin{document}
\begin{frame}[t,fragile]
\frametitle{table summation}
\begin{spreadtab}{{tabular}{|c|c|c|c|c|c|c|}}
\hline
      @a & @v & @w & @x & @y & @z & @Total\\
\hline
@b & 18.6 & 12.9 & 20.3 & 13.7 & 21.45 & sum(b2:f2)\\
\hline
@c & 26.1 & 15.7 & 14.0 & 17.8 & 33.8 & sum(b3:f3)\\
\hline
@d & 12.9 & 29.8 & 18.3 & 14.2 & 20.8 & sum(b4:f4)\\
\hline
& sum(b2:b4) & sum(c2:c4) & sum(d2:d4) & sum(e2:e4) &  sum(f2:f4)& \\
\hline
\end{spreadtab}
\end{frame}
\end{document}

在此处输入图片描述

实现这一目标的其他方法包括pgfplotstable(至少我相信是这样的)。

将这些总和转移到矩阵,但这需要一些努力。

\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{verbatim}
\usepackage{spreadtab}
\usepackage{tikz}
\usetikzlibrary{calc, fit, matrix}
\newsavebox\SpreadTab
\begin{document}
%begin matrix autofit width from https://tex.stackexchange.com/a/484807/121799
\tikzset{matrix rows/.initial=1,matrix cols/.initial=1,matrix name/.initial=x,
vline/.style={/utils/exec=\foreach \XX in {1,...,\pgfkeysvalueof{/tikz/matrix rows}}
{\ifnum\XX=1
\xdef\MatLstA{(\pgfkeysvalueof{/tikz/matrix name}-\XX-#1)}
\xdef\MatLstB{(\pgfkeysvalueof{/tikz/matrix name}-\XX-\the\numexpr1+#1\relax)}
\else
\xdef\MatLstA{\MatLstA (\pgfkeysvalueof{/tikz/matrix name}-\XX-#1)}
\xdef\MatLstB{\MatLstB (\pgfkeysvalueof{/tikz/matrix name}-\XX-\the\numexpr1+#1\relax)}
\fi},
insert path={node[fit=\MatLstA,inner sep=0pt] (fitA) {}
node[fit=\MatLstB,inner sep=0pt] (fitB) {} ($(fitA.east)!0.5!(fitB.west)$) coordinate (aux) 
(\pgfkeysvalueof{/tikz/matrix name}.north-|aux) -- (\pgfkeysvalueof{/tikz/matrix name}.south-|aux)}},
hline/.style={/utils/exec=\foreach \XX in {1,...,\pgfkeysvalueof{/tikz/matrix cols}}
{\ifnum\XX=1
\xdef\MatLstA{(\pgfkeysvalueof{/tikz/matrix name}-#1-\XX)}
\xdef\MatLstB{(\pgfkeysvalueof{/tikz/matrix name}-\the\numexpr1+#1\relax-\XX)}
\else
\xdef\MatLstA{\MatLstA (\pgfkeysvalueof{/tikz/matrix name}-#1-\XX)}
\xdef\MatLstB{\MatLstB (\pgfkeysvalueof{/tikz/matrix name}-\the\numexpr1+#1\relax-\XX)}
\fi},
insert path={node[fit=\MatLstA,inner sep=0pt] (fitA) {}
node[fit=\MatLstB,inner sep=0pt] (fitB) {} ($(fitA.south)!0.5!(fitB.north)$) coordinate (aux) 
(\pgfkeysvalueof{/tikz/matrix name}.west|-aux) -- (\pgfkeysvalueof{/tikz/matrix name}.east|-aux)}},
full matrix grid/.style={
vline/.list={1,...,\the\numexpr\pgfkeysvalueof{/tikz/matrix cols}-1},
hline/.list={1,...,\the\numexpr\pgfkeysvalueof{/tikz/matrix rows}-1},
insert path={(\pgfkeysvalueof{/tikz/matrix name}.south west) rectangle (\pgfkeysvalueof{/tikz/matrix name}.north east)}}}
%end matrix autofit width
\begin{frame}[t,fragile]
\frametitle{matrix summation}
\begin{lrbox}{\SpreadTab}
\begin{spreadtab}[\STsavecell\SumOne{g2}\STsavecell\SumTwo{g3}\STsavecell\SumThree{g4}%
\STsavecell\SumB{b5}\STsavecell\SumC{c5}\STsavecell\SumD{d5}%
\STsavecell\SumE{e5}\STsavecell\SumF{f5}\STsavecell\SumTotal{g5}]{{tabular}{|c|c|c|c|c|c|c|}}
\hline
      @a & @v & @w & @x & @y & @z & @Total\\
\hline
@b & 18.6 & 12.9 & 20.3 & 13.7 & 21.45 & sum(b2:f2)\\
\hline
@c & 26.1 & 15.7 & 14.0 & 17.8 & 33.8 & sum(b3:f3)\\
\hline
@d & 12.9 & 29.8 & 18.3 & 14.2 & 20.8 & sum(b4:f4)\\
\hline
& sum(b2:b4) & sum(c2:c4) & sum(d2:d4) & sum(e2:e4) &  sum(f2:f4)& 
sum(b5:f5)\\
\hline
\end{spreadtab}
\end{lrbox}

\begin{tikzpicture}
\matrix (x) [matrix of nodes, row sep=10pt, column sep=10pt,nodes in empty cells] {%
a & v & w & x & y & z & Total\\
b & 18.6 & 12.9 & 20.3 & 13.7 & 21.45 & \SumOne\\
c & 26.1 & 15.7 & 14.0 & 17.8 & 33.8 & \SumTwo\\
d & 12.9 & 29.8 & 18.3 & 14.2 & 20.8 & \SumThree\\
Total & \SumB & \SumC  & \SumD & \SumE & \SumF & \SumTotal \\};
\draw[matrix name=x,matrix cols=7,matrix rows=5,full matrix grid];
\end{tikzpicture}

%\usebox\SpreadTab
\end{frame}
\end{document}

在此处输入图片描述

相关内容