I'm taking the next step in my learning of LaTeX and starting to explore the possibility of creating custom environments; however, the resources I've consulted don't mention how to create an environment like the bmatrix environment that does not have a pre-defined number of arguments.

In particular, I'd like to define a variant of bmatrix (and similar) that allows me to colour the rows / columns.

Ideally, I'd like to figure out a way to define the environment so that I can:

  • specify the colour of each row / column at the opening of the environment (or at the first element of the row / column). E.g. if \ca, \cb and \cc are macros for different colours, then something like \begin{cbmatrix}{\ca \ca \cb} would start a bmatrix environment where the entries in the first row will each be wrapped in \ca, similarly for the second row, and the entries of the third row would be wrapped in cb,

i.e. the desired output would be equivalent to:

\ca{e1} & \ca{e2} & \ca{e3} & \ca{e4} \\
\ca{e5} & \ca{e6} & \ca{e7} & \ca{e8} \\
\cb{e9}& \cb{e10} & \cb{e11} & \cb{e12} \\
e13 & e14 & e15 & e16
  • easily switch between applying the colour command to rows or columns (perhaps with a starred version of the environment)?

I realize this is a very specific end-result, but I'm generally curious to know where to begin to be able to define this type of environment, where the number of inputs / arguments isn't predefined?

  • 2
    bmatrix has no arguments so the number of arguments is defined, if you mean thenumber of array columns that is also defined (to 10) it is essentially \begin{array}{*{10}{c}} Dec 10, 2019 at 22:09
  • 1
    There is a big difference between making settings at the column-level and the row-level. See, for example, An improved \rowstyle that takes the cell contents as an argument.
    – Werner
    Dec 10, 2019 at 22:09
  • @DavidCarlisle Yes, I meant the number of array columns (didn't know how to refer to the elements you place in the matrix). I didn't realize that bmatrix was limited to 10 (never had a larger array, I guess). So it just ignores unused columns?
    – Rax Adaam
    Dec 10, 2019 at 22:42
  • @Werner thank you, I will have a close look at that; I suppose the quick take-away is that I will have to create two separate environments... Perhaps I can name them similarly, so toggling between them achieves a similar effect.
    – Rax Adaam
    Dec 10, 2019 at 22:43
  • 1
    the 10 is a user-settable amsmath parameter, defaulting to 10, yes latex array and tabular always silently ignore unused columns Dec 10, 2019 at 22:43

1 Answer 1


Very fragile mixture of array, collcell, and some internal macros.



       \expandafter\def\csname rowcolor\number\value{row}\expandafter\endcsname\expandafter{\name}%
   \newcommand\colorrow{\csname rowcolor\number\value{row}\endcsname}%

    e1  & e2  & e3  & e4  \\
    e5  & e6  & e7  & e8  \\
    e9  & e10 & e11 & e12 \\
    e13 & e14 & e15 & e16 \\

enter image description here

  • thank you very much for this, I will study the solution & see if I can generalize away from it :) Did not expect such a quick & complete answer :) Why do you say it is a fragile mixture?
    – Rax Adaam
    Dec 10, 2019 at 22:39

You must log in to answer this question.

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