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

  • 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. – Andrew Apr 4 '18 at 21:39
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}
  • @JohnKormylo This is already fully compatible with \label, \ref and hyperref. I'll edit to show this. – Andrew Apr 4 '18 at 23:26
  • I was trying to use amsmath. It does all sorts of nasty things to \@currentlabel. – John Kormylo Apr 5 '18 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? – Filco28 Apr 5 '18 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. – Andrew Apr 6 '18 at 0:03
  • @Andrew Perfect! Thank you for an elegant solution. – Filco28 Apr 6 '18 at 17:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.