用红色标记围绕列和行绘制表格

用红色标记围绕列和行绘制表格

我如何在同一张桌子上绘制类似的红色标记,如下所示?: 所需画面

我使用了以下代码来绘制画面:

\begin{equation*}
\begin{array}{cc|cccccccc|c}
           &     & x_1 & x_2  &  x_3 & s_1 & s_2 & s_3  & a_2  &  a_3  &   \\
 \text{Basis} & c_B &  12 &   18 &   10 &  0  &  0  & 0    &   -M &   -M  & \text{RHS} \\ \hline
 s_1   &  0  & 2   &  3   &   4   &  1    & 0   & 0    &   0  &  0   &  50 \\
 a_2   & -M  & 1   &  -1  &  -1   &  0    & -1  &  0   &   1  &  0   &  0  \\
 a_3   & -M  & 0   & 1    & -1.5  &  0    & 0   &-1    &   0  &  1   &  0  \\ \hline
    \multicolumn{2}{c|}{z_{j}}     & -M  & -0   &  2.5M &  0    & M   &  M   &  -M  &  -M  &  0  \\
    \multicolumn{2}{c|}{c_{j}-z_{j}}    &12+M & 18   &10-2.5M&  0    & -M  &  0   &   0  &  0   &   \\
 \end{array}
\end{equation*}

我认为我可以使用 \hline 和 \vline 命令,但我不知道如何使用!或者我只是将其绘制为图片 :)

答案1

使用答案之一leandriis 的链接

\documentclass[fleqn]{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{tikzmark,fit}
\begin{document}
\begin{equation*}
\begin{array}{cc|cccccccc|c}
           &     & x_1 & x_2  &  x_3 & s_1 & s_2 & s_3  & a_2  &  a_3  &   \\
 \text{Basis} & c_B &  12 &   18 &   10 &  0  &  0  & 0    &   -M &   -M  & \text{RHS} \\ \hline
 s_1   &  0  & \tikzmarknode{2}{2}   &  3   &   4   &  1    & 0   & 0    &   0  &  0   &  50 \\
 a_2   & -M  & \tikzmarknode{1}{1}   &  -1  &  -1   &  0    & -1  &  0   &   1  &  0   &  \tikzmarknode{0'}{0}  \\
 a_3   & -M  & \tikzmarknode{0}{0}   & 1    & -1.5  &  0    & 0   &-1    &   0  &  1   &  0  \\ \hline
    \multicolumn{2}{c|}{z_{j}}     & -M  & -0   &  2.5M &  0    & M   &  M   &  -M  &  -M  &  0  \\
    \multicolumn{2}{c|}{c_{j}-z_{j}}    &\tikzmarknode{pr}{12+M} & 18   &10-2.5M&  0    & -M  &  0   &   0  &  0   &   \\
    \multicolumn{11}{c}{\vphantom{\uparrow}}
 \end{array}
 \begin{tikzpicture}[overlay,remember picture,red,semithick,
    pfit/.style={draw,inner sep=2pt,rounded
     corners=1pt,fit=#1}]
  \node[pfit=(1)(0'),inner xsep=1em](pf1){};
  \node[pfit=(2)(0),yshift=-1pt](pf2){};
  \draw[stealth-] (pf1.east) -- ++ (1em,0);
  \draw[stealth-] ([yshift=-2pt]pr.south) -- ++ (0,-1em);
\end{tikzpicture}
\end{equation*}
\end{document}

在此处输入图片描述

答案2

这是一种简单的方法pstricks:创建一些条目节点,并将它们连接起来\ncbox

\documentclass{article}

\usepackage{array}
\usepackage{mathtools}
\usepackage{pst-node} 

\begin{document}

\begin{equation*}
\renewcommand{\arraystretch}{1.2}
\begin{array}{cc|*{8}{c}|c}
           & & x_1 & x_2 & x_3 & s_1 & s_2 & s_3 & a_2 & a_3 & \\
 \text{Basis} & c_B & 12 & 18 & 10 & 0 & 0 & 0 & -M & -M & \text{RHS} \\ \hline
 s_1 & 0 & \Rnode{C}{2} & 3 & 4 & 1 & 0 & 0 & 0 & 0 & 50 \\
 a_2 & -M &\Rnode{A}{ 1 }& -1 & -1 & 0 & -1 & 0 & 1 & 0 &\Rnode{B}{0}\\
 a_3 & -M & \Rnode{D}{0} & 1 & -1.5 & 0 & 0 &-1 & 0 & 1 & 0 \\ \hline
    \multicolumn{2}{c|}{z_{j}} & -M & -0 & 2.5M & 0 & M & M & -M & -M & 0 \\
    \multicolumn{2}{c|}{c_{j}-z_{j}} &12+M & 18 &10-2.5M& 0 & -M & 0 & 0 & 0 & \\
 \end{array}
\psset{linewidth=1pt, linearc=0.1, linecolor=red, boxsize=0.7em}
\ncbox[nodesep=1.5em]{A}{B}\ncbox[nodesep=0.5ex]{C}{D}
\end{equation*}

\end{document} 

在此处输入图片描述

答案3

{NiceTabular}这是使用和 TikZ 的解决方案,使用数组中nicematrix创建的 TikZ 节点来绘制元素。nicematrix

\documentclass{article}
\usepackage{nicematrix,tikz}

\begin{document}

\begin{equation*}
\begin{NiceArray}{cc|cccccccc|c}
       &     & x_1 & x_2  &  x_3 & s_1 & s_2 & s_3  & a_2  &  a_3  &   \\
 \text{Basis} & c_B &  12 &   18 &   10 &  0  &  0  & 0    &   -M &   -M  & \text{RHS} \\ \hline
 s_1   &  0  & 2   &  3   &   4   &  1    & 0   & 0    &   0  &  0   &  50 \\
 a_2   & -M  & 1   &  -1  &  -1   &  0    & -1  &  0   &   1  &  0   &  0  \\
 a_3   & -M  & 0   & 1    & -1.5  &  0    & 0   &-1    &   0  &  1   &  0  \\ \hline
    \multicolumn{2}{c}{z_{j}}     & -M  & -0   &  2.5M &  0    & M   &  M   &  -M  &  -M  &  0  \\
    \multicolumn{2}{c}{c_{j}-z_{j}}    &12+M & 18   &10-2.5M&  0    & -M  &  0   &   0  &     &   \\
\CodeAfter
  \begin{tikzpicture} [red,thick]
    \draw ([xshift=6pt]4-|3) rectangle (5-|12) ; 
    \draw ([xshift=-3pt,yshift=1pt]3-3.north west) rectangle ([xshift=3pt,yshift=-2pt]5-3.south east) ;
    \draw [<-] (last-|3.5) -- ++ (0,-3mm) ; 
    \draw [<-] (4.5-|last) -- ++ (3mm,0) ; 
  \end{tikzpicture}
\end{NiceArray}
\end{equation*}

\end{document}

上述代码的输出

相关内容