8

I try to color the diagonal of a matrix.

\begin{equation}
    C=
    \begin{bmatrix}
    1 & 1 & 0\\
    0 & 1 & 1\\
    1 & 0 & 1\\
    \end{bmatrix}
\end{equation}

All I found was related to rows and columns (see. https://tex.stackexchange.com/a/69714/74926 and http://www.alecjacobson.com/weblog/?p=1289) Could someone may help.


I already found this. Sadly the line overlay the text and the brackets. Some further help would be great.

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}

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

\begin{document}
\[
\begin{pmatrix}
  \tikzmark{top}{1} & \tikzmark{top2}{1} & 0 \\
  0 & 1 & \tikzmark{bottom2}{1} \\
  \tikzmark{end}{1} & 0 & \tikzmark{bottom}{1}\\
\end{pmatrix}
\]

\begin{tikzpicture}[overlay,remember picture]
     \draw[red,line width=1mm,line cap=round] (top.north west) -- (bottom.south east);
     \draw[red,line width=1mm,line cap=round] (top2.north west) -- (bottom2.south east);
     \draw[red,line width=1mm,line cap=round] (end.north west) -- (end.south east);
\end{tikzpicture}
\end{document}

3 Answers3

10
\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}

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

\begin{document}
    \begin{equation}
\begin{pmatrix}
  \tikzmark{top}{1} & \tikzmark{top2}{1} & 0 \\
  0 & 1 & \tikzmark{bottom2}{1} \\
  \tikzmark{end}{1} & 0 & \tikzmark{bottom}{1}\\
\end{pmatrix}
    \end{equation}

\begin{tikzpicture}[overlay,remember picture]
     \draw[opacity=.4,line width=3mm,line cap=round] (top.center) -- (bottom.center);
     \draw[opacity=.4,line width=3mm,line cap=round] (top2.center) -- (bottom2.center);
     \draw[opacity=.4,line width=3mm,line cap=round] (end.center) -- (end.center);
\end{tikzpicture}
\end{document}

enter image description here

karlkoeller
  • 124,410
6

Welcome, you can use something like this:

\documentclass[a4paper]{article}
\usepackage{amsmath,amssymb,xcolor}
\newcommand\RED{\color{red}}
\begin{document}
\begin{equation}
    C=
    \begin{bmatrix}
      \RED 1 & 1 & 0\\
    0 & \RED 1 & 1\\
    1 & 0 & \RED 1\\
    \end{bmatrix}
\end{equation}
\end{document}

Result:

enter image description here

daleif
  • 54,450
1

You can do that with {pNiceMatrix} of nicematrix.

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

\begin{document} \begin{equation} \begin{pNiceMatrix} 1 & 1 & 0 \ 0 & 1 & 1 \ 1 & 0 & 1 \CodeAfter \tikz \draw [opacity=.4,line width=3mm,line cap=round] (1-1.center) -- (3-3.center) (1-2.center) -- (2-3.center) (3-1.center) -- (3-1.center) ; \end{pNiceMatrix} \end{equation}

\end{document}

Output of the above code

F. Pantigny
  • 40,250