Matrix Zeilen- oder Spaltenhintergrund ändern

Matrix Zeilen- oder Spaltenhintergrund ändern

Ich habe eine Matrix, die unten dargestellt ist. Ich hätte gern die ganze Zeile grün und nicht nur die Zellen.

Wie kann ich das machen?

\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}

Die Ausgabe ist:

Bildbeschreibung hier eingeben

Antwort1

Sie können \rowcolor, \columncolor, und \cellcoloraus dem colortblPaket verwenden, das in meinem Beispiel über das xcolorPaket geladen wurde:

\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}

Bildbeschreibung hier eingeben

Antwort2

Ein anderer Ansatz könnte die Verwendung vonhf-tikzPaket.

Beispiel:

\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}

Ergebnis:

Bildbeschreibung hier eingeben

Antwort3

Innerhalb {pNiceMatrix}von nicematrixsteht Ihnen ein Befehl zur Verfügung \Block, mit dem Sie einen Teil der Matrix hervorheben können.

\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}

Sie benötigen mehrere Kompilierungen (da nicematrixim Hintergrund PGF/Tikz-Knoten verwendet werden).

Ausgabe des obigen Codes

Antwort4

Wenn die abgerundete Kante der Balken nicht unbedingt erforderlich ist, dann eine Alternative zuCarLaTeXDie Antwort, die als Grundlage für meine Antwort dient, lautet:

\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}

(also, die Matrix ist nicht original, sondern stammt aus einigen meiner Experimente), was ergibt:

Bildbeschreibung hier eingeben

Nachtrag: Die obige Antwort enthält einige falsche Annahmen: (i) wie Transparenz funktioniert (auf Knotenebene ergibt sie keinen Sinn, daher ist es besser, sie nicht auf diese Weise zu verwenden); (ii) Transparenz musste in Zeile 2 und Spaltenstildefinition aufgelöst werden (siehe mwe unten); (iii) Knoten mit nur Füllung sollten sich nicht hinsichtlich der Breite der Kantenlinien überlappen, daher sep=-0.5\pgflinewidth, row sep=-0.5\pgflinewidthmussten sie entfernt werden; (iv) aus dem Vergleich der Knotendefinition inCarLaTeXAntwort und mir ist vor kurzem ein wichtiger Unterschied aufgefallen: nodes={text width=.75em, text height=1.75ex, text depth=.5ex, align=center}aber ich versuche, es in der Form zu verwenden nodes={minimum size=1.75ex, text depth=.5ex, align=center}, in der align=centerein Fehler auftritt (durch Entfernen wird der Fehler behoben).

zum Vergleich habe ich das erste MWE oben so gelassen wie es ist. Das korrigierte ist:

\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}

Das Ergebnis ist:

Bildbeschreibung hier eingeben

verwandte Informationen