0

From the solution code found here, I wanted to be able to also have a colored box around the x4 column like this:

enter image description here

I also looked at the solution posted here that gives different colors in rows/columns, but could not figure out how to implement their solution here.

Can you assist me in accomplishing this task? Thanks.

Here is the code:

\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}
Joe
  • 9,080

1 Answers1

1

You can fake it with [rounded corners=.7em] but would probably be better off drawing the shape. \pgfextractx and \pgfextracty compute the width and height of node (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}

matrix with highlights

John Kormylo
  • 79,712
  • 3
  • 50
  • 120
  • Sincerest Thanks for your solution! I could not draw the rounded rectangle (highlighted in cyan) properly, but how can I go about adding the rounded rectangle to the last column (after the |) to your elegant solution? Thanks again! – Joe Mar 04 '16 at 16:45
  • 1
    Just use \mytikzmark on the ends of the row or column to be highlighted assignning them names, then draw/fill the node or path around it. – John Kormylo Mar 04 '16 at 16:53