![矩阵数组中的列和](https://linux22.com/image/250067/%E7%9F%A9%E9%98%B5%E6%95%B0%E7%BB%84%E4%B8%AD%E7%9A%84%E5%88%97%E5%92%8C.png)
我试图使我的方程看起来如下所示:
我似乎无法接近(有没有办法将标签\bordermatrix
放在底部?)。这是我到目前为止的代码(没有虚线和列总数)
\begin{eqnarray}
P^{\ast}_{t+1} &=& P_{t+1} + D_{t,t+1} - B_{t,t+1} \\ \nonumber
&=&
\left(
\begin{array}{llll}
1110 & 110 & 10 & 10 \\
45 & 440 & 45 & 0 \\
70 & 75 & 770 & 70 \\
35 & 30 & 30 & 330 \\
\end{array}
\right) +
\left(
\begin{array}{llll}
44.05 & 3.36 & 0.53 & 0.24 \\
1.79 & 13.44 & 2.37 & 0.00 \\
2.78 & 2.29 & 40.53 & 1.71 \\
1.39 & 0.92 & 1.58 & 8.05 \\
\end{array}
\right) -
\left(
\begin{array}{llll}
80 & 0 & 0 & 0 \\
0 & 25 & 0 & 0 \\
0 & 0 & 40 & 0 \\
0 & 0 & 0 & 40 \\
\end{array}
\right)
\end{eqnarray}
答案1
以下是获取所需输出的一种方法:
\documentclass{article}
\usepackage{amsmath}% http://ctan.org/pkg/amsmath
\usepackage{arydshln}% http://ctan.org/pkg/arydshln
\newcommand{\addsum}[1]{%
\mathstrut\smash{\begin{array}[t]{@{}r@{}}#1\end{array}}%
}
\begin{document}
\begin{align*}
P^{\ast}_{t+1} &= P_{t+1} + D_{t,t+1} - B_{t,t+1} \\
&=
\left(
\begin{array}{rrrr}
1110 & 110 & 10 & 10 \\
45 & 440 & 45 & 0 \\
70 & 75 & 770 & 70 \\
35 & 30 & 30 & 330
\end{array}
\right) +
\left(
\begin{array}{rrrr}
44.05 & 3.36 & 0.53 & 0.24 \\
1.79 & 13.44 & 2.37 & 0.00 \\
2.78 & 2.29 & 40.53 & 1.71 \\
\addsum{1.39\\50.01} &
\addsum{0.92\\20.01} &
\addsum{1.58\\45.01} &
\addsum{8.05\\10.00} \\ \hdashline
\end{array}
\right) -
\left(
\begin{array}{rrrr}
80 & 0 & 0 & 0 \\
0 & 25 & 0 & 0 \\
0 & 0 & 40 & 0 \\
0 & 0 & 0 & 40
\end{array}
\right)
\end{align*}
\end{document}
我修改了四列的对齐方式r
。再做一些工作,就可以得到一个居中的和。
总计按列添加,使用\addsum
您指定最后一行和“总和”行中包含的信息。arydshln
包裹提供虚线规则。或者,使用\hline
within\addsum
代替\hdashline
会产生以下输出:
%...
\left(
\begin{array}{rrrr}
44.05 & 3.36 & 0.53 & 0.24 \\
1.79 & 13.44 & 2.37 & 0.00 \\
2.78 & 2.29 & 40.53 & 1.71 \\
\addsum{1.39\\\hline 50.01} &
\addsum{0.92\\\hline 20.01} &
\addsum{1.58\\\hline 45.01} &
\addsum{8.05\\\hline 10.00}
\end{array}
\right) -
%...
在这两种情况下,\smash
都会删除其内容的所有垂直高度/深度。因此,在此表达式下方添加内容时要小心。在表单中添加一个空白行\\&
通常就足够了。
答案2
我正在尝试 Martin Scharrer 的有趣作品collcell
包,我设法自动获取列的总和。它没有针对结果进行调整,因为其他答案无论如何都很棒。
\documentclass{article}
\usepackage{amsmath,collcell,tikz}
\def\columnone#1{#1\expandafter\pgfmathparse\expandafter{\cone + #1}\xdef\cone{\pgfmathresult}}
\def\columntwo#1{#1\expandafter\pgfmathparse\expandafter{\ctwo + #1}\xdef\ctwo{\pgfmathresult}}
\def\columnthree#1{#1\expandafter\pgfmathparse\expandafter{\cthree + #1}\xdef\cthree{\pgfmathresult}}
\def\columnfour#1{#1\expandafter\pgfmathparse\expandafter{\cfour + #1}\xdef\cfour{\pgfmathresult}}
\newcolumntype{X}{>{\collectcell\columnone}c<{\endcollectcell}}
\newcolumntype{Y}{>{\collectcell\columntwo}c<{\endcollectcell}}
\newcolumntype{W}{>{\collectcell\columnthree}c<{\endcollectcell}}
\newcolumntype{Z}{>{\collectcell\columnfour}c<{\endcollectcell}}
\begin{document}
\begin{align*}
P^{\ast}_{t+1} &= P_{t+1} + D_{t,t+1} - B_{t,t+1} \\
&=
\left(
\begin{array}{rrrr}
1110 & 110 & 10 & 10 \\
45 & 440 & 45 & 0 \\
70 & 75 & 770 & 70 \\
35 & 30 & 30 & 330
\end{array}
\right) + \left[
\begin{tikzpicture}[baseline=(current bounding box.center)]
\def\cone{0} \def\ctwo{0} \def\cthree{0} \def\cfour{0}
\node[inner sep=0] (tabnode) {
\begin{tabular}{XYWZ}
44.05 & 3.36 & 0.53 & 0.24 \\
1.79 & 13.44 & 2.37 & 0.00 \\
2.78 & 2.29 & 40.53 & 1.71 \\
1.39 & 0.92 & 1.58 & 8.05 \\
\end{tabular}
};
\useasboundingbox (tabnode.north east) -- (tabnode.south west);
\draw[dashed] (tabnode.south west) -- (tabnode.south east);
\node[anchor=north] (s1) at (tabnode.205) {\cone};
\node[anchor=north] (s2) at (tabnode.237) {\ctwo};
\node[anchor=north] (s3) at (tabnode.307) {\cthree};
\node[anchor=north] (s4) at (tabnode.338) {\cfour};
\end{tikzpicture}
\right]
-
\left(
\begin{array}{rrrr}
80 & 0 & 0 & 0 \\
0 & 25 & 0 & 0 \\
0 & 0 & 40 & 0 \\
0 & 0 & 0 & 40
\end{array}
\right)
\end{align*}
\end{document}
基本上,在每列的相应宏中都会进行数字累加,然后我们将该数字放入 TikZ 节点中。其中有几件事:首先,节点放置并不令人愉快,需要通过获取列宽并在小数点处对齐等方式实现自动化解决方案。其次,最好在执行命令后重置这些寄存器以供连续使用。数字格式化相对简单,但我跳过了它。
答案3
以下是基于的解决方案TikZ
。基本提示是:
- 更换矩阵分离器以避免出现
align
环境问题; - 固定
baseline
位置并inner sep
调整尺寸。
代码:
\documentclass{article}
\usepackage{amsmath,amssymb}
\usepackage{tikz}
\usetikzlibrary{matrix,calc}
\begin{document}
\begin{align*}
P^{\ast}_{t+1} &= P_{t+1} + D_{t,t+1} - B_{t,t+1} \\
&=
\left(
\begin{array}{llll}
1110 & 110 & 10 & 10 \\
45 & 440 & 45 & 0 \\
70 & 75 & 770 & 70 \\
35 & 30 & 30 & 330 \\
\end{array}
\right)+
\begin{tikzpicture}[baseline=-0.1cm]
%\let\&=\pgfmatrixnextcell % <= one choice to change col separator
\matrix[matrix of math nodes,left delimiter=(,right delimiter=),inner sep=2.5pt,
ampersand replacement=\&] % <= to change col separator for align env
{
\node {44.05}; \& \node{3.36};\& \node {0.53}; \& \node{0.24}; \\
\node {1.79}; \& \node{13.44};\& \node {2.37}; \& \node{0.00}; \\
\node {2.78}; \& \node{2.29}; \&\node {40.53}; \& \node{1.71}; \\
\node(a) {1.39}; \& \node(b){0.92};\& \node(c) {1.58}; \& \node(d){8.05}; \\
};
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\foreach \x/\xtext in {a/50.01,b/20.01,c/45.01,d/10.00}{
% line below last row
\draw ($(\x.south)-(0.3,0.1)$) -- ($(\x.south)+(0.3,-0.1)$);
% sums
\node[shift=(\x.south),yshift=-0.45cm](0,0) {\xtext} ;
}
\end{tikzpicture}
-
\left(
\begin{array}{llll}
80 & 0 & 0 & 0 \\
0 & 25 & 0 & 0 \\
0 & 0 & 40 & 0 \\
0 & 0 & 0 & 40 \\
\end{array}
\right)
\end{align*}
\end{document}
图形结果:
改进
也可以将图片的代码改成(最终输出相同):
\begin{tikzpicture}[baseline=-0.1cm]
\matrix(m)[matrix of math nodes,left delimiter=(,right delimiter=),inner sep=2.5pt,ampersand replacement=\&]
{
44.05 \& 3.36 \& 0.53 \& 0.24 \\
1.79 \& 13.44 \& 2.37 \& 0.00 \\
2.78 \& 2.29 \& 40.53 \& 1.71 \\
1.39 \& 0.92 \& 1.58 \& 8.05 \\
};
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\foreach \x/\xtext in {m-4-1/50.01,m-4-2/20.01,m-4-3/45.01,m-4-4/10.00}{
% line below last row
\draw ($(\x.south)-(0.3,0.1)$) -- ($(\x.south)+(0.3,-0.1)$);
% sums
\node[shift=(\x.south),yshift=-0.45cm](0,0) {\xtext} ;
}
\end{tikzpicture}
答案4
这是一个{pNiceMatrix}
使用nicematrix
(和)\cmidrule
的解决方案booktabs
。
\documentclass{article}
\usepackage{nicematrix}% for {pNiceMatrix}
\usepackage{booktabs}% for \cmirdule
\begin{document}
\begin{align*}
P^{\ast}_{t+1} &= P_{t+1} + D_{t,t+1} - B_{t,t+1} \\
&=
\left(
\begin{array}{rrrr}
1110 & 110 & 10 & 10 \\
45 & 440 & 45 & 0 \\
70 & 75 & 770 & 70 \\
35 & 30 & 30 & 330
\end{array}
\right) +
\begin{pNiceMatrix}[r,last-row,margin]
44.05 & 3.36 & 0.53 & 0.24 \\
1.79 & 13.44 & 2.37 & 0.00 \\
2.78 & 2.29 & 40.53 & 1.71 \\
1.39 & 0.92 & 1.58 & 8.05 \\
\cmidrule(lr){1-1}
\cmidrule(lr){2-2}
\cmidrule(lr){3-3}
\cmidrule(lr){4-4}
50.01 & 20.01 & 45.01 & 10.00 \\
\end{pNiceMatrix}
-
\left(
\begin{array}{rrrr}
80 & 0 & 0 & 0 \\
0 & 25 & 0 & 0 \\
0 & 0 & 40 & 0 \\
0 & 0 & 0 & 40
\end{array}
\right)
\end{align*}
\end{document}