2

Is there any way how to achieve that numbering for the matrices is displayed under the matrix itself and not to the right of it?

I use \equation environment for numbering and \multicols because I want to have the matrices side-by-side.

This is code I use:

\documentclass{article}

\usepackage{multicol}
\usepackage[table]{xcolor}

\begin{document}

\begin{multicols}{3}
    \noindent
    \begin{equation}
        \left(\begin{array}{cccc}
            \rowcolor{red!20}
            0 & 0 & 0 & 0 \\
            0 & 0 & 0 & 0 \\
            \rowcolor{blue!20}
            0 & 0 & 0 & 0 \\
            0 & 0 & 0 & 0 \\
            0 & 0 & 0 & 0 \\
        \end{array}\right)
    \end{equation}
    \begin{equation}
        \left(\begin{array}{>{\columncolor{olive!20}}cc>{\columncolor{yellow!20}}cc}
            0 & 0 & 0 & 0 \\
            0 & 0 & 0 & 0 \\
            0 & 0 & 0 & 0 \\
            0 & 0 & 0 & 0 \\
            0 & 0 & 0 & 0 \\
        \end{array}\right)
    \end{equation}
    \begin{equation}
        \left(\begin{array}{>{\columncolor{red!20}}cc>{\columncolor{b!20}}cc}
            0 & 0 & 0 & 0 \\
            0 & 0 & 0 & 0 \\
            0 & 0 & 0 & 0 \\
            0 & 0 & 0 & 0 \\
            0 & 0 & 0 & 0 \\
        \end{array}\right)
    \end{equation}
\end{multicols}

\end{document}

This is how it looks like: before

This is how I would like it to be: after

1
  • Welcome to TeX.SX! Can you please expand the code snippet that you have posted to a full minimal working example. A MWE should compile and be as small as possible to demonstrate your problem. it's much easier to help you if we have full working code to start from.
    – user30471
    Commented Apr 4, 2018 at 21:39

1 Answer 1

4

Edit II To fix the issues with row colours I have changed the environment to use gather from the amsmath package

By default equation numbers can be put on the left or the right but not underneath. To do this you'll need to place them there "by hand". The code below defines a new environment that does this for you. What it really does is put the equation inside an array with the equation in the first row and the equation number added as a second row to produce:

enter image description here

Edit I As the image shows, this is compatible with \label and \ref commands and with hyperref. The trick is that \refstepcounter is used to increment the equation counter.

Here is the full code:

\documentclass{article}
\usepackage{multicol}
\usepackage{hyperref}
\usepackage{environ}
\usepackage{amsmath}% needed for gather
\usepackage[table]{xcolor}
\NewEnviron{Equation}{\refstepcounter{equation}%
  \begin{gather*}\BODY\\(\theequation)\end{gather*}%
}

\begin{document}

  \begin{multicols}{3}
      \noindent
      \begin{Equation}\label{one}
          \left(\begin{array}{>{\columncolor{olive!20}}cc>{\columncolor{yellow!20}}cc}
            \rowcolor{red!20}
            0 & 0 & 0 & 0 \\
            0 & 0 & 0 & 0 \\
            \rowcolor{blue!20}
            0 & 0 & 0 & 0 \\
            0 & 0 & 0 & 0 \\
            0 & 0 & 0 & 0 \\
          \end{array}\right)
      \end{Equation}
      \begin{Equation}\label{two}
          \left(\begin{array}{>{\columncolor{olive!20}}cc>{\columncolor{yellow!20}}cc}
              0 & 0 & 0 & 0 \\
              0 & 0 & 0 & 0 \\
              0 & 0 & 0 & 0 \\
              0 & 0 & 0 & 0 \\
              0 & 0 & 0 & 0 \\
          \end{array}\right)
      \end{Equation}
      \begin{Equation}
          \left(\begin{array}{>{\columncolor{red!20}}cc>{\columncolor{olive!20}}cc}
              0 & 0 & 0 & 0 \\
              0 & 0 & 0 & 0 \\
              0 & 0 & 0 & 0 \\
              0 & 0 & 0 & 0 \\
              0 & 0 & 0 & 0 \\
          \end{array}\right)
      \end{Equation}
  \end{multicols}
  Look at \ref{one} and \ref{two}.

\end{document}

Edit III Here's a version that uses tikz to colour the rows and columns via a "helper environment" ColouredMatrix. The output is similar:

enter image description here

The main advantage of this approach is that it is marginally easier to specify the row and column colours. The downsize is that you need to use \& as the column separator. Here is the code:

\documentclass{article}
\usepackage{multicol}
\usepackage{hyperref}
\usepackage{environ}
\usepackage{amsmath}
\usepackage[table]{xcolor}
\NewEnviron{Equation}{\refstepcounter{equation}%
  \begin{gather*}\BODY\\(\theequation)\end{gather*}%
}

\usepackage{tikz}
\usetikzlibrary{matrix}
\tikzset{
% short hands for colouring the rows and columns of the matrix
  coloured row/.style args={#1,#2}{row #1/.style={nodes={fill=#2!20}}},
  coloured col/.style args={#1,#2}{column #1/.style={nodes={fill=#2!20}}},
}

%usage: \begin{ColouredMatrix}[coloured row={row colour}, ...]\end{ColouredMatrix}
\NewEnviron{ColouredMatrix}[1][]{%
  \tikzpicture[% adjust the size and placement of the delimiters as they are too big by default
       every left delimiter/.style={xshift=0.65em,scale=0.9,yshift=0.4em},
       every right delimiter/.style={xshift=-0.65em,scale=0.9,yshift=0.4em}]
    \matrix [matrix of math nodes, left delimiter=(, right delimiter=),
             ampersand replacement=\&, #1]
    { \BODY };
  \endtikzpicture%
}

\begin{document}

  \begin{multicols}{3}
      \noindent
      \begin{Equation}\label{one}
        \begin{ColouredMatrix}[%
          coloured row={1,red}, coloured row={3,blue},
          coloured col={1,olive},coloured col={4,yellow}]
            0 \& 0 \& 0 \& 0 \\
            0 \& 0 \& 0 \& 0 \\
            0 \& 0 \& 0 \& 0 \\
            0 \& 0 \& 0 \& 0 \\
            0 \& 0 \& 0 \& 0 \\
         \end{ColouredMatrix}
      \end{Equation}
      \begin{Equation}\label{two}
        \begin{ColouredMatrix}[%
           coloured col={1,olive}, coloured col={4,yellow}]
              0 \& 0 \& 0 \& 0 \\
              0 \& 0 \& 0 \& 0 \\
              0 \& 0 \& 0 \& 0 \\
              0 \& 0 \& 0 \& 0 \\
              0 \& 0 \& 0 \& 0 \\
         \end{ColouredMatrix}
      \end{Equation}
      \begin{Equation}
        \begin{ColouredMatrix}[%
           coloured col={1,red}, coloured row={3,green}]
              0 \& 0 \& 0 \& 0 \\
              0 \& 0 \& 0 \& 0 \\
              0 \& 0 \& 0 \& 0 \\
              0 \& 0 \& 0 \& 0 \\
              0 \& 0 \& 0 \& 0 \\
         \end{ColouredMatrix}
      \end{Equation}
  \end{multicols}
  Look at \ref{one} and \ref{two}.

\end{document}
5
  • @JohnKormylo This is already fully compatible with \label, \ref and hyperref. I'll edit to show this.
    – user30471
    Commented Apr 4, 2018 at 23:26
  • I was trying to use amsmath. It does all sorts of nasty things to \@currentlabel. Commented Apr 5, 2018 at 13:08
  • @Andrew Now I am unable to highlight the column and the row in the same matrix (because the row colours need manual reset and it overrides the column colour). Any idea what the problem might be?
    – Filip N.
    Commented Apr 5, 2018 at 17:08
  • @Filco28 I have changed the code to use the gather environment from the amsmath package. This fixes the issues with row colours. In particular, it is no longer necessary to reset the row colours from one line to the next. I have also added a tikz approach to colouring the rows and colours.
    – user30471
    Commented Apr 6, 2018 at 0:03
  • @Andrew Perfect! Thank you for an elegant solution.
    – Filip N.
    Commented Apr 6, 2018 at 17:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .