Math & ScienceBox around matrix entries

Information and discussion about LaTeX's math and science related features (e.g. formulas, graphs).
Post Reply
dearleader
Posts: 3
Joined: Fri Nov 27, 2009 10:19 pm

Box around matrix entries

Post by dearleader »

Hello,

I am trying to put coloured boxes around certain minors of an nxn matrix, but am unable to find anywhere on the web how to do this, and I was therefore hoping someone may have a suggestion on how to do this. Thank you in advance,

Jong-il

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Box around matrix entries

Post by localghost »

This is a job for the colortbl package. But nowadays it should be loaded by the xcolor package with the according option. Consider the following example.

Code: Select all

\documentclass[english]{scrartcl}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{babel}
\usepackage{amsmath}
\usepackage[table]{xcolor}

\begin{document}
  \[
    \begin{pmatrix}
      \cellcolor{gray} 1 & 0 & 0 \\
      0 & \cellcolor{gray} 1 & 0 \\
      0 & 0 & \cellcolor{gray} 1
    \end{pmatrix}
  \]

  \[
    \left(
    \begin{array}{ccc}
      \cellcolor{gray} 1 & 0 & 0 \\
      0 & \cellcolor{gray} 1 & 0 \\
      0 & 0 & \cellcolor{gray} 1
    \end{array}
    \right)
  \]
\end{document}
Usually for matrices there is the (x)matrix environment from amsmath. But in this case it reveals a flaw. So it is better to use the array environment with some appropriate additions.

Further information about the packages as well as their manuals can be found on CTAN.


Best regards and welcome to the board
Thorsten
dearleader
Posts: 3
Joined: Fri Nov 27, 2009 10:19 pm

Re: Box around matrix entries

Post by dearleader »

Thank you very much for your help. I had however, another small issue which I cannot find the solution to and I was wondering if you know a solution to this by any chance:

After colouring several separate cells, I would like to put a frame around them. i.e. in your matrix I would like to colour the cells in the minor consisting of the entries in the 1st and 2nd row and column, and then put a frame around these 4 entries.

Thank you in advance,

Jong-il
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Box around matrix entries

Post by localghost »

I think you should decide for one or the other method to mark the minor A(33) because the combination of both doesn't work proper. Look at the code below.

Code: Select all

\documentclass[english]{scrartcl}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{babel}
\usepackage{amsmath}
\usepackage[table]{xcolor}

\begin{document}
  \[
    \left(
    \begin{array}{ccc}
      \cellcolor{gray!25} a_{11} & \cellcolor{gray!25} a_{12} & a_{13} \\
      \cellcolor{gray!25} a_{21} & \cellcolor{gray!25} a_{22} & a_{23} \\
      a_{31} & a_{32} & a_{33}
    \end{array}
    \right)
  \]

  \[
    \left(
    \begin{array}{ccc}\cline{1-2}
      \multicolumn{1}{|c}{a_{11}} & \multicolumn{1}{c|}{a_{12}} & a_{13} \\
      \multicolumn{1}{|c}{a_{21}} & \multicolumn{1}{c|}{a_{22}} & a_{23} \\ \cline{1-2}
      a_{31} & a_{32} & a_{33}
    \end{array}
    \right)
  \]

  \[
    \left(
    \begin{array}{ccc}\cline{1-2}
      \multicolumn{1}{|c}{\cellcolor{gray!25} a_{11}} & \multicolumn{1}{c|}{\cellcolor{gray!25} a_{12}} & a_{13} \\
      \multicolumn{1}{|c}{\cellcolor{gray!25} a_{21}} & \multicolumn{1}{c|}{\cellcolor{gray!25} a_{22}} & a_{23} \\ \cline{1-2}
      a_{31} & a_{32} & a_{33}
    \end{array}
    \right)
  \]
\end{document}
At the moment I have no solution for this issue
Post Reply