マトリックスの行または列の背景を変更する

マトリックスの行または列の背景を変更する

下図のようなマトリックスがあります。セルだけではなく、行全体を緑色にしたいと思います。

どうやってやるの?

\begin{equation}\label{eq:appendrow}
\newcommand\x{\times}
\newcommand\y{\colorbox{mygreen}{$1$}}
  \left(\begin{array}{cccc}
    \x  & \x  & \x & \x \\
    0   & \x  & \x & \x \\
    0   & 0   & \x & \x \\
    0   & 0   & 0  & \x \\
    \y  & \y  & \y & \y \\
  \end{array}\right)
\end{equation}

出力は次のようになります。

ここに画像の説明を入力してください

答え1

私の例では、パッケージを通じてロードされた、パッケージから\rowcolor、、\columncolorおよびを使用できます。\cellcolorcolortblxcolor

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

\newcommand\x{\times}
\newcommand\y{\cellcolor{green!10}}

\begin{document}

\begin{equation}\label{eq:appendrow}
  \left(\begin{array}{cccc}
    \rowcolor{red!20}
    \x  & \x  & \x & \x \\
    0   & \x  & \x & \x \\
   \rowcolor{blue!20}
    0   & 0   & \x & \x \\
    0   & 0   & 0  & \x \\
    \y a  &  b  & \y c &  d\\
  \end{array}\right)
\end{equation}

\begin{equation}
  \left(\begin{array}{>{\columncolor{olive!20}}cc>{\columncolor{yellow!20}}cc}
    \x  & \x  & \x & \x \\
    0   & \x  & \x & \x \\
    0   & 0   & \x & \x \\
    0   & 0   & 0  & \x \\
    a  & b  & c & d \\
  \end{array}\right)
\end{equation}

\end{document}

ここに画像の説明を入力してください

答え2

別の方法としては、hf-tikzパッケージ。

例:

\documentclass{article}
\usepackage{amsmath}

\newcommand\x{\times}

% requires version 0.3 of the package
\usepackage[customcolors]{hf-tikz}

\tikzset{style green/.style={
    set fill color=green!50!lime!60,
    set border color=white,
  },
  style cyan/.style={
    set fill color=cyan!90!blue!60,
    set border color=white,
  },
  style orange/.style={
    set fill color=orange!80!red!60,
    set border color=white,
  },
  hor/.style={
    above left offset={-0.15,0.31},
    below right offset={0.15,-0.125},
    #1
  },
  ver/.style={
    above left offset={-0.1,0.3},
    below right offset={0.15,-0.15},
    #1
  }
}

\begin{document}

\begin{equation}\label{eq:appendrow}
  \left(\begin{array}{cccc}
    \x  & \x  & \x & \x \\
    0   & \tikzmarkin[hor=style orange]{el} \x  & \x\tikzmarkend{el} & \x \\
    0   & 0   & \x & \x \\
    0   & 0   & 0  & \x \\
    \tikzmarkin[hor=style green]{row} a  &  b  &  c &  d \tikzmarkend{row}\\
  \end{array}\right)
\end{equation}

\begin{equation}\label{eq:appendcol}
  \left(\begin{array}{cccc}
    \tikzmarkin[ver=style cyan]{col 1}\x  & \x  & \tikzmarkin[ver=style green]{col 2} \x & \x \\
    0   & \x  & \x & \x \\
    0   & 0   & \x & \x \\
   0   & 0   & 0  & \x \\
    a \tikzmarkend{col 1}  &  b  &  c  \tikzmarkend{col 2} &  d \\
  \end{array}\right)
\end{equation}

\end{document}

結果:

ここに画像の説明を入力してください

答え3

{pNiceMatrix}は、マトリックスの一部を強調表示するために使用できるnicematrixコマンドがあります。\Block

\documentclass{article}
\usepackage{nicematrix}

\begin{document}

$\begin{pNiceMatrix}[margin]
a & b & c & d \\
\Block[fill=red!15,rounded-corners]{1-4}{}
a & b & c & d \\
a & b & c & d \\
a & b & \Block[draw,fill=blue!15,rounded-corners]{2-2}{}
        c & d \\
a & b & c & d \\
\end{pNiceMatrix}$

\end{document}

複数のコンパイルが必要です (nicematrix内部で PGF/Tikz ノードが使用されるため)。

上記コードの出力

答え4

バーの丸い境界線が重要でない場合は、カーラテックス私の回答を準備する上でベースとして使用される答えは次のとおりです。

\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{matrix}
\newcommand\x{\times}

\begin{document}
\begin{tikzpicture}
\matrix [matrix of math nodes,
         nodes={rectangle, %draw, very thin,
                minimum size=1.2em, text depth=0.25ex,
                inner sep=0pt, outer sep=0pt,
                fill opacity=0.5, text opacity=1,
                anchor=center},
         column sep=-0.5\pgflinewidth,
         row sep=-0.5\pgflinewidth,
         column 2/.append style = {nodes={fill=cyan!50}},
         row 2/.append style = {nodes={fill=cyan!50}},
         row 2 column 2/.append style={nodes={fill=cyan}},
         inner sep=0pt,
         left delimiter=(, right delimiter=),
         ]
{
a_1 & \x  & a_3 \\
a_4 & \x  & a_6 \\
a_7 & \x  & a_9 \\
};
\end{tikzpicture}
\end{document}

(まあ、行列はオリジナルではなく、私のいくつかの実験からのものです)、次のようになります。

ここに画像の説明を入力してください

補遺: 上記の回答には、いくつかの誤った前提が含まれています。(i) 透明度がどのように機能するか (ノードレベルでは意味がないので、そのような方法で使用しない方がよいでしょう)。(ii) 透明度は行 2 と列のスタイル定義で解決する必要がありました (以下の mwe を参照)。(iii) 塗りつぶしのみのノードは、エッジ ラインの幅で互いに重なってはならないため、sep=-0.5\pgflinewidth, row sep=-0.5\pgflinewidth削除する必要がありました。(iv) ノード定義の比較からカーラテックス回答と私の回答では、最近重要な違いに気づきました。ただし、エラーが発生するnodes={text width=.75em, text height=1.75ex, text depth=.5ex, align=center}フォームで使用しようとしています(削除するとエラーが削除されます)。nodes={minimum size=1.75ex, text depth=.5ex, align=center}align=center

比較のため、上記の最初の mwe はそのまま残しました。修正したものは次のとおりです。

\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{matrix}
\newcommand\x{\times}

\begin{document}   
\begin{tikzpicture}
\matrix [matrix of math nodes,
         nodes={rectangle,
                minimum size=1.5em, text depth=0.25ex,
                inner sep=0pt, outer sep=0pt,
                anchor=center},
         row 2/.append style = {nodes={preaction={fill=cyan!30}}},
         column 2/.append style = {nodes={fill=red!60},fill opacity=0.5, text opacity=1},
         inner sep=0pt,
         left delimiter=(, right delimiter=),
         ]
{
a_1 & \x  & a_3 \\
a_4 & \x  & a_6 \\
a_7 & \x  & a_9 \\
};
\end{tikzpicture}
\end{document}

これにより、次のようになります。

ここに画像の説明を入力してください

関連情報