矩阵中两列分别着色

矩阵中两列分别着色

从解决方案代码来看找到这里,我希望能够在 x4 列周围有一个彩色框,如下所示:

在此处输入图片描述

我也看了发布的解决方案这里在行/列中给出不同的颜色,但无法弄清楚如何在这里实现它们的解决方案。

你能帮助我完成这个任务吗?谢谢。

以下是代码:

\documentclass{article}
\usepackage{xcolor}
\usepackage{tikz}
\usepackage{etoolbox}

\usetikzlibrary{arrows,matrix,positioning,fit,arrows.meta,}

\definecolor{ocre}{RGB}{0,173,239}

\tikzset{%
  highlight/.style={rectangle,rounded corners,fill=ocre!50,draw,
    fill opacity=0.5,thick,inner sep=0pt}
}
\newcommand{\tikzmark}[2]{\tikz[overlay,remember picture,
  baseline=(#1.base)] \node (#1) {#2};}
%
\newcommand{\Highlight}[1][submatrix]{%
    \tikz[overlay,remember picture]{
    \node[highlight,fit=(left.north west) (right.south east)] (#1) {};}
}
\newcommand{\mytikzmark}[2]{\tikz[overlay,remember picture, baseline=(#1.base)] \node (#1) {#2};}

\let\bbordermatrix\bordermatrix
\patchcmd{\bbordermatrix}{8.75}{4.75}{}{}
\patchcmd{\bbordermatrix}{\left(}{\left[}{}{}
\patchcmd{\bbordermatrix}{\right)}{\right]}{}{}

\newcommand{\wall}[2]{%
  \smash{%
    \vrule height \dimexpr\ht\strutbox+#1 depth \dimexpr\dp\strutbox+#2\relax
  }%
}

\begin{document}
\begin{equation}
\bbordermatrix{
 & x_{1} & x_{2} & x_{3}                 & x_{4} &                 &   \cr
 & 1     & 0     & \mytikzmark{left}{-1} & 0     & \wall{0pt}{3pt} & 0 \cr
 & 0     & 1     & 2                     & 0     & \wall{3pt}{3pt} & 0 \cr
 & 0     & 0     & \mytikzmark{right}{0} & 1     & \wall{3pt}{0pt} & 0 \cr
}
\Highlight[new1]
\end{equation}
\tikz[remember picture,overlay]{%
  \draw [LaTeX-] (new1.south) ++(0,-2.5pt) [out=-90,in=160] to ++(5mm,-10mm) node [right, xshift=-2.5mm, font=\itshape, text=red, align=center] {free\\variable};
}


\end{document}

答案1

您可以用它来伪造它[rounded corners=.7em],但最好绘制形状。 \pgfextractx\pgfextracty计算节点的宽度和高度(meany)

\documentclass{article}
\usepackage{xcolor}
\usepackage{tikz}
\usepackage{etoolbox}

\usetikzlibrary{arrows,matrix,positioning,fit,arrows.meta,tikzmark}

\definecolor{ocre}{RGB}{0,173,239}

\newcommand{\mytikzmark}[2]{\tikz[overlay,remember picture, baseline=(#1.base)] \node (#1) {#2};}

\let\bbordermatrix\bordermatrix
\patchcmd{\bbordermatrix}{8.75}{4.75}{}{}
\patchcmd{\bbordermatrix}{\left(}{\left[}{}{}
\patchcmd{\bbordermatrix}{\right)}{\right]}{}{}

\newcommand{\wall}[2]{%
  \smash{%
    \vrule height \dimexpr\ht\strutbox+#1 depth \dimexpr\dp\strutbox+#2\relax
  }%
}
\newlength{\tempx}
\newlength{\tempy}

\begin{document}
\begin{equation}
\bbordermatrix{
 & x_{1} & x_{2} & x_{3}                 & x_{4} &                 &   \cr
 & 1     & 0     & \mytikzmark{eeny}{-1} & \mytikzmark{meeny}{0}     & \wall{0pt}{3pt} & 0 \cr
 & 0     & 1     & 2                     & 0     & \wall{3pt}{3pt} & 0 \cr
 & 0     & 0     & \mytikzmark{miny}{0} & \mytikzmark{mo}{1}     & \wall{3pt}{0pt} & 0 \cr
}
\end{equation}

\begin{tikzpicture}[overlay,remember picture]
  \node[rectangle,rounded corners,fit=(eeny.north west) (miny.south east),
    fill=ocre!50,draw,fill opacity=0.5,thick,inner sep=0pt] (newname) {}; 
  \pgfextractx{\tempx}{\pgfpointdiff{\pgfpointanchor{meeny}{west}}{\pgfpointanchor{meeny}{east}}}
  \pgfextracty{\tempy}{\pgfpointdiff{\pgfpointanchor{meeny}{south}}{\pgfpointanchor{meeny}{north}}}
  \draw[thick,fill=green,fill opacity=0.5] (meeny.west) arc[x radius=0.5\tempx, y radius=0.5\tempy,start angle=180,end angle=0] -- (meeny.east)
     -- (mo.east) arc[x radius=0.5\tempx,y radius=0.5\tempy,start angle=0,end angle=-180] -- (mo.west) -- cycle;
  \draw [LaTeX-] (newname.south) ++(0,-2.5pt) [out=-90,in=160] to ++(5mm,-10mm)
    node [right, xshift=-2.5mm, font=\itshape, text=red, align=center] {free\\variable};
\end{tikzpicture}

\end{document}

带高亮显示的矩阵

相关内容