我制作了一个边框矩阵,用方括号 [] 代替 (),现在顶部和左侧有这些索引。在我的例子中,顶部的索引是 u_1、v_1、u_2、v_2 等等。左侧的索引是 F_1x、F_1y、F_2x、F_2y。现在我想让所有“u_i”索引都变成蓝色,所有“v_i”索引都变成绿色。左侧也一样,所有“F_ix”都变成蓝色,所有“F_iy”都变成绿色。我的解决方案看起来像这样并且可以工作,但如果矩阵有更多行和线,那么代码就太多了。
\bordermatrix{~ & \color{blue!80!black} \bf \overline {u}_1
& \color{green!80!black} \bf \overline {v}_1
& \color{blue!80!black} \bf \overline {u}_5
& \color{green!80!black} \bf \overline {v}_5 \cr
\color{blue!80!black} \bf \overline {F}_{1x} & 336000 & 0 & -336000 & 0 \cr
\color{green!80!black} \bf \overline {F}_{1y} & 0 & 0 & 0 & 0 \cr
\color{blue!80!black} \bf \overline {F}_{5x} & -336000 & 0 & 336000 & 0 \cr
\color{green!80!black} \bf \overline {F}_{5y} & 0 & 0 & 0 & 0 \cr
}
有没有更短、更优雅的方法来达到相同的效果?
答案1
使用一些命令可以更轻松地完成工作;该示例还包括使用blkarray
包裹:
\documentclass{article}
\usepackage{xcolor}
\usepackage{amsmath}
\usepackage{blkarray}
\newcommand\Gmat[2]{%
\color{green!80!black}\mathbf{\overline{#1}_{#2}}}
\newcommand\Bmat[2]{%
\color{blue!80!black}\mathbf{\overline{#1}_{#2}}}
\begin{document}
\[
\bordermatrix{
~ & \Bmat{u}{1} & \Gmat{v}{1} & \Bmat{u}{5} & \Gmat{v}{5} \cr
\Bmat{F}{1x} & 336000 & 0 & -336000 & 0 \cr
\Gmat{F}{1y} & 0 & 0 & 0 & 0 \cr
\Bmat{F}{5x} & -336000 & 0 & 336000 & 0 \cr
\Gmat{F}{5y} & 0 & 0 & 0 & 0 \cr
}
\]
\[
\begin{blockarray}{ccccc}
~ & \Bmat{u}{1} & \Gmat{v}{1} & \Bmat{u}{5} & \Gmat{v}{5} \\
\begin{block}{c[cccc]}
\Bmat{F}{1x} & 336000 & 0 & -336000 & 0 \\
\Gmat{F}{1y} & 0 & 0 & 0 & 0 \\
\Bmat{F}{5x} & -336000 & 0 & 336000 & 0 \\
\Gmat{F}{5y} & 0 & 0 & 0 & 0 \\
\end{block}
\end{blockarray}
\]
\end{document}
答案2
以下是利用 的环境来实现此目的的一种方法nicematrix
。
\documentclass{article}
\usepackage{xcolor}
\usepackage{amsmath}
\usepackage{nicematrix}
\begin{document}
\newcommand\mat[2]{\mathbf{\overline{#1}_{#2}}}
\NiceMatrixOptions
{
code-for-first-row = \ifodd \value{jCol} \color{blue} \else \color{green} \fi ,
code-for-first-col = \ifodd \value{iRow} \color{blue} \else \color{green} \fi
}
\[\begin{pNiceMatrix}[first-row,first-col]
& \mat{u}{1} & \mat{v}{1} & \mat{u}{5} & \mat{v}{5} \\
\mat{F}{1x} & 336000 & 0 & -336000 & 0 \\
\mat{F}{1y} & 0 & 0 & 0 & 0 \\
\mat{F}{5x} & -336000 & 0 & 336000 & 0 \\
\mat{F}{5y} & 0 & 0 & 0 & 0 \\
\end{pNiceMatrix}\]
\[\begin{bNiceMatrix}[first-row,first-col]
& \mat{u}{1} & \mat{v}{1} & \mat{u}{5} & \mat{v}{5} \\
\mat{F}{1x} & 336000 & 0 & -336000 & 0 \\
\mat{F}{1y} & 0 & 0 & 0 & 0 \\
\mat{F}{5x} & -336000 & 0 & 336000 & 0 \\
\mat{F}{5y} & 0 & 0 & 0 & 0 \\
\end{bNiceMatrix}\]
\end{document}