突出显示数组块中的单行时出现的问题

突出显示数组块中的单行时出现的问题

如下图所示,我尝试通过突出显示数据透视表行和数据透视表列来创建一个单纯形表。但是,当我突出显示数据透视表行时,我遇到了一个问题,因为命令会填充数据透视表行及其下方的所有行。我该如何解决这个问题?

\documentclass{article}
\usepackage{amsmath}
\usepackage[table]{xcolor}

\usepackage{tikz}
 \newcommand\Circle[1]{%
   \tikz[baseline=(char.base)]\node[circle,draw,inner sep=2pt] (char) {#1};}

\begin{document}
 \begin{table}[h]
  \begin{equation*}
   \begin{array}{cc}
          \\
       & z \\ 
     0 & s_1 \\
     0 & s_2 \\
     0 & s_3 \\
     $-3$ & x_{1} \\
   \end{array}

   \begin{bmatrix}
    \begin{array}{cccccc|cc}
     x_1 & x_2 & s_1 & s_2 & s_3 & s_4 & b & \theta \\ \hline
     0 & -2 & 0 & 0 & 0 & 3 & 6 & \\ \hline
     \rowcolor{red!20}
     0 & \Circle{$2$} & 1 & 0 & 0 & -1 & 4 & 2 \\ 
     0 & 1 & 0 & 1 & 0 & -2 & 4 & 4 \\
     0 & 1 & 0 & 0 & 1 & 1 & 3 & 3\\
     1 & 0 & 0 & 0 & 0 & 1 & 2 & 0 \\ 
    \end{array}
   \end{bmatrix}
  \end{equation*}
 \end{table}
\end{document}

在此处输入图片描述

答案1

按列着色似乎更自然,这样可以控制彩色列的宽度。这是一个解决方案。我更改了一些参数来改善突出显示的视觉效果(至少符合我的口味……)

\documentclass{article}
\usepackage{amsmath}
\usepackage[table, x11names]{xcolor}

\usepackage{tikz}
 \newcommand\Circle[1]{%
   \tikz[baseline=(char.base)]\node[circle,draw,inner sep=1pt] (char) {#1};}

\begin{document}

 \begin{table}[h]
 \setlength\extrarowheight{2pt}
  \begin{equation*}
   \begin{array}{cc}
          \\
       & z \\
     0 & s_1 \\
     0 & s_2 \\
     0 & s_3 \\
     $-3$ & x_{1} \\
   \end{array}
%
   \begin{bmatrix}
    \begin{array}{c >{\columncolor{PaleVioletRed3!20}[0pt][0pt]}ccccc|cc}
     x_1 & \cellcolor{white}{x_2} & s_1 & s_2 & s_3 & s_4 & b & \theta \\ \hline
     0 & \cellcolor{white}{-2} & 0 & 0 & 0 & 3 & 6 & \\ \hline
     0 & \Circle{$2$} & 1 & 0 & 0 & -1 & 4 & 2 \\
     0 & 1 & 0 & 1 & 0 & -2 & 4 & 4 \\
     0 & 1 & 0 & 0 & 1 & 1 & 3 & 3\\
     1 & 0 & 0 & 0 & 0 & 1 & 2 & 0 \\
    \end{array}
   \end{bmatrix}
  \end{equation*}
 \end{table}

\end{document} 

在此处输入图片描述

对于row-and-column highlighting,似乎不可能使用colortabl, so I propose to make row-highlighting withpstricks . Roughly I add empty nodes at the beginning and at the end of the highlighting zone, and I connect them with an\ncbox` 命令同时执行这两项操作:

\documentclass{article}
\usepackage{amsmath}
\usepackage[table, x11names]{xcolor}

\usepackage{pst-node}
\usepackage{auto-pst-pdf}
\pagestyle{empty}
\begin{document}

 \begin{table}[h]
 \setlength\extrarowheight{2pt}
  \begin{equation*}
\begin{postscript}
   \begin{array}{cc}
          \\
       & z \\
     0 & s_1 \\
     0 & s_2 \\
     0 & s_3 \\
     $-3$ & x_{1} \\
   \end{array}
%
   \begin{bmatrix}
    \begin{array}{c >{\columncolor{PaleVioletRed3!20}[-0.7pt][-0.7pt]}ccccc|cc}
     x_1 & \cellcolor{white}{x_2} & s_1 & s_2 & s_3 & s_4 & b & \theta \\ \hline
     0 & \cellcolor{white}{-2} & 0 & 0 & 0 & 3 & 6 & \\ \hline
    \pnode{B}0 & \circlenode[linewidth=0.4pt, framesep=1pt]{P}{$2$} & 1 & 0 & 0 & -1 & 4 & 2\pnode{E} \\
    0 & 1 & 0 & 1 & 0 & -2 & 4 & 4 \\
     0 & 1 & 0 & 0 & 1 & 1 & 3 & 3\\
     1 & 0 & 0 & 0 & 0 & 1 & 2 & 0 \\
    \end{array}%
\ncbox[nodesep=3pt, boxsize=2.1ex, boxdepth=0.6ex, linestyle=none, fillstyle=solid, fillcolor=PaleVioletRed3, opacity=0.2]{B}{E}
   \end{bmatrix}%
\end{postscript}
  \end{equation*}
 \end{table}

\end{document} 

在此处输入图片描述

答案2

我找到了一个解决方案,但感觉它更像是一种黑客手段,而不是真正的解决方案。

 \documentclass{article}
 \usepackage{amsmath}
 \usepackage[table]{xcolor}

 \usepackage{tikz}
  \newcommand\Circle[1]{%
    \tikz[baseline=(char.base)]\node[circle,draw,inner sep=2pt] (char) {#1};}

 \begin{document}
   \begin{table}[h]
    \begin{equation*}
     \begin{array}{cc}
        \\
   & z \\ 
 0 & s_1 \\
 0 & s_2 \\
 0 & s_3 \\
 $-3$ & x_{1} \\
 \end{array}
 \begin{bmatrix}
  \begin{array}{c>{\columncolor{yellow!20}}ccccc|cc}
x_1 & x_2 & s_1 & s_2 & s_3 & s_4 & b & \theta \\ \hline
0 & -2 & 0 & 0 & 0 & 3 & 6 & \\ \hline
\rowcolor{red!20}
0 & \Circle{$2$} & 1 & 0 & 0 & -1 & 4 & 2 \\ 
\rowcolor{white}
0 & \cellcolor{yellow!20} 1 & 0 & 1 & 0 & -2 & 4 & 4 \\
0 & \cellcolor{yellow!20} 1 & 0 & 0 & 1 & 1 & 3 & 3\\
1 & \cellcolor{yellow!20} 0 & 0 & 0 & 0 & 1 & 2 & 0 \\ 
\end{array}
\end{bmatrix}
\end{equation*}
\end{table}
\end{document}

在此处输入图片描述

答案3

与。{NiceArray}nicematrix

\documentclass{article}
\usepackage{xcolor}
\usepackage{nicematrix}

\usepackage{tikz}
 \newcommand\Circle[1]{%
   \tikz[baseline=(char.base)]\node[circle,draw,inner sep=2pt] (char) {#1};}

\begin{document}

$\begin{NiceArray}{rccccccc|cl}[colortbl-like]
   &     & x_1 & x_2        & s_1 & s_2 & s_3 & s_4 & b & \theta\;\;\\
\cline{3-10}
   & z   & 0   & -2         & 0   & 0   & 0   & 3   & 6 &        \\
\cline{3-10}
0  & s_1 & \rowcolor{red!15}
           0   & \Circle{2} & 1   & 0   & 0   & -1  & 4 & 2      \\
0  & s_2 & 0   & 1          & 0   & 1   & 0   & -2  & 4 & 4      \\
0  & s_3 & 0   & 1          & 0   & 0   & 1   & 1   & 3 & 3      \\
-3 & x_1 & 1   & 0          & 0   & 0   & 0   & 1   & 2 & 0      \\
\CodeAfter \SubMatrix[{1-3}{6-10}][left-xshift=1.3mm]
\end{NiceArray}$

\end{document}

您需要多次编译(因为nicematrix在后台使用 PGF/Tikz 节点)。

上述代码的输出

相关内容