Ordering Numbers with Nicematrix Package
Ordering Numbers with Nicematrix Package
F. Pantigny
fpantigny@[Link]
October 23, 2025
Abstract
The LaTeX package nicematrix provides new environments similar to the classical environ-
ments {tabular}, {array} and {matrix} of array and amsmath but with extended features.
dimensions (cm)
Price
C1 C2 Cn Product
L l h
L1 a11 a12 a1n
L2 a21
a22 a2n
small 3 5.5 1 30
standard 5.5 8 1.5 50.5
premium
Ln an1 an2 ann 8.5 10.5 2 80
extra 8.5 10 1.5 85.5
special 12 12 0.5 70
The package nicematrix is entirely contained in the file [Link]. This file may be put in the
current directory or in a texmf tree. However, the best is to install nicematrix with a TeX distribution
such as MiKTeX, TeX Live or MacTeX.
Remark
If you use LaTeX via Internet with, for example, Overleaf of TeXPage, you can upload the file
[Link] in the repertory of your project in order to take full advantage of the latest version
de nicematrix.1
This package can be used with xelatex, lualatex, pdflatex but also by the classical workflow
latex-dvips-ps2pdf (or Adobe Distiller). However, the file [Link] of the present documen-
tation should be compiled with LuaLaTeX.
This package requires and loads the packages array, amsmath, pgfcore and the module shapes of pgf
(tikz, which is a layer over pgf, is not loaded). The final user only has to load the package with
\usepackage{nicematrix}.
The idea of nicematrix is to create pgf nodes under the cells and the positions of the rules of the
tabular created by array and to use these nodes to develop new features. As usual with pgf, the
coordinates of these nodes are written in the aux to be used on the next compilation and that’s why
nicematrix may need several compilations2 . One must not use the command \nofiles (which
prevents the creation of the aux file).
Most features of nicematrix may be used without explicit use of pgf or TikZ (which, in fact, is not
loaded by default).
A command \NiceMatrixOptions is provided to fix the options (the scope of the options fixed by
this command is the current TeX group: they are semi-global).
∗ This document corresponds to the version 7.4 of nicematrix, at the date of 2025/10/23.
1 The latest version of the file [Link] may be downloaded on the Github depot of nicematrix:
[Link]
2 If you use Overleaf, Overleaf will do automatically a sufficient number of compilations.
1
1 The environments of this package
It’s well known that some rows of the arrays created by default with LaTeX are, by default, too close
to each other. Here is a classical example.
$\begin{pmatrix}
\frac{1}{2} & -\frac{1}{2} \\
1
− 12
2
\frac{1}{3} & \frac{1}{4} \\
1 1
3 4
\end{pmatrix}$
Inspired by the package cellspace which deals with that problem, the package nicematrix provides two
keys cell-space-top-limit and cell-space-bottom-limit similar to the parameters of cellspace
called \cellspacetoplimit and \cellspacebottomlimit.
There is also a key cell-space-limits to set both parameters at once.
The initial value of these parameters is 0 pt in order to have for the environments of nicematrix the
same behaviour as those of array and amsmath. However, a value of 1 pt would probably be a good
choice and we suggest to set them with \NiceMatrixOptions.5
\NiceMatrixOptions{cell-space-limits = 1pt}
3 In fact, it’s possible to use directly the X columns in the environment {NiceTabular} (and the required width for
creation of the PGF/TikZ nodes for the contents of the cells but some functionalities won’t work any longer (cf. 53).
5 One should remark that these parameters apply also to the columns of type S of siunitx whereas the package
2
$\begin{pNiceMatrix} !
1
\frac12 & -\frac12 \\ 2 − 12
\frac13 & \frac14 \\ 1
3
1
4
\end{pNiceMatrix}$
It’s also possible to change these parameters for only a few rows by using the command \RowStyle
provided by nicematrix (cf. p. 26).
The package nicematrix provides a option baseline for the vertical position of the arrays. This
option takes in as value an integer which is the number of the row on which the array will be aligned.
$A = \begin{pNiceMatrix}[baseline=2] 1
p 1−p
p
\frac{1}{\sqrt{1+p^2}} & p & 1-p \\ 1+p2
1 & 1 & 1 \\ A= 1 1 1
1 & p & 1+p 1 p 1+p
\end{pNiceMatrix}$
It’s also possible to use the option baseline with one of the special values t, c or b. These letters
may also be used absolutely like the option of the environments {tabular} and {array} of array.
The initial value of baseline is c.
In the following example, we use the option t (equivalent to baseline=t) immediately after an \item
of list. One should remark that the presence of a \hline at the beginning of the array doesn’t prevent
the alignment of the baseline with the baseline of the first row (with {tabular} or {array} of array,
one must use \firsthline).
\begin{enumerate}
\item an item
\smallskip
\item \renewcommand{\arraystretch}{1.2} 1. an item
$\begin{NiceArray}[t]{lcccccc}
\hline 2. n 0 1 2 3 4 5
n & 0 & 1 & 2 & 3 & 4 & 5 \\ un 1 2 4 8 16 32
u_n & 1 & 2 & 4 & 8 & 16 & 32
\hline
\end{NiceArray}$
\end{enumerate}
However, it’s also possible to use the tools of booktabs6 : \toprule, \bottomrule, \midrule, etc.
\begin{enumerate}
\item an item
\smallskip
\item
1. an item
$\begin{NiceArray}[t]{lcccccc}
\toprule
2. n 0 1 2 3 4 5
n & 0 & 1 & 2 & 3 & 4 & 5 \\
\midrule un 1 2 4 8 16 32
u_n & 1 & 2 & 4 & 8 & 16 & 32
\bottomrule
\end{NiceArray}$
\end{enumerate}
3
It’s also possible to use the key baseline to align a matrix on an horizontal rule (drawn by \hline).
In this aim, one should give the value line-i where i is the number of the row following the horizontal
rule.
\NiceMatrixOptions{cell-space-limits=1pt}
$A=\begin{pNiceArray}{cc|cc}[baseline=line-3]
\dfrac{1}{A} & \dfrac{1}{B} & 0 & 0 \\ 1 1
0 0
\dfrac{1}{C} & \dfrac{1}{D} & 0 & 0 \\ A
1 B
\hline 1
0 0
0 & 0 & A & B \\ C D
A=
0
0 A B
0 & 0 & D & D \\
0 0 D D
\end{pNiceArray}$
4 The blocks
• The first argument is the size of the block with the syntax i-j where i is the number of rows of
the block and j its number of columns.
If this argument is empty, its default value is 1-1. If the number of rows is not specified, or
equal to *, the block extends until the last row (idem for the columns).
• The second argument is the content of the block. In {NiceTabular}, {NiceTabular*} and
{NiceTabularX}, the content of the block is composed in text mode whereas, in the other
environments, it is composed in math mode.
One may wish to raise the size of the “A” placed in the block of the previous example. Since this
element is composed in math mode, it’s not possible to use directly a command like \large, \Large
and \LARGE. That’s why the command \Block provides an option between angle brackets to specify
some TeX code which will be inserted before the beginning of the math mode.8
4
$\begin{bNiceArray}{cw{c}{1cm}c|c}[margin]
\Block{3-3}<\Large>{A} & & & 0 \\
0
0 & & & \Vdots \\
& & & 0 \\
A
\hline
0
0 & \Cdots& 0 & 0 0 0 0
\end{bNiceArray}$
In fact, the command \Block accepts as first optional argument (between square brackets) a list of
pairs key=value.
First, there are keys which are quick tools to control the appearance of the block.
• the key fill takes in as value a color and fills the block with that color;
• the key opacity sets the opacity of the filling color specified by fill;9
• the key draw takes in as value a color and strokes the frame of the block with that color (the
default value of that key is the current color of the rules of the array);
• the key color takes in as value a color and apply that color the content of the block but draws
also the frame of the block with that color;
• the keys hlines, vlines and hvlines draw all the corresponding rules in the block;10
• the key line-width is the width of the rules (is relevant only when one of the keys draw,
hvlines, vlines and hlines is used);
• the key rounded-corners requires rounded corners (for the frame drawn by draw and the shape
drawn by fill) with a radius equal to the value of that key (the default value is 4 pt11 ).
Sometimes, these tools are not sufficient to control the appearance of the block. The following keys
are more powerful but also more difficult to use. Moreover, the require the loading of TikZ by the
user (with \usepackage{tikz}). By default, nicematrix does not load TikZ but only pgf, which is a
sublayer of TikZ.
• The key borders provides the ability to draw only some borders of the blocks; the value of
that key is a (comma-separated) list of elements covered by left, right, top and bottom;
it’s possible, in fact, in the list which is the value of the key borders, to add an entry of
the form tikz={list} where list is a list of couples key=value of TikZ specifying the graphical
characteristics of the lines that will be drawn (for an example, see p. 68).
• When the key tikz is used, the TikZ path corresponding of the rectangle which delimits the
block is executed with TikZ12 by using as options the value of that key tikz (which must be a
list of keys allowed for a TikZ path).
In fact, in the list of the keys provided by the user as value of tikz, it’s possible to put a key
offset . That key is not provided by TikZ but by nicematrix. It will narrow the rectangular
frame corresponding to the block by a margin (horizontally and vertically) equal to the value
(of that key offset). That new frame, a bit narrower, will be executed by TikZ with options
which are the other keys in the list of keys provided as value to the key tikz of \Block.
For examples, cf. p. 62.
9 Caution: that feature creates instructions of transparency in the pdf and some readers of pdf don’t support such
instructions.
10 However, the rules are not drawn in the sub-blocks of the block, as always with nicematrix: the rules are not drawn
in the blocks, except when they have the key transparent (cf. section 5 p. 11).
11 This value is the initial value of the rounded corners of TikZ.
12 TikZ should be loaded (by default, nicematrix only loads pgf) and, if it’s not, an error will be raised.
5
• the key name provides a name to the rectangular TikZ node corresponding to the block; it’s
possible to use that name with TikZ in the \CodeAfter of the environment (cf. p. 38);
There is also keys for the horizontal and vertical positions of the content of the block: cf. 4.5 p. 7.
One must remark that, by default, the commands \Blocks don’t create space. There
is exception only for the blocks mono-column and the blocks mono-row under some conditions as
explained just below.
In the following example, we have had to enlarge by hand the columns 2 and 3 (with the construction
w{c}{...} of array).
\begin{NiceTabular}{cw{c}{2cm}w{c}{3cm}c}
rose & tulip & daisy & dahlia \\
violet
..& \Block[draw=red,fill=[RGB]{204,204,255},rounded-corners]{2-2}
..........{\LARGE Some beautiful flowers} & & marigold \\
...& & marigold \\
iris & & & lis \\
arum & periwinkle & forget-me-not & hyacinth
\end{NiceTabular}
• The natural width of the contents of these blocks is taken into account for the width of the
current column.
In the columns with a fixed width (columns w{...}{...}, W{...}{...}, p{...}, b{...},
m{...}, V and X), the content of the block is formatted as a paragraph of that width.
• The specification of the horizontal position provided by the type of column (c, r or l) is taken
into account for the blocks. For a block in a column ot type p{...}, b{...}, m{...}, V{...}
ou X, the alignment c will be used by default. However, those types of columns may have an
optional argument for the horizontal alignment (eg: p[l]{...}) and, in that case, that type of
alignement is passed to the block.
Of course, the \Block may also have its own specification of alignment: cf. 4.5 p. 7.
• The specifications of font specified for the column by a construction >{...} in the preamble of
the array are taken into account for the mono-column blocks of that column (this behaviour is
probably expected).
6
\begin{NiceTabular}{@{}>{\bfseries}lr@{}} \hline
\Block{2-1}{John} & 12 \\ 12
John
.....................& 13 \\ \hline 13
Steph & 8 \\ \hline Steph 8
\Block{3-1}{Sarah} & 18 \\ 18
.....................& 17 \\ Sarah 17
.....................& 15 \\ \hline 15
Ashley & 20 \\ \hline Ashley 20
Henry & 14 \\ \hline Henry 14
\Block{2-1}{Madison} & 15 \\ 15
Madison
.....................& 19 \\ \hline 19
\end{NiceTabular}
13 If one simply wishes to color the background of a unique cell, there is no point using the command \Block: it’s
7
\begin{NiceTabular}{@{}c!{\qquad}ccc!{\qquad}ccc@{}}
\toprule
Rank & \Block{1-3}{First group} & & & \Block{1-3}{Second group} \\
.....& 1A & 1B & 1C & 2A & 2B & 2C \\
\midrule
.1 & 0.657 & 0.913 & 0.733 & 0.830 & 0.387 & 0.893\\
.2 & 0.343 & 0.537 & 0.655 & 0.690 & 0.471 & 0.333\\
.3 & 0.783 & 0.885 & 0.015 & 0.306 & 0.643 & 0.263\\
.4 & 0.161 & 0.708 & 0.386 & 0.257 & 0.074 & 0.336\\
\bottomrule
\end{NiceTabular}
In order to have an horizontal positioning of the content of the block computed with the limits of the
columns of the LaTeX array (and not with the contents of those columns), one may use the key L ,
R and C of the command \Block.14
Here is the same example with the key C for the first block.
\begin{NiceTabular}{@{}c!{\qquad}ccc!{\qquad}ccc@{}}
\toprule
Rank & \Block[C]{1-3}{First group} & & & \Block{1-3}{Second group} \\
.....& 1A & 1B & 1C & 2A & 2B & 2C \\
\midrule
.1 & 0.657 & 0.913 & 0.733 & 0.830 & 0.387 & 0.893\\
.2 & 0.343 & 0.537 & 0.655 & 0.690 & 0.471 & 0.333\\
.3 & 0.783 & 0.885 & 0.015 & 0.306 & 0.643 & 0.263\\
.4 & 0.161 & 0.708 & 0.386 & 0.257 & 0.074 & 0.336\\
\bottomrule
\end{NiceTabular}
The command \Block supports also the keys p and j. With the key p , the content of the block is
formatted like a paragraph (as in a column of type p). That key may be used in conjunction with
a key l, c or r, and, in that case, the paragraph is formatted with \raggedright, \centering or
\raggedleft (or \RaggedRight, \Centering and \RaggedLeft when ragged2e) est chargée. With
the key j (which inforces the key p), the paragraph is justified.
14 Remark: The keys L, C and R require less computations than the keys l, c and r. If you are concernend about
8
It’s possible to put an environment {itemize} or {enumerate} in a block which uses the key p or
j (in the other case, you will have a LaTeX error: Not allowed in LR mode). For the following
example, we have loaded the package enumitem (for the key left of the environment {itemize}).
\begin{NiceTabular}[hvlines]{ccc}
one & two two & three three \\
one &
\Block[p]{*-2}{%
\begin{itemize}[left=0pt]
\item one two three four five
\item two
\item three
\end{itemize}
} \\
one & \\
one & \\
one & \\
one & \\
one & \\
one & \\
\end{NiceTabular}
• With the key t , the baseline of the content of the block is aligned with the baseline of the first
row concerned by the block.
• with the key b, the baseline of the last row of the content of the block (we recall that the
content of a block may contain several lines of text separated by \\) is aligned with the baseline
of the last of the rows of the array involved in the block.
When no key is given, the key m applies (except in the mono-row blocks).
\NiceMatrixOptions{rules/color=[gray]{0.75}, hvlines}
9
Un
\begin{NiceTabular}{ccc}
\Block[fill=red!10,t,l]{4-2}{two\\lines} two
.& & \Huge Un\\ lines deux
.& & deux \\ trois
.& & trois \\
.& & \Huge quatre \\
text & text \\
quatre
text text
\end{NiceTabular}
Un
\begin{NiceTabular}{ccc}
\Block[fill=red!10,b,r]{4-2}{two\\lines}
.& & \Huge Un\\ deux
.& & deux \\ trois
.& & trois \\
.& & \Huge quatre \\
text & text \\
two
lines quatre
text text
\end{NiceTabular}
Un
\begin{NiceTabular}{ccc}
\Block[fill=red!10,T,L]{4-2}{two\\lines} two
.& & \Huge Un\\ lines
deux
.& & deux \\ trois
.& & trois \\
.& & \Huge quatre \\
text & text \\
quatre
text text
\end{NiceTabular}
Un
\begin{NiceTabular}{ccc}
\Block[fill=red!10,B,R]{4-2}{two\\lines}
.& & \Huge Un\\ deux
.& & deux \\ trois
.& & trois \\
.& & \Huge quatre \\
text & text \\
two
lines quatre
text text
\end{NiceTabular}
The extension nicematrix provides the ability to use \\ and & directly in the content of a block (in
order to format its contents) but there is some rectrictions.
• One must not use both \\ and & in the same block.
• For \\, there is no other restriction. It’s possible to use \\ in a block to format a text on several
lines.
• In order to use &, the key ampersand-in-blocks (alias: &-in-blocks ) must been activated16 .
Then, the block is divided in sub-blocks as illustrated below. Be careful: when ampersand-
in-blocks is in force, the (main) argument of the command \Block is syntactically divided
into sub-blocks by splitting on the ampersands &, the ampersands between curly braced are
protected but not those in an environment.17
16 Otherwise, the use of & in the command \Block will raise a TeX error :
10
With the ampserand &, it’s possible to divide horizontally a block in sub-blocks of the same size.
\begin{NiceTabular}{ll}%
...[hvlines,ampersand-in-blocks] the five first naturels numbers
..& the five first naturels numbers \\ 3 one two three
3 & \Block{}{one & two & three} \\ 4 one two three four
4 & \Block{}{one& two & three & four} \\
5 one two three four five
5 & \Block{}{one & two & three & four & five} \\
\end{NiceTabular}
As we can see, the blocks (which was are in fact mono-cell blocks) are divided into sub-blocks of the
same size. However, maybe the following code would be prefered.
\begin{NiceTabular}{lccccc}%
...[hvlines,ampersand-in-blocks]
..& \Block{1-5}{the five first the five first natural numbers
.................natural numbers} \\ 3 one two three
3 & \Block{1-5}{one & two & three} \\ 4 one two three four
4 & \Block{1-5}{one& two & three & four} \\ 5 one two three four five
5 & one & two & three & four & five \\
\end{NiceTabular}
In this code, we have blocks of size 1-5 which are divided into three or four sub-blocks.
5 The rules
The usual techniques for the rules may be used in the environments of nicematrix (except \vline).
However, there is some small differences with the classical environments.
\begin{NiceTabular}{|c|c|} \hline
First & Second \\ \hline\hline First Second
Peter \\ \hline Peter
Mary & George\\ \hline Mary George
\end{NiceTabular}
However, the vertical rules are not drawn in the blocks (created by \Block: cf. p. 4) nor in the
corners (created by the key corner: cf. p. 14) nor in the potential exterior rows (created by the keys
first-row and last-row: cf. p. 29).
If you use booktabs (which provides \toprule, \midrule, \bottomrule, etc.) and if you actually want
to add vertical rules (which is not in the spirit of booktabs), you should notice that the vertical rules
drawn by nicematrix are compatible with booktabs. Remark that nicematrix does not load booktabs.
$\begin{NiceArray}{c|ccc} \toprule
a & b & c & d \\ \midrule a b c d
1 & 2 & 3 & 4 \\ 1 2 3 4
1 & 2 & 3 & 4 \\ \bottomrule 1 2 3 4
\end{NiceArray}$
However, it’s still possible to define a specifier (named, for instance, I) to draw vertical rules with
the standard behaviour of array.
11
\newcolumntype{I}{!{\vrule}}
12
All these tools don’t draw the rules in the blocks nor in the empty corners (when the
key corners is used), nor in the exterior rows and columns.
• These blocks are:
– the blocks created by the command \Block18 presented p. 4;
– the blocks implicitly delimited by the continuous dotted lines created by \Cdots, \Vdots,
etc. (cf. p. 31).
• The corners are created by the key corners explained below (see p. 14).
• For the exterior rows and columns, see p. 29.
In particular, this remark exhibits a first difference between the standard command \hline and the
command \Hline provided by nicematrix.
Moreover, the key \Hline takes in an optional argument (between square brackets) which is a list of
key=value pairs. For the description of those keys, see custom-line on p. 16.19
As well as the command \Hline, the specifier “|” supports an optional argument between square
brackets for the characteristics of the rule.
\begin{NiceTabular}{ | c |[color=blue] c |}
\Hline
a & b \\ a b
\Hline[color=red] c d
c & d \\
\Hline
\end{NiceTabular}
18 And also the command \multicolumn but it’s recommended to use instead \Block in the environments of nicematrix.
19 Technical remark: If the user wants to write a command over the command \Hline, it shall be ensured that this
new command is expandable in the TeX sens (by using, for instance, \NewExpandableDocumentCommand of LaTeX3,
\newcommand of LaTeX or \def of TeX). Example: \NewExpandableDocumentCommand}{}{\Hline[color=red]}
13
5.3.2 The keys hvlines and hvlines-except-borders
The key hvlines (no value) is the conjunction of the keys hlines and vlines.
\setlength{\arrayrulewidth}{1pt}
\begin{NiceTabular}{cccc}[hvlines,rules/color=blue]
rose & tulip & sunflower & lily \\
violet & \Block[draw=red]{2-2}{\LARGE flowers} & & orchid \\
carnation & & & lys \\
daisy & peony & jasmine & magnolia
\end{NiceTabular}
It’s worth noting that, when the key rounded-corners is used for the environment {NiceTabular},
the key hvlines draws rounded corners for the exterior frame of the tabular: cf. part 14.1, p. 49.
The key hvlines-except-borders is similar to the key hvlines but does not draw the rules on the
horizontal and vertical borders of the array. For an example of use of that key, see the part “Use
with tcolorbox”, p. 63.
A
A A A
A
In the example on the right (where B is in the center of a A A A A
block of size 2×2), we have colored in blue the four (empty) A A A A A A
corners of the array. A A A A A A
A A A
A
B
A
When the key corners 21 is used, nicematrix computes the (empty) corners and these corners will be
taken into account by the tools for drawing the rules (the rules won’t be drawn in the corners).
20 For sake of completeness, we should also say that a cell contained in a block (even an empty cell) is not taken into
account for the determination of the corners. That behaviour is natural. The precise definition of a “non-empty cell”
is given below (cf. p. 60).
21 The key corners that we describe now has no direct link with the key rounded-corners described in the part 14.1,
p. 49
14
\NiceMatrixOptions{cell-space-top-limit=3pt}
\begin{NiceTabular}{*{6}{c}}[corners,hvlines] A
..& & & & A \\ A A A
..& & A & A & A \\ A
..& & & A \\ A A A A
..& & A & A & A & A \\
A A A A A A
A & A & A & A & A & A \\
A & A & A & A & A & A \\ A A A A A A
..& A & A & A \\ A A A
..& \Block[C]{2-2}{B} & & A \\ A
B
..& & & A \\ A
\end{NiceTabular}
It’s also possible to provide to the key corners a (comma-separated) list of corners (designed by NW,
SW, NE and SE).
\NiceMatrixOptions{cell-space-top-limit=3pt}
\begin{NiceTabular}{*{6}{c}}[corners=NE,hvlines] 1
1\\ 1 1
1&1\\ 1 2 1
1&2&1\\
1 3 3 1
1&3&3&1\\
1&4&6&4&1\\ 1 4 6 4 1
.& & & & &1 1
\end{NiceTabular}
. The corners are also taken into account by the tools provided by nicematrix to color cells, rows
and columns. These tools don’t color the cells which are in the corners (cf. p. 19). The command
\TikzEveryCell available in the \CodeBefore and the \CodeAfter (cf. p. 43) takes also into account
the empty corners.
15
Nevertheless, it’s always possible to draw whatever rule we wish with TikZ in the \CodeAfter (or
the \CodeBefore) by using the PGF/TikZ nodes created by nicematrix: cf. p. 53.
• the key command is the name (without the backslash) of a command that will be created by
nicematrix and that will be available for the final user in order to draw horizontal rules (similarly
to \hline);
• the key ccommand is the name (without the backslash) of a command that will be created by
nicematrix and that will be available for the final user to order to draw partial horizontal rules
(similarly to \cline, hence the name ccommand): the argument of that command is a list of
intervals of columns specified by the syntax i or i-j.22
• the key letter takes in as argument a letter23 that the user will use in the preamble of an
environment with preamble (such as {NiceTabular} in order to specify a vertical rule.
We will now speak of the keys which describe the rule itself. Those keys may also be used in the
(optional) argument of an individual command \Hline or in the (optional) argument of a specifier
“|” in the preamble of an environment.
• First possibility
It’s possible to specify composite rules, with a color and a color for the inter-rule space (as
possible with colortbl for instance).
– the key multiplicity is the number of consecutive rules that will be drawn: for instance,
a value of 2 will create double rules such those created by \hline\hline or || in the
preamble of an environment;
– the key color sets the color of the rules ;
– the key sep-color sets the color between two successive rules (should be used only in
conjunction with multiplicity). The name of that key is inspired by the command
\doublerulesepcolor of colortbl.
That system may be used, in particular, for the definition of commands and letters to draw
rules with a specific color (and those rules will respect the blocks and corners as do all the rules
of nicematrix).
\begin{NiceTabular}{lcIcIc}[custom-line = {letter=I, color=blue}]
\hline
..........& \Block{1-3}{dimensions} \\
..........& L & l & h \\
\hline
Product A & 3 & 1 & 2 \\
Product B & 1 & 3 & 4 \\
Product C & 5 & 4 & 1 \\
\hline
\end{NiceTabular}
22 It’s recommended to use such commands only once in a row because each use will create space between the rows
corresponding to the total width of the rule. Likewise, it’s possible to draw several rules with only one use of the
command.
23 The following letters are forbidden: lcrpmbVX|()[]!@<>
16
dimensions
L l H
Product A 3 1 2
Product B 1 3 4
Product C 5 4 1
The key sep-color with the value white may also be used in case of an horizontal double-rule
on the top of a colored cell (if we want the space between both rules above the cell not colored
by the color of the cell).
\NiceMatrixOptions
..{
....custom-line =
.....{
.......command = DoubleRule ,
.......multiplicity = 2 ,
.......sep-color = white one two three
.....}
four five six
..}
\begin{NiceTabular}{ccc}
one & two & three \\
\DoubleRule
four & \cellcolor{yellow} five & six \\
\end{NiceTabular}
• Second possibility
It’s possible to use the key tikz (if TikZ is loaded). In that case, the rule is drawn directly
with TikZ by using as parameters the value of the key tikz which must be a list of key=value
pairs which may be applied to a TikZ path.
By default, no space is reserved for the rule that will be drawn with TikZ. It is possible to
specify a reservation (horizontal for a vertical rule and vertical for an horizontal one) with the
key total-width. That value of that key, is, in some ways, the width of the rule that will be
drawn (nicematrix does not compute that width from the characteristics of the rule specified in
tikz).
\begin{NiceTabular}{cIcIc}
one & two & three \\
four & five & six \\
seven & eight & nine
\end{NiceTabular}
17
draw rounded dots. The initial value of the key total-width is, in this case, equal to the
diameter of the dots (but the user may change the value with the key total-width if needed).
Those dotted rules are also used by nicematrix to draw continuous dotted rules between cells of
the matrix with \Cdots, \Vdots, etc. (cf. p. 31).
In fact, nicematrix defines by default the commands \hdottedline and \cdottedline and the
letter “:” for those dotted rules.24
\NiceMatrixOptions % present in [Link]
..{
....custom-line =
......{
........letter = : ,
........command = hdottedline ,
........ccommand = cdottedline ,
........dotted
......}
..}
Thus, it’s possible to use the commands \hdottedline and \cdottedline to draw horizontal
dotted rules.
\begin{pNiceMatrix}
1 & 2 & 3 & 4 & 5 \\
\hdottedline
1 2 3 4 5
In the environments with an explicit preamble (like {NiceTabular}, {NiceArray}, etc.), it’s
possible to draw a vertical dotted line with the specifier “:”.
$\left(\begin{NiceArray}{cccc:c}
1 & 2 & 3 & 4 & 5 \\
1 2 3 4 5
6 & 7 & 8 & 9 & 10 \\ 6 7 8 9 10
11 & 12 & 13 & 14 & 15 11 12 13 14 15
\end{NiceArray}\right)
New 7.2
As previously said, the previous keys may be used in the optional argument of an individual command
\Hline and in the optional argument of a specifier “|” in the preamble of an environment (for instance
{NiceTabular}).
However, in that case, it’s also possible to use two other keys, start and end, which specify the
extremities of the rule.
\begin{NiceTabular}{cc|[color=blue,start=2]ccc}
one & two & three & four one two three four
\Hline[start=2,end=3] five six seven eight
five & six & seven & eight \\ nine ten eleven twelve
nine & ten & eleven & twelve
\end{NiceTabular}
24 However, it’s possible to overwrite those definitions with a custom-line (in order, for example, to switch to dashed
lines).
18
6 The color of the background of the rows and columns
19
These commands respect the rounded corners if the key rounded-corners (described in the part 14.1
at the page 49) has been used.
All these commands accept an optional argument, between square brackets and in first position. That
optional argument may contain two elements (separated by a comma)
• the colorimetric space (RGB, rgb, HTML, etc) as specified by the the extension xcolor;
• a specification of opacity of the form opacity = value.28
• The command \cellcolor takes its name from the command \cellcolor of colortbl.
This command takes in as mandatory arguments a color and a list of cells, each of which with
the format i-j where i is the number of the row and j the number of the column of the cell. In
fact, despite its name, this command may be used to color a whole row (with the syntax i-) or
a whole column (with the syntax -j).
\begin{NiceTabular}{ccc}[hvlines]
\CodeBefore
..\cellcolor[HTML]{FFFF88}{3-1,2-2,-3} a b c
\Body e f g
a & b & c \\
h i j
e & f & g \\
h & i & j \\
\end{NiceTabular}
• The command \rectanglecolor takes three mandatory arguments. The first is the color. The
second is the upper-left cell of the rectangle and the third is the lower-right cell of the rectangle.
\begin{NiceTabular}{ccc}[hvlines]
\CodeBefore
..\rectanglecolor{blue!15}{2-2}{3-3} a b c
\Body e f g
a & b & c \\
h i j
e & f & g \\
h & i & j \\
\end{NiceTabular}
• The command \arraycolor takes in as mandatory argument a color and color the whole
tabular with that color (except the potential exterior rows and columns: cf. p. 29). It’s only a
particular case of \rectanglecolor.
• The command \chessboardcolors takes in as mandatory arguments two colors and it colors
the cells of the tabular in quincunx with these colors.
$\begin{pNiceMatrix}[r,margin]
\CodeBefore
..\chessboardcolors{red!15}{blue!15}
\Body
1 −1 1
1 & -1 & 1 \\ −1 1 −1
-1 & 1 & -1 \\ 1 −1 1
1 & -1 & 1
\end{pNiceMatrix}$
We have used the key r which aligns all the columns rightwards (cf. p. 50).
28 Caution: that feature creates instructions of transparency in the pdf and some readers of pdf don’t support such
instructions.
20
• The command \rowcolor takes its name from the command \rowcolor of colortbl. Its first
mandatory argument is the color and the second is a comma-separated list of rows or interval
of rows with the form a-b (an interval of the form a- represent all the rows from the row a until
the end).
$\begin{NiceArray}{lll}[hvlines]
\CodeBefore
..\rowcolor{red!15}{1,3-5,8-}
\Body
a_1 & b_1 & c_1 \\ a1 b1 c1
a_2 & b_2 & c_2 \\ a2 b2 c2
a_3 & b_3 & c_3 \\ a3 b3 c3
a_4 & b_4 & c_4 \\ a4 b4 c4
a_5 & b_5 & c_5 \\ a5 b5 c5
a_6 & b_6 & c_6 \\ a6 b6 c6
a_7 & b_7 & c_7 \\ a7 b7 c7
a_8 & b_8 & c_8 \\ a8 b8 c8
a_9 & b_9 & c_9 \\ a9 b9 c9
a_{10} & b_{10} & c_{10} \\ a10 b10 c10
\end{NiceArray}$
• The command \columncolor takes its name from the command \columncolor of colortbl. Its
syntax is similar to the syntax of \rowcolor.
• The command \rowcolors (with a s) takes its name from the command \rowcolors of colortbl.
The s emphasizes the fact that there is two colors. This command colors alternately the rows
of the tabular with the two colors (provided in second and third argument), beginning with the
row whose number is given in first (mandatory) argument. One of the arguments of color may
be empty (no color is applied in the corresponding rows).
In fact, the first (mandatory) argument is, more generally, a comma separated list of intervals
describing the rows involved in the action of \rowcolors (an interval of the form i- describes
in fact the interval of all the rows of the tabular, beginning with the row i).
The last argument of \rowcolors is an optional list of pairs key=value (the optional argument
in the first position corresponds to the colorimetric space). The available keys are cols, restart
and respect-blocks.
– The key cols describes a set of columns. The command \rowcolors will color only the
cells of these columns. The value is a comma-separated list of intervals of the form i-j
(where i or j may be replaced by *).
– With the key restart , each interval of rows (specified by the first mandatory argument)
begins with the same color.29
– With the key respect-blocks the “rows” alternately colored may extend over several
rows if they have to incorporate blocks (created with the command \Block: cf. p. 4).
29 Otherwise, the color of a given row relies only upon the parity of its absolute number.
21
\begin{NiceTabular}{clr}[hvlines]
\CodeBefore
..\rowcolors[gray]{2}{0.8}{}[cols=2-3,restart]
\Body
\Block{1-*}{Results} \\ Results
John & 12 \\ John 12
Stephen & 8 \\ A
Stephen 8
Sarah & 18 \\
Sarah 18
Ashley & 20 \\
Ashley 20
Henry & 14 \\ B
Henry 14
Madison & 15
Madison 15
\end{NiceTabular}
\begin{NiceTabular}{lr}[hvlines]
\CodeBefore
..\rowcolors{1}{blue!10}{}[respect-blocks] 12
\Body John
13
\Block{2-1}{John} & 12 \\ Steph 8
.....................& 13 \\ 18
Steph & 8 \\ Sarah 17
\Block{3-1}{Sarah} & 18 \\ 15
.....................& 17 \\
Ashley 20
.....................& 15 \\
Henry 14
Ashley & 20 \\
15
Henry & 14 \\ Madison
19
\Block{2-1}{Madison} & 15 \\
.....................& 19
\end{NiceTabular}
• The extension nicematrix provides also a command \rowlistcolors. This command generalises
the command \rowcolors: instead of two successive arguments for the colors, this command
takes in an argument which is a (comma-separated) list of colors. In that list, the symbol =
represent a color identical to the previous one.
\begin{NiceTabular}{c}
\CodeBefore
..\rowlistcolors{1}{red!15,blue!15,green!15} Peter
\Body James
Peter \\ Abigail
James \\ Elisabeth
Abigail \\ Claudius
Elisabeth \\ Jane
Claudius \\ Alexandra
Jane \\
Alexandra \\
\end{NiceTabular}
It’s also possible to use in the command \rowlistcolors a color series defined by the command
\definecolorseries of xcolor (and initialized with the command \resetcolorseries30 ).
30 For the initialization, in the following example, you have used the counter iRow (which corresponds to the internal
TeX counter \c@iRow) which, when used in the \CodeBefore (and in the \CodeAfter) corresponds to the number of
rows of the array: cf. p 51. That leads to an adjustment of the gradation of the colors to the size of the tabular.
22
\begin{NiceTabular}{c}
\CodeBefore
...\definecolorseries{BlueWhite}{rgb}{last}{blue}{white}
...\resetcolorseries[\value{iRow}]{BlueWhite} Peter
...\rowlistcolors{1}{BlueWhite!!+} James
\Body Abigail
Peter \\ Elisabeth
James \\ Claudius
Abigail \\ Jane
Elisabeth \\ Alexandra
Claudius \\
Jane \\
Alexandra \\
\end{NiceTabular}
We recall that all the color commands we have described don’t color the cells which are in the
“corners”. In the following example, we use the key corners to require the determination of the
corner north east (NE).
\begin{NiceTabular}{ccccccc}[corners=NE,margin,hvlines,first-row,first-col]
\CodeBefore
..\rowlistcolors{1}{blue!15, } 0 1 2 3 4 5 6
\Body 0 1
..& 0 & 1 & 2 & 3 & 4 & 5 & 6 \\ 1 1 1
0 & 1 \\ 2 1 2 1
1 & 1 & 1 \\ 3 1 3 3 1
2 & 1 & 2 & 1 \\
4 1 4 6 4 1
3 & 1 & 3 & 3 & 1 \\
5 1 5 10 10 5 1
4 & 1 & 4 & 6 & 4 & 1 \\
6 1 6 15 20 15 6 1
5 & 1 & 5 & 10 & 10 & 5 & 1 \\
6 & 1 & 6 & 15 & 20 & 15 & 6 & 1 \\
\end{NiceTabular}
The previous example uses the keys first-row and first-col which are described in the chapter
concerning the «exterior» rows and columns (cf. p. 29).
As one can see, by default, the coloring commands that we have described don’t apply in those exterior
rows and columns.
However, it may still be possible to color in those rows and columns by providing explicitly the
numbers of those rows and columns.
In the following example, we require a color in the column 0 (which is the «first column» and which
exists because the key first-col has been used).
\begin{NiceTabular}{ccccccc}[corners=NE,margin,hvlines,first-row,first-col]
\CodeBefore
..\rowlistcolors{1}{blue!15, }
..\columncolor{red!15}{0} 0 1 2 3 4 5 6
\Body 0 1
..& 0 & 1 & 2 & 3 & 4 & 5 & 6 \\ 1 1 1
0 & 1 \\ 2 1 2 1
1 & 1 & 1 \\ 3 1 3 3 1
2 & 1 & 2 & 1 \\ 4 1 4 6 4 1
3 & 1 & 3 & 3 & 1 \\ 5 1 5 10 10 5 1
4 & 1 & 4 & 6 & 4 & 1 \\ 6 1 6 15 20 15 6 1
5 & 1 & 5 & 10 & 10 & 5 & 1 \\
6 & 1 & 6 & 15 & 20 & 15 & 6 & 1 \\
\end{NiceTabular}
One should remark that all the previous commands are compatible with the commands of booktabs
(\toprule, \midrule, \bottomrule, etc). However, booktabs is not loaded by nicematrix.
23
\begin{NiceTabular}[c]{lSSSS}
\CodeBefore
..\rowcolor{red!15}{1-2}
..\rowcolors{3}{blue!15}{}
\Body
\toprule
dimensions (cm)
Price
\Block[C]{2-1}{Product} & Product
\Block{1-3}{dimensions (cm)} & & & L l h
\Block{2-1}{\rotate Price} \\ small 3 5.5 1 30
\cmidrule(rl){2-4} standard 5.5 8 1.5 50.5
.& L & l & h \\ premium 8.5 10.5 2 80
\midrule extra 8.5 10 1.5 85.5
small & 3 & 5.5 & 1 & 30 \\ special 12 12 0.5 70
standard & 5.5 & 8 & 1.5 & 50.5 \\
premium & 8.5 & 10.5 & 2 & 80 \\
extra & 8.5 & 10 & 1.5 & 85.5 \\
special & 12 & 12 & 0.5 & 70 \\
\bottomrule
\end{NiceTabular}
We have used the type of column S of siunitx (which should be loaded by the user).
New 7.1
It’s also possible, in the \CodeBefore, to use the commands \EmptyColumn and \EmptyRow. The
command \EmptyColumn takes in as argument a (comma-separated) list of numbers of columns and
requires that no rules nor background colors will be drawn in the corresponding columns. The
command \EmptyRow is similar for the rows.
\begin{NiceTabular}{ccccc}[hvlines,no-cell-nodes]
\CodeBefore
..\rowcolor{blue!15}{1} one two three four
..\EmptyColumn{3} two
\Body one three four
rows
...one & two && three & four \\
...one & \Block{}{two\\ rows} && three & four \\
\end{NiceTabular}
31 That command \cellcolor will delete the following spaces (which does not the command \cellcolor of colortbl).
Moreover, if one wishes to define a command above that command \cellcolor, it must be protected in the TeX
sens (whereas, if it were the command \cellcolor of colortbl, one should, no the contrary, write a fully expandable
command).
32 If you want a commande to color the following n rows, consider the command \RowStyle and its key fillcolor,
p. 26
24
• \rowcolors which takes in as arguments two colors and color the rest of the tabular with those
colors;
• \rowlistcolors which takes in as argument a color and color the rest of the tabular with the
colors of that list of colors.33
These commands are compatible with the commands for the overlays of Beamer (\only, etc.)
\NewDocumentCommand { \Blue } { } { \columncolor{blue!15} }
\begin{NiceTabular}{>{\Blue}c>{\Blue}cc}
\toprule
\rowcolor{red!15}
Last name & First name & Birth day \\
\midrule
Achard & Jacques & 5 juin 1962 \\
Lefebvre & Mathilde & 23 mai 1988 \\
Vanesse & Stephany & 30 octobre 1994 \\
Dupont & Chantal & 15 janvier 1998 \\
\bottomrule
\end{NiceTabular}
Each use of the \rowlistcolors (or \rowcolors, which is, in fact, a special case of \rowlistcolors)
stops the potential coloring schemes34 specified by a previous command \rowlistcolors. In par-
ticular, it’s possible to start coloring the rows with \rowlistcolors{...} and stop coloring by a
command \rowlistcolors with an empty argument.
\begin{NiceTabular}{c}[hvlines]
one \\
two \\ one
\rowlistcolors{red!15} two
three \\ three
four \\ four
five \\ five
\rowlistcolors{} six
six \\ seven
seven \\
\end{NiceTabular}
33 When the command \rowlistcolors (or the command \rowcolors) is used in a cell of the column j of the array,
on different columns.
25
7 The command \RowStyle
The command \RowStyle takes in as argument some formatting instructions that will be applied to
each cell on the rest of the current row.
That command also takes in as optional argument (between square brackets) a list of key=value
pairs.
• The key nb-rows sets the number of rows to which the specifications of the current command
will apply (with the special value *, it will apply to all the following rows).
• The keys cell-space-top-limit , cell-space-bottom-limit and cell-space-limits are
available with the same meaning that the corresponding global keys (cf. p. 2).
• The key fill (alias: rowcolor ) sets the color of the background and opacity 35 sets its opacity.
• If the key rounded-corners is used, that background will have rounded-corners.
• The key color sets the color of the text.36
• The key bold enforces bold characters for the cells of the row, both in math and text mode.
\begin{NiceTabular}{cccc}
\hline
second
fourth
\RowStyle[cell-space-limits=3pt]{\rotate}
third
first
first & second & third & fourth \\
\RowStyle[nb-rows=2,fill=blue!50,color=white]{\sffamily} 1 2 3 4
1 & 2 & 3 & 4 \\ I II III IV
I & II & III & IV
\end{NiceTabular}
The command \rotate is described p. 50.
In the environments of nicematrix, it’s also possible to fix the minimal width of all the columns (except
the potential exterior columns: cf. p. 29) directly with the key columns-width.
35 Caution: that feature creates instructions of transparency in the pdf and some readers of pdf don’t support such
instructions.
36 The key color uses the command \color but also inserts an instruction \leavevmode before. This instruction
prevents an extra vertical space in the cells which belong to columns of type p, b, m and X (which start in vertical
mode of LaTeX). For the columns V (of varwidth), that’s not enough, except when LuaLaTeX is used with the package
luacolor (see question 460489 on TeX StackExchange).
26
$\begin{pNiceMatrix}[columns-width = 1cm]
1 & 12 & -123 \\
1 12 −123
12 & 0 & 0 \\ 12 0 0
4 & 1 & 2 4 1 2
\end{pNiceMatrix}$
Note that the space inserted between two columns (equal to 2 \tabcolsep in {NiceTabular} and
to 2 \arraycolsep in the other environments) is not suppressed (of course, it’s possible to suppress
this space by setting \tabcolsep or \arraycolsep equal to 0 pt before the environment).
It’s possible to give the special value auto to the option columns-width: all the columns of the array
will have a width equal to the widest cell of the array.37
$\begin{pNiceMatrix}[columns-width = auto]
1 & 12 & -123 \\
1 12 −123
12 & 0 & 0 \\ 12 0 0
4 & 1 & 2 4 1 2
\end{pNiceMatrix}$
Without surprise, it’s possible to fix the minimal width of the columns of all the arrays of a current
scope with the command \NiceMatrixOptions.
\NiceMatrixOptions{columns-width=10mm}
$\begin{pNiceMatrix}
a & b \\ c & d
\end{pNiceMatrix}
a b 1 1245
=
= c d 345 2
\begin{pNiceMatrix}
1 & 1245 \\ 345 & 2
\end{pNiceMatrix}$
It’s also possible to fix a zone where all the matrices will have their columns of the same width, equal
to the widest cell of all the matrices. This construction uses the environment {NiceMatrixBlock}
with the option auto-columns-width38 . The environment {NiceMatrixBlock} has no direct link
with the command \Block presented previously in this document (cf. p. 4).
\begin{NiceMatrixBlock}[auto-columns-width]
$\begin{array}{c}
\begin{bNiceMatrix}
9 17
.9 & 17 \\ -2 & 5 −2 5
.\end{bNiceMatrix} \\ \\
\begin{bNiceMatrix}
1 1245345
.1 & 1245345 \\ 345 & 2 345 2
\end{bNiceMatrix} \\
\end{array}$
\end{NiceMatrixBlock}
37 The result is achieved with only one compilation (but PGF/TikZ will have written information in the aux file and
future.
27
\fbox{%
\begin{varwidth}{8cm}
\begin{itemize} • first item
\item first item
\item second item • second item
\end{itemize}
\end{varwidth}}
\fbox{%
\begin{minipage}{8cm}
\begin{itemize} • first item
\item first item
\item second item • second item
\end{itemize}
\end{minipage}}
The package varwidth provides also the column type V. A column of type V{hdimi} encapsulates all
its cells in a {varwidth} with the argument hdimi (and does also some tuning).
When the package varwidth is loaded, the columns V of varwidth are supported by nicematrix.
\begin{NiceTabular}[corners=NW,hvlines]{V{3cm}V{3cm}V{3cm}}
& some text & some very very very long text \\
some very very very long text \\
some very very very long text
\end{NiceTabular}
Concerning nicematrix, one of the interests of this type of columns is that, for a cell of a column of
type V, the PGF/TikZ node created by nicematrix for the content of that cell has a width adjusted
to the content of the cell : cf. p. 55.
The columns V of nicematrix supports the keys t, p, m, b, l, c and r also supported by the columns
X: see their description in the section 8.3, p. 28.
One should remark that the extension varwidth (at least in its version 0.92) has some problems: for
instance, with LuaLaTeX, it does not work when the content begins with \color.
28
• It’s possible to give a weight for the column by providing a positive number directly as argument
of the specifier X. For example, a column X[2] will have a width double of the width of a column X
(which has a weight equal to 1).40
• It’s possible to specify an horizontal alignment with one of the letters l, c and r (which insert
respectively \raggedright, \centering and \raggedleft followed by \arraybackslash).41
• It’s possible to specify a vertical alignment with one of the keys t (alias p), m and b (which
construct respectively columns of type p, m and b). The initial value is t.
• New 7.2
It’s possible to use the key V in a column of type X. When that key is used, the column X behaves
in fact like a column V of the extension varwidth (which must have been loaded by the user).
Henece, the widht of the columns, as computed by the X process, is in fact the maximal widht
of the column.
\begin{NiceTabular}[width=9cm]{X[c,m]X[0.5,c,m]}[hvlines]
a rather long text which fits on several lines
& a rather long text which fits on several lines \\
a shorter text & a shorter text
\end{NiceTabular}
a rather long
a rather long text which fits on
text which fits on
several lines
several lines
a shorter text a shorter text
The options first-row , last-row, first-col and last-col allow the composition of exterior rows
and columns in the environments of nicematrix. It’s particularly interesting for the (mathematical)
matrices.
A potential “first row” (exterior) has the number 0 (and not 1). Idem for the potential “first column”.
$\begin{pNiceMatrix}[first-row,last-row,first-col,last-col,nullify-dots]
.......& C_1 & \Cdots & & C_4 & \\
L_1 & a_{11} & a_{12} & a_{13} & a_{14} & L_1 \\
\Vdots & a_{21} & a_{22} & a_{23} & a_{24} & \Vdots \\
.......& a_{31} & a_{32} & a_{33} & a_{34} & \\
L_4 & a_{41} & a_{42} & a_{43} & a_{44} & L_4 \\
.......& C_1 & \Cdots & & C_4 &
\end{pNiceMatrix}$
C1 C4
L1 a11 a12 a13 a14 L1
a21 a22 a23 a24
a31 a32 a33 a34
L4 a41 a42 a43 a44 L4
C1 C4
The dotted lines have been drawn with the tools presented p. 31.
40 The negative values of the weight, as provided by tabu (which is now obsolete), are not supported by nicematrix. If
ragged2e instead of the commands \raggedright, \centering and \raggedleft. That ensures a better output.
29
We have several remarks to do.
• For the environments with an explicit preamble (i.e. {NiceTabular}, {NiceArray} and its
variants), no letter must be given in that preamble for the potential first column and the
potential last column: they will automatically (and necessarily) be of type r for the first column
and l for the last one.42
• One may wonder how nicematrix determines the number of rows and columns which are needed
for the composition of the “last row” and “last column”.
– For the environments with explicit preamble, like {NiceTabular} and {pNiceArray}, the
number of columns can obviously be computed from the preamble.
– When the option light-syntax (cf. p. 52) is used, nicematrix has, in any case, to load the
whole body of the environment (and that’s why it’s not possible to put verbatim material
in the array with the option light-syntax). The analysis of this whole body gives the
number of rows and the number of columns.
– In the other cases, nicematrix compute the number of rows and columns during the first
compilation and write the result in the aux file for the next run.
However, it’s possible to provide the number of the last row and the number of the last
column as values of the options last-row and last-col, tending to an acceleration of the
whole compilation of the document. That’s what we will do throughout the rest of the
document.
It’s possible to control the appearance of these rows and columns with options code-for-first-row,
code-for-last-row , code-for-first-col and code-for-last-col. These options specify tokens
that will be inserted before each cell of the corresponding row or column.
\NiceMatrixOptions{code-for-first-row = \color{red},
...................code-for-first-col = \color{blue},
...................code-for-last-row = \color{green},
...................code-for-last-col = \color{magenta}}
\begin{displaymath}
\begin{pNiceArray}{cc|cc}[first-row,last-row=5,first-col,last-col,nullify-dots]
.......& C_1 & \multicolumn1c{\Cdots} & & C_4 & \\
L_1 & a_{11} & a_{12} & a_{13} & a_{14} & L_1 \\
\Vdots & a_{21} & a_{22} & a_{23} & a_{24} & \Vdots \\
\hline
.......& a_{31} & a_{32} & a_{33} & a_{34} & \\
L_4 & a_{41} & a_{42} & a_{43} & a_{44} & L_4 \\
.......& C_1 & \multicolumn1c{\Cdots} & & C_4 &
\end{pNiceArray}
\end{displaymath}
C1 C4
L1 a11 a12 a13 a14 L1
a21 a22 a23 a24
a31 a32 a33 a34
L4 a41 a42 a43 a44 L4
C1 C4
Remarks
• As shown in the previous example, the horizontal and vertical rules don’t extend in the exterior
rows and columns. This remark also applies to the customized rules created by the key custom-
line (cf. p. 16).
42 The users wishing exterior columns with another type of alignment should consider the command \SubMatrix
available in the \CodeAfter and in the \CodeBefore (cf. p. 40) or try to put delimiter directly in the preamble of the
array (cf. p. 37).
30
• A specification of color present in code-for-first-row also applies to a dotted line drawn in
that exterior “first row” (except if a value has been given to xdots/color). Idem for the other
exterior rows and columns.
• Logically, the potential option columns-width (described p. 26) doesn’t apply to the “first
column” and “last column”.
• For technical reasons, it’s not possible to use the option of the command \\ after the “first row”
or before the “last row”. The placement of the delimiters would be wrong. If you are looking for
a workaround, consider the command \SubMatrix in the \CodeAfter (and the \CodeBefore)
described p. 40.
Inside the environments of the package nicematrix, new commands are defined: \Ldots , \Cdots ,
\Vdots , \Ddots and \Iddots . These commands are intended to be used in place of \dots, \cdots,
\vdots, \ddots and \iddots.43
Each of them must be used alone in the cell of the array and it draws a dotted line between the
first non-empty cells44 on both sides of the current cell. Of course, for \Ldots and \Cdots, it’s an
horizontal line; for \Vdots, it’s a vertical line and for \Ddots and \Iddots diagonal ones. It’s possible
to change the color of these lines with the option color.45
\begin{bNiceMatrix}
a1 a1
a_1 & \Cdots & & & a_1 \\
\Vdots & a_2 & \Cdots & & a_2 \\
a2 a2
.........& \Vdots & \Ddots[color=red] \\
\\
a_1 & a_2 & & & a_n
a1 a2 an
\end{bNiceMatrix}
In order to represent the null matrix, one can use the following codage:
\begin{bNiceMatrix}
0 & \Cdots & 0 \\ 0 0
\Vdots & & \Vdots \\
0 & \Cdots & 0 0 0
\end{bNiceMatrix}
However, one may want a larger matrix. Usually, in such a case, the users of LaTeX add a new row
and a new column. It’s possible to use the same method with nicematrix:
\begin{bNiceMatrix}
0 0
0 & \Cdots & \Cdots & 0 \\
\Vdots & & & \Vdots \\
\Vdots & & & \Vdots \\
0 & \Cdots & \Cdots & 0
0 0
\end{bNiceMatrix}
In the first column of this example, there are two instructions \Vdots but, of course, only one dotted
line is drawn.
43 The command \iddots, defined in nicematrix, is a variant of \ddots with dots going forward. If mathdots is loaded,
31
In fact, in this example, it would be possible to draw the same matrix more easily with the following
code:
\begin{bNiceMatrix}
0 0
0 & \Cdots & & 0 \\
\Vdots & & & \\
........& & & \Vdots \\
0 & & \Cdots & 0
0 0
\end{bNiceMatrix}
There are also other means to change the size of the matrix. Someone might want to use the optional
argument of the command \\ for the vertical dimension and a command \hspace* in a cell for the
horizontal dimension.46
However, a command \hspace* might interfere with the construction of the dotted lines. That’s why
the package nicematrix provides a command \Hspace which is a variant of \hspace transparent for
the dotted lines of nicematrix.
0 0
$\begin{bNiceMatrix}
0 & \Cdots & \Hspace*{1cm} & 0 \\
\Vdots & & & \Vdots \\[1cm]
0 & \Cdots & & 0
\end{bNiceMatrix}$
0 0
If we add \ldots instructions in the second row, the geometry of the matrix is modified.
$B = \begin{pmatrix}
h & i & j & k & l & m \\
h i j k l m
B=
x & \ldots & \ldots & \ldots & \ldots & x x ... ... ... ... x
\end{pmatrix}$
By default, with nicematrix, if we replace {pmatrix} by {pNiceMatrix} and \ldots by \Ldots, the
geometry of the matrix is not changed.
$C = \begin{pNiceMatrix}
h & i & j & k & l & m \\
h i j k l m
C=
x & \Ldots & \Ldots & \Ldots & \Ldots & x x x
\end{pNiceMatrix}$
However, one may prefer the geometry of the first matrix A and would like to have such a geometry
with a dotted line in the second row. It’s possible by using the option nullify-dots (and only one
instruction \Ldots is necessary).
$D = \begin{pNiceMatrix}[nullify-dots]
h & i & j & k & l & m \\
h i j k l m
D=
x & \Ldots & & & & x x x
\end{pNiceMatrix}$
46 In nicematrix, one should use \hspace* and not \hspace for such an usage because nicematrix loads array. One may
also remark that it’s possible to fix the width of a column by using the environment {NiceArray} (or one of its variants)
with a column of type w or W: see p. 26
32
The option nullify-dots smashes the instructions \Ldots (and the variants) horizontally but also
vertically.
Caution: the key nullify-dots has a name that may be confusing; that key does not imply that
the dotted rules won’t be drawn!
However, if these cells are empty, the dotted line extends only in the cells specified by the argument
of \Hdotsfor (by design).
$\begin{pNiceMatrix}
1 & 2 & 3 & 4 & 5 \\
1 2 3 4 5
..& \Hdotsfor{3} \\
1 & 2 & 3 & 4 & 5 \\
1 2 3 4 5
1 & 2 & 3 & 4 & 5 1 2 3 4 5
\end{pNiceMatrix}$
Remark: Unlike the command \hdotsfor of amsmath, the command \Hdotsfor may be used even
when the package colortbl47 is loaded (however, with nicematrix, loading colortbl is rather point-less
since nicematrix provides its own coloring tools: cf. p. 19).
The package nicematrix also provides a command \Vdotsfor similar to \Hdotsfor but for the vertical
dotted lines. The following example uses both \Hdotsfor and \Vdotsfor:
\begin{bNiceMatrix}
C[a_1,a_1] & \Cdots & C[a_1,a_n]
..& \hspace*{20mm} & C[a_1,a_1^{(p)}] & \Cdots & C[a_1,a_n^{(p)}] \\
\Vdots & \Ddots & \Vdots
..& \Hdotsfor{1} & \Vdots & \Ddots & \Vdots \\
C[a_n,a_1] & \Cdots & C[a_n,a_n]
..& & C[a_n,a_1^{(p)}] & \Cdots & C[a_n,a_n^{(p)}] \\
\rule{0pt}{15mm}\NotEmpty & \Vdotsfor{1} & & \Ddots & & \Vdotsfor{1} \\
C[a_1^{(p)},a_1] & \Cdots & C[a_1^{(p)},a_n]
..& & C[a_1^{(p)},a_1^{(p)}] & \Cdots & C[a_1^{(p)},a_n^{(p)}] \\
\Vdots & \Ddots & \Vdots
..& \Hdotsfor{1} & \Vdots & \Ddots & \Vdots \\
C[a_n^{(p)},a_1] & \Cdots & C[a_n^{(p)},a_n]
..& & C[a_n^{(p)},a_1^{(p)}] & \Cdots & C[a_n^{(p)},a_n^{(p)}]
\end{bNiceMatrix}
47 We recall that when xcolor is loaded with the option table, the package colortbl is loaded.
33
(p) (p)
C[a1 , a1 ] C[a1 , an ] C[a1 , a1 ] C[a1 , an ]
(p) (p)
C[an , a1 ] C[an , an ] C[an , a1 ] C[an , an ]
(p) (p) (p) (p) (p) (p)
C[a1 , a1 ] C[a1 , an ] C[a1 , a1 ] C[a1 , an ]
(p) (p) (p) (p) (p) (p)
C[an , a1 ] C[an , an ] C[an , a1 ] C[an , an ]
Therefore, with the keys renew-dots and renew-matrix, a classical code gives directly the output
of nicematrix.
\NiceMatrixOptions{renew-dots,renew-matrix}
\begin{pmatrix}
1 1
1 & \cdots & \cdots & 1 \\
0
0 & \ddots & & \vdots \\
\vdots & \ddots & \ddots & \vdots \\
0 & \cdots & 0 & 1 0 0 1
\end{pmatrix}
48 The options renew-dots, renew-matrix can be fixed with the command \NiceMatrixOptions like the other options.
34
With the key horizontal-label , the labels stay horizontal.
$\begin{bNiceMatrix} 1 0
1 & \hspace*{1cm} & 0 \\[8mm] n times
..& \Ddots[horizontal-label]^{n \text{ times}} & \\
0 & & 1
\end{bNiceMatrix}$ 0 1
• color;
• radius;
• shorten-start, shorten-end and shorten;
• inter;
• line-style.
For an individual command, it’s possible to write horizontal-label (in the singular).
These options may also be fixed with \NiceMatrixOptions, as options of \CodeAfter or at the level
of a given environment but, in those cases, they must be prefixed by xdots (xdots to remind that it
works for \Cdots, \Ldots, \Vdots, etc.), and, thus have for names:
• xdots/horizontal-labels;
• xdots/color;
• xdots/radius;
• xdots/shorten-start, xdots/shorten-end and xdots/shorten;
• xdots/inter;
• xdots/line-style.
For the clarity of the explanations, we will use those names.
With the key xdots/horizontal-labels, the labels (introduced by _, ^ and :) stay horizontal.
The option xdots/color fixes the color or the dotted line. It’s possible to use a color definied “on
the fly” (e.g.: xdots/color = { [RGB]{204,204,255} }). One should remark that the dotted lines
drawn in the exterior rows and columns have a special treatment: cf. p. 29.
The option radius fixes the radius of the dots. The initial value is 0.53 pt.
The keys xdots/shorten-start and xdots/shorten-end fix the margin at the extremities of the
line. The key xdots/shorten fixes both parameters. The initial value is 0.3 em (it is recommended
to use a unit of length dependent of the current font).49
at the level of an environment (such as {pNiceMatrix}), those keys only apply to the extremeties of dotted lines
corresponding to a non-empty content of a cell. When they are used for a command such as \Cdots (and, in that case,
their names are shorten, shorten-start and shorten-end), they apply to all the extremities.
35
The option xdots/inter fixes the length between the dots. The initial value is 0.45 em (it is
recommended to use a unit of length dependent of the current font).
The commands \Hbrace and \Vbrace have the same syntax. Both commands take in three arguments:
• an optional argument, between square brackets, which contains a list of key=value pairs: the
keys allowed are color, horizontal-labels, shorten, shorten-start and shorten-end.
• a mandatory argument which is the number of columns (for \Hbrace) or the number of rows
(for \Vbrace) over which the command applies;
50 The first reason of this behaviour is that the pdf format includes a description for dashed lines. The lines specified
with this descriptor are displayed very efficiently by the pdf readers. It’s easy, starting from these dashed lines, to
create a line composed by square dots whereas a line of rounded dots needs a specification of each dot in the pdf file.
Nevertheless, you can have a look at the following page to see how to have dotted rules with rounded dots in TikZ:
[Link]
36
• an mandatory argument which is the label of the curly brace.
As regards the ampersands (&), \Hbrace has the same behavior as the commands \multicolumn,
\hdotsfor, \Hdotsfor, etc.: only one ampersand must be inserted, even if the brace that will be
drawn will extend on several columns.
$\begin{NiceArray}{ccccc}%
..[ hvlines ,
....first-row ,
....last-row = 6, p q
....first-col ,
....last-col , 1 1 134 1 1
....xdots/horizontal-labels ] p 1 1 134 1 1 p
In the environments with preamble ({NiceArray}, {pNiceArray}, etc.), it’s possible to put vertical
delimiters directly in the preamble of the environment.52
The opening delimiters should be prefixed by the keyword \left and the closing delimiters by the
keyword \right . It’s not mandatory to use \left and \right pair-wise.
All the vertical extensible delimiters of LaTeX are allowed.
51 On the other side, the command \line in the \CodeAfter (cf. p. 39) does not create block.
52 This syntax is inspired by the extension blkarray.
37
Here is a example which uses the delimiters \lgroup and \rgroup.
$\begin{NiceArray}{\left\lgroup ccc\right\rgroup l}
1 & 2 & 3 &
4 & 1 & 6 &
7 & 8 & 9 & \scriptstyle L_3 \gets L_3 + L_1 + L_2
\end{NiceArray}$
1 2 3
4 1 6
7 8 9 L3 ←L3 +L1 +L2
For this example, it would also have been possible to use the environment {NiceArrayWithDelims}
(cf. the section 14.10, p. 53) and the key last-col (cf. p. 29).
There is a particular case: for the delimiters (, [ and \{53 , and the corresponding closing delimiters,
the prefixes \left et \right are optional.54
Here is an example with a left delimiter \{ in a {NiceTabular} (remark the compatibility with the
key t).
When there are two successive delimiters (necessarily a closing one following by an opening one for
another submatrix), a space equal to \enskip is automatically inserted.
$\begin{pNiceArray}{(c)(c)(c)}
a_{11} & a_{12} & a_{13} \\
a_{21} & \displaystyle \int_0^1\dfrac{1}{x^2+1}\,dx & a_{23} \\
a_{31} & a_{32} & a_{33}
\end{pNiceArray}$
a11 a12 a13
1 1 Z
a21
dx a23
x 2+1
0
a31 a32 a33
For more complex constructions, in particular with delimiters spanning only a subset of the rows
of the array, one should consider the command \SubMatrix available in the \CodeAfter (and the
\CodeBefore). See the section 12.2, p. 40.
12 The \CodeAfter
The option code-after may be used to give some code that will be executed after the construction
of the matrix.55
53 For the braces, the protection by a backslash is mandatory (that’s why we have written \{).
54 For the delimiters [ and ], the prefixes remain mandatory when there is a conflict of notation with the square
brackets for the options of some descriptors of columns.
55 There is also a key code-before described p. 19.
38
For the legibility of the code, an alternative syntax is provided: it’s possible to give the instructions of
the code-after at the end of the environment, after the keyword \CodeAfter (the key code-after
is of course mandatory in the \AutoNiceMatrix and the similar commands). Although \CodeAfter
is a keyword, it takes in an optional argument (between square brackets).56
The experienced users may, for instance, use the PGF/TikZ nodes created by nicematrix in the
\CodeAfter. These nodes are described further beginning on p. 53.
The \CodeAfter provides several special commands: \line, \SubMatrix, \OverBrace, \UnderBrace
and \TikzEveryCell. We will now present these commands.
One should avoid spurious spaces in that \CodeAfter.57
• a specification of cell of the form i-j where is the number of the row and j is the number of the
column;
• the name of a block (created by the command \Block with the key name of that command).
The options available for the customisation of the dotted lines created by \Cdots, \Vdots, etc. are
also available for this command (cf. p. 35).
This command may be used, for example, to draw a dotted line between two adjacent cells.
\NiceMatrixOptions{xdots/shorten = 0.6 em}
\begin{pNiceMatrix}
I 0 0
I & 0 & \Cdots &0 \\
0 & I & \Ddots &\Vdots\\
0 I
\Vdots &\Ddots & I &0 \\
I 0
0 &\Cdots & 0 &I
0 0 I
\CodeAfter \line{2-2}{3-3}
\end{pNiceMatrix}
It can also be used to draw a diagonal line not parallel to the other diagonal lines (by default, the
dotted lines drawn by \Ddots are “parallelized”: cf. p. 59).
\begin{bNiceMatrix}
1 & \Cdots & & 1 & 2 & \Cdots & 2 \\
0 & \Ddots & & \Vdots & \Vdots & \hspace*{2.5cm} & \Vdots \\
\Vdots & \Ddots & & & & & \\
0 & \Cdots & 0 & 1 & 2 & \Cdots & 2
\CodeAfter \line[shorten=6pt]{1-5}{4-7}
\end{bNiceMatrix}
1 1 2 2
0
0 0 1 2 2
56 Here are the keys accepted in that argument: delimiters/color, rules and its sub-keys, sub-matrix (linked to
the command \SubMatrix) and its sub-keys and xdots (for the command \line) and its sub-keys.
57 See: [Link]
39
12.2 The command \SubMatrix in the \CodeAfter (and the \CodeBefore)
The command \SubMatrix provides a way to put delimiters on a portion of the array considered as
a submatrix. The command \SubMatrix takes in five arguments:
• the first argument is the left delimiter, which may be any extensible delimiter provided by
LaTeX : (, [, \{, \langle, \lgroup, \lfloor, etc. but also the null delimiter .;
• the second argument is the upper-left corner of the submatrix with the syntax i-j where i the
number of row and j the number of column (the special word last is allowed);
• the third argument is the lower-right corner with the same syntax;
• the fourth argument is the right delimiter;
• the last argument, which is optional, is a list of key=value pairs.58
One should remark that the command \SubMatrix draws the delimiters after the construction of the
array: no space is inserted by the command \SubMatrix itself. That’s why, in the following example,
we have used the key margin and you have added by hand some space between the third and fourth
column with @{\hspace{1.5em}} in the preamble of the array.
\[\begin{NiceArray}{ccc@{\hspace{1.5em}}c}[cell-space-limits=2pt,margin]
.1 & 1 & 1 & x \\
\dfrac{1}{4} & \dfrac{1}{2} & \dfrac{1}{4} & y \\
1 1 1 x
.1 & 2 & 3 & z 1 1 1
4 2 4 y
\CodeAfter
..\SubMatrix({1-1}{3-3}) 1 2 3 z
..\SubMatrix({1-4}{3-4})
\end{NiceArray}\]
Eventually, in this example, it would probably have been easier to put the delimiters directly in the
preamble of {NiceArray} (see section 11, p. 37) with the following construction.
$\begin{NiceArray}{(ccc)(c)}[cell-space-limits=2pt]
1 1 1
x
.1 & 1 & 1 & x \\ 1 1 1
\dfrac{1}{4} & \dfrac{1}{2} & \dfrac{1}{4} & y \\
4
y
2 4
.1 & 2 & 3 & z 1 2 3 z
\end{NiceArray}$
In fact, the command \SubMatrix also takes in two optional arguments specified by the traditional
symbols ^ and _ for material in superscript and subscript (but no space is reserved for that material).
$\begin{bNiceMatrix}[right-margin=1em]
1 & 1 & 1 \\
1 & a & b \\
1 1 1
T
1 & c & d 1 a b
\CodeAfter 1 c d
..\SubMatrix[{2-2}{3-3}]^{T}
\end{bNiceMatrix}$
58 There is no optional argument between square brackets in first position because a square bracket just after
\SubMatrix must be interpreted as the first (mandatory) argument of the command \SubMatrix: that bracket is
the left delimiter of the sub-matrix to construct (eg.: \SubMatrix[{2-2}{4-7}]).
40
• slim is a boolean key: when that key is in force, the horizontal position of the delimiters is
computed by using only the contents of the cells of the submatrix whereas, in the general case,
the position is computed by taking into account the cells of the whole columns implied in the
submatrix (see example below). ;
• vlines contents a list of numbers of vertical rules that will be drawn in the sub-matrix (if this
key is used without value, all the vertical rules of the sub-matrix are drawn);
• hlines is similar to vlines but for the horizontal rules;
• hvlines , which must be used without value, draws all the vertical and horizontal rules;
• code insert code, especially TikZ code, after the construction of the submatrix. That key is
detailed below.
One should remark that the keys add their rules after the construction of the main matrix: no space
is added between the rows and the columns of the array for these rules.
All these keys are also available in \NiceMatrixOptions, at the level of the environments of nicematrix
or as option of the command \CodeAfter with the prefix sub-matrix which means that their names
are therefore sub-matrix/left-xshift, sub-matrix/right-xshift, sub-matrix/xshift, etc.
$\begin{NiceArray}{cc@{\hspace{5mm}}l}[cell-space-limits=2pt]
..& & \frac{1}{2} \\
..& & \frac{1}{4} \\[1mm] 1
!
a & b & \frac{1}{2}a+\frac{1}{4}b \\ 2
1
c & d & \frac{1}{2}c+\frac{1}{4}d \\ 4
\CodeAfter
! !
1 1
a b 2a + 4b
..\SubMatrix({1-3}{2-3}) 1 1
c d 2c + 4d
..\SubMatrix({3-1}{4-2})
..\SubMatrix({3-3}{4-3})
\end{NiceArray}$
Here is the same example with the key slim used for one of the submatrices.
$\begin{NiceArray}{cc@{\hspace{5mm}}l}[cell-space-limits=2pt]
..& & \frac12 \\
..& & \frac14 \\[1mm] 1
!
a & b & \frac12a+\frac14b \\ 2
1
c & d & \frac12c+\frac14d \\ 4
\CodeAfter
! !
1 1
a b 2a + 4b
..\SubMatrix({1-3}{2-3})[slim] 1 1
c d 2c + 4d
..\SubMatrix({3-1}{4-2})
..\SubMatrix({3-3}{4-3})
\end{NiceArray}$
There is also a key name which gives a name to the submatrix created by \SubMatrix. That name is
used to create PGF/TikZ nodes: cf p. 58.
Despite its name, the command \SubMatrix may also be used within a {NiceTabular}. Here is an
example (which uses \bottomrule and \toprule of booktabs).
\begin{NiceTabular}{@{}ll@{}}
\toprule
Part A & the first part \\
\Block{2-1}{Part B} & a first sub-part \\ Part A the first part
....................& a second sub-part \\ a first sub-part
Part B a second sub-part
\bottomrule
\CodeAfter
..\SubMatrix{\lbrace}{2-2}{3-2}{.}
\end{NiceTabular}
41
The command \SubMatrix is, in fact, also available in the \CodeBefore. By using \SubMatrix in
the \CodeBefore, the delimiters drawn by those commands \SubMatrix are taken into account to
limit the continuous dotted lines (drawn by \Cdots, \Vdots, etc.) which have an open extremity.
For an example, see voir 18.9 p. 74.
Caution : The following functionality is fragile and does not work with latex–dvips–ps2pdf.
The key code of the command \SubMatrix allows the insertion of code after the construction of
the submatrix. It’s meant to be used to insert TikZ instructions because, in the TikZ instructions
inserted by that code, the nodes of the form [Link] or i-|j are interpreted with i and j as
numbers of row and column relative to the submatrix.59
$\begin{NiceArray}{ccc@{}w{c}{5mm}@{}ccc}
....& & && -1 & 1 & 2 \\
....& & && 0 & 3 & 4 \\
....& & && 0 & 0 & 5 \\
..1 & 2 & 3 && -1 & 7 & 25 \\
..0 & 4 & 5 && 0 & 12 & 41 \\
..0 & 0 & 6 && 0 & 0 & 30
\CodeAfter
..\NewDocumentCommand{\MyDraw}{}{\tikz \draw [blue] (2-|1) -| (3-|2) -|
......(4-|3) ;}
..\SubMatrix({1-5}{3-7})[code = \MyDraw ]
..\SubMatrix({4-1}{6-3})[code = \MyDraw ]
..\SubMatrix({4-5}{6-7})[code = \MyDraw ]
\end{NiceArray}$
−1 1 2
0 3 4
0 0 5
1 2 3 −1 7 25
0 4 5 0 12 41
0 0 6 0 0 30
As we see, the drawing done by our command \MyDraw is relative to the submatrix to which it is
applied.
42
\begin{pNiceMatrix}
1 & 2 & 3 & 4 & 5 & 6 \\ A B
11 & 12 & 13 & 14 & 15 & 16 \\ z }|
1 2 3
{z }|
4 5 6
{
\CodeAfter 11 12 13 14 15 16
..\OverBrace{1-1}{2-3}{A}
..\OverBrace{1-4}{2-6}{B}
\end{pNiceMatrix}
Caution: There is no vertical space reserved for those braces and their labels.60
In fact, the commands \OverBrace and \UnderBrace take in an optional argument (in first position
and between square brackets) for a list of key=value pairs. The available keys are:
• left-shorten and right-shorten which do not take in value; when the key left-shorten is
used, the abscissa of the left extremity of the brace is computed with the contents of the cells
of the involved sub-array, otherwise, the position of the potential vertical rule is used (idem for
right-shorten).
• shorten , which is the conjunction of the keys left-shorten and right-shorten;
• yshift , which shifts vertically the brace (and its label);
• color, which sets the color of the brace (and its label).
\begin{pNiceMatrix}
1 & 2 & 3 & 4 & 5 & 6 \\ A B
11 & 12 & 13 & 14 & 15 & 16 \\ z }| { z }| {
1 2 3 4 5 6
\CodeAfter 11 12 13 14 15 16
..\OverBrace[shorten,yshift=3pt]{1-1}{2-3}{A}
..\OverBrace[shorten,yshift=3pt]{1-4}{2-6}{B}
\end{pNiceMatrix}
\renewcommand{\arraystretch}{1.3}
\begin{NiceTabular}{ccc}[corners]
..& \Block{1-2}{columns} \\
..\Block{2-1}{rows} columns
..& cell 1 1 & cell 1 2 \\ cell 1 1 cell 1 2
rows
..& cell 2 1 & cell 2 2 cell 2 1 cell 2 2
\CodeAfter
..\TikzEveryCell{offset=1pt,draw}
\end{NiceTabular}
The command \TikzEveryCell has two optional keys, available between square brackets.
60 See: [Link]
43
• with the key empty , the command only acts on the empty cells (the exact definition of an
“empty cell” is given in part 17.2, p. 60);
• with key non-empty , the command only acts on the non-empty cells.
\renewcommand{\arraystretch}{1.4}
\begin{NiceTabular}{cccccc}[hvlines] P O U R V U
..P & O & U & R & V & U \\
..O & & & E & I & \\ O E I
..M & O & R & F & A & L \\ M O R F A L
..E & T & A & L & & E \\
..L & A & S & E & R & S \\ E T A L E
..O & & E & X & I & T L A S E R S
\CodeAfter
O E X I T
..\TikzEveryCell[empty]{fill=gray,draw}
\end{NiceTabular}
The command \TikzEveryCell is, in fact, also available in the \CodeBefore.
44
13.3 The notes of tabular
The package nicematrix also provides a command \tabularnote which gives the ability to specify
notes that will be composed at the end of the array with a width of text equal to the width of the array
(except the potential exterior columns specified by first-col and last-col: cf. 9, p. 29). With no
surprise, that command is available only in the environments {NiceTabular}, {NiceTabular*} and
{NiceTabularX}.
In fact, this command is available only if the extension enumitem has been loaded (before or after
nicematrix). Indeed, the notes are composed at the end of the array with a type of list provided by
the package enumitem.
\begin{NiceTabular}{@{}llr@{}}
\toprule \RowStyle{\bfseries}
Last name & First name & Birth day \\
\midrule
Achard\tabularnote{Achard is an old family of the Poitou.}
& Jacques & June 5, 2005 \\
Lefebvre\tabularnote{The name Lefebvre is an alteration of the name Lefebure.}
& Mathilde & January 23, 1975 \\
Vanesse & Stephany & October 30, 1994 \\
Dupont & Chantal & January 15, 1998 \\
\bottomrule
\end{NiceTabular}
• If you have several successive commands \tabularnote{...} with no space at all between
them, the labels of the corresponding notes are composed together, separated by commas (this
is similar to the option multiple of footmisc for the footnotes).
• If a command \tabularnote{...} is exactly at the end of a cell (with no space at all after)
and if the alignment mode of the column is c or r, the label of the note is composed in an
overlapping position (towards the right). This structure may provide a better alignment of the
cells of a given column.
• If the key notes/para is used, the notes are composed at the end of the array in a single
paragraph (as with the key para of threeparttable).
• There is a key tabularnote which provides a way to insert some text in the zone of the notes
before the numbered tabular notes.
An alternative syntax is available with the environment {TabularNote}. That environment
should be used at the end of the environment {NiceTabular} (but before a potential instruction
\CodeAfter).
• If the package booktabs has been loaded (before or after nicematrix), the key notes/bottomrule
draws a \bottomrule of booktabs after the notes.
• The command \tabularnote may be used before the environment of nicematrix. Thus, it’s
possible to use it on the title inserted by \caption in an environment {table} of LaTeX (or
in a command \captionof of the package caption). It’s also possible, as expected, to use
the command \tabularnote in the caption provided by the key caption of the environment
{NiceTabaular}.
45
If several commands \tabularnote are used in a tabular with the same argument, only one
note is inserted at the end of the tabular (but all the labels are composed, of course). It’s
possible to control that feature with the key notes/detect-duplicates.61
• It’s possible to create a reference to a tabular note created by \tabularnote (with the usual
command \label used after the \tabularnote).
• The command \tabularnote has an optional argument (between square brackets) to change
the symbol of the reference of the note.
Example: \tabularnote[$\star$]{A footnote...}
For an illustration of some of those remarks, see table 1, p. 47. This table has been composed with
the following code (the package caption has been loaded in this document).
\begin{table}
\centering
\NiceMatrixOptions{caption-above}
\begin{NiceTabular}{@{}llc@{}
..[
....caption = A tabular whose caption has been specified by the key
......\texttt{caption}\tabularnote[$\star$]{It's possible to put a tabular
......note in the caption.} ,
....label = t:tabularnote ,
....tabularnote = Some text before the notes. ,
....notes/bottomrule
..]
\toprule
Last name & First name & Length of life \\
\midrule
Churchill & Wiston & 91\\
Nightingale\tabularnote{Considered as the first nurse of history}%
\tabularnote{Nicknamed ``the Lady with the Lamp''.}
& Florence\tabularnote{This note is shared by two references.} & 90 \\
Schoelcher & Victor & 89\tabularnote{The label of the note is overlapping.}\\
Touchet & Marie\tabularnote{This note is shared by two references.} & 89 \\
Wallis & John & 87 \\
\bottomrule
\end{NiceTabular}
\end{table}
• notes/bottomrule
• notes/style
• notes/label-in-tabular
• notes/label-in-list
• notes/enumitem-keys
• notes/enumitem-keys-para
61 For technical reasons, the final user is not allowed to put several commands \tabularnote with exactly the same
46
Table 1: A tabular whose caption has been
specified by the key caption?
• notes/code-before
For sake of commodity, it is also possible to set these keys in \NiceMatrixOptions via a key notes
which takes in as value a list of pairs key=value where the name of the keys need no longer be prefixed
by notes:
\NiceMatrixOptions
{
notes =
{
bottomrule ,
style = ... ,
label-in-tabular = ... ,
enumitem-keys =
{
labelsep = ... ,
align = ... ,
...
}
}
}
• The key notes/para requires the composition of the notes (at the end of the tabular) in a
single paragraph.
Initial value: false
That key is also available within a given environment.
• The key notes/bottomrule adds a \bottomrule of booktabs after the notes. Of course, that
rule is drawn only if there is really notes in the tabular. The package booktabs must have been
loaded (before or after the package nicematrix). If it is not, an error is raised.
Initial value: false
That key is also available within a given environment.
• The key notes/style is a command whose argument is specified by #1 and which gives the
style of numerotation of the notes. That style will be used by \ref when referencing a tabular
note marked with a command \label. The labels formatted by that style are used, separated
by commas, when the user puts several consecutive commands \tabularnote. The marker #1
is meant to be the name of a LaTeX counter.
47
Initial value: \textit{\alph{#1}}
Another possible value should be a mere \arabic{#1}
The command \nobreak is for the event that the option para is used.
• The notes are composed at the end of the tabular by using internally a style of list of enumitem.
This style of list is defined as follows (with, of course, keys of enumitem):
noitemsep , leftmargin = * , align = left , labelsep = 0pt
The specification align = left in that style requires a composition of the label leftwards in
the box affected to that label. With that tuning, the notes are composed flush left, which is
pleasant when composing tabulars in the spirit of booktabs (see for example the table 1, p. 47).
The key notes/enumitem-keys specifies a list of pairs key=value (following the specifications
of enumitem) to customize that style of list (it uses internally the command \setlist* of
enumitem).
• The key notes/enumitem-keys-para is similar to the previous one but corresponds to the type
of list used when the option para is in force. Of course, when the option para is used, a list
of type inline (as called by enumitem) is used and the pairs key=value should correspond to
such a list of type inline.
Initially, the style of list is defined by: afterlabel = \nobreak, itemjoin = \quad
• The key notes/code-before is a token list inserted by nicematrix just before the composition
of the notes at the end of the tabular.
Initial value: empty
For example, if one wishes to compose all the notes in gray and \footnotesize, he should use
that key:
\NiceMatrixOptions{notes/code-before = \footnotesize \color{gray}}
It’s also possible to add \raggedright or \RaggedRight in that key (\RaggedRight is a com-
mand of ragged2e).
48
13.5 Use of {NiceTabular} with threeparttable
If you wish to use an environment {NiceTabular}, {NiceTabular*} or {NiceTabularX} in an envi-
ronment {threeparttable} of the eponymous package, you have to patch {threeparttable} with
the following code.
\makeatletter
\AddToHook{env/threeparttable/begin}
..{\TPT@hookin{NiceTabular}\TPT@hookin{NiceTabular*}\TPT@hookin{NiceTabularX}}
\makeatother
Nevertheless, the use of threeparttable in conjunction with nicematrix seems rather point-less because
of the functionalities provided by nicematrix (see the key caption in the section 13.1, p. 44).
14 Other features
\begin{NiceTabular}
..[hvlines,rounded-corners]
..{ccc}
\CodeBefore Last name First name Profession
..\rowcolor{red!15}{1} Arvy Jacques Physicist
\Body
Jalon Amandine Physicist
Last name & First name & Profession \\
Arvy & Jacques & Physicist \\
Jalon & Amandine & Physicist
\end{NiceTabular}
49
14.3 Use of the column type S of siunitx
If the package siunitx is loaded (before or after nicematrix), it’s possible to use the S column type of
siunitx in the environments of nicematrix.
$\begin{pNiceArray}{ScW{c}{1cm}c}[nullify-dots,first-row]
{C_1} & \Cdots & & C_n \\ C1 Cn
2.3 0 0
2.3 & 0 & \Cdots & 0 \\ 12.4
12.4 & \Vdots & & \Vdots \\
1.45
1.45 \\
7.2 0 0
7.2 & 0 & \Cdots & 0
\end{pNiceArray}$
On the other hand, the d columns of the package dcolumn are not supported by nicematrix.
\NiceMatrixOptions%
.{code-for-first-row = \scriptstyle \rotate \text{image of },
image of e1
image of e2
image of e3
..code-for-last-col = \scriptstyle }
$A = \begin{pNiceMatrix}[first-row,last-col=4]
e_1 & e_2 & e_3 \\
1 & 2 & 3 & e_1 \\ 1 2 3 e1
4 & 5 & 6 & e_2 \\ A = 4 5 6 e2
7 & 8 & 9 & e_3 7 8 9 e3
\end{pNiceMatrix}$
If the command \rotate is used in the “last row” (exterior to the matrix), the corresponding elements
are aligned upwards as shown below.
\NiceMatrixOptions%
.{code-for-last-row = \scriptstyle \rotate ,
1 2 3 e1
..code-for-last-col = \scriptstyle } A = 4 5 6e2
$A = \begin{pNiceMatrix}[last-row=4,last-col=4] 7 8 9 e3
1 & 2 & 3 & e_1 \\
image of e1
e2
e3
50
The command \rotate has a key c: \rotate[c] (spaces are deleted after \rotate[c]). When that
key is used, the content is composed in a \vcenter and, therefore, in most cases, we will have a
vertical alignment.
$\begin{bNiceArray}{cccc|c}[small,
............................last-col ,
............................code-for-last-col = \scriptscriptstyle,
............................columns-width = 3mm ]
1 & -2 & 3 & 4 & 5 \\
0 & 3 & 2 & 1 & 2 & L_2 \gets 2 L_1 - L_2 \\
0 & 1 & 1 & 2 & 3 & L_3 \gets L_1 + L_3
\end{bNiceArray}$
h 1 −2 3 4 5 i
0 3 2 1 2 L2 ←2L1 −L2
0 1 1 2 3 L3 ←L1 +L3
One should note that the environment {NiceMatrix} with the option small is not composed exactly
as the environment {smallmatrix}. Indeed, all the environments of nicematrix are constructed upon
{array} (of the package array) whereas the environment {smallmatrix} is constructed directly with
an \halign of TeX.
In fact, the option small corresponds to the following tuning:
• the cells of the array are composed with \scriptstyle;
When the key small is in force, some functionalities of nicematrix are no longer available: for example,
it’s no longer possible to put vertical delimiters directly in the preamble of an environment with
preamble (cf. section 11, p. 37).
65 There counters are actual LaTeX counters: the underlying TeX counters are \c@iRow and \c@jCol
51
$\begin{pNiceMatrix}% don’t forget the %
....[first-row,
.....first-col,
.....code-for-first-row = \mathbf{\alph{jCol}} , a b c d
1 1 2 3 4
.....code-for-first-col = \mathbf{\arabic{iRow}} ] 25 6 7 8
& & & & \\ 3 9 10 11 12
& 1 & 2 & 3 & 4 \\
& 5 & 6 & 7 & 8 \\
& 9 & 10 & 11 & 12
\end{pNiceMatrix}$
If LaTeX counters called iRow and jCol are defined in the document by packages other than nicematrix
(or by the final user), they are shadowed in the environments of nicematrix.
The package nicematrix also provides commands in order to compose automatically matrices from a
general pattern. These commands are \AutoNiceMatrix, \pAutoNiceMatrix, \bAutoNiceMatrix ,
\vAutoNiceMatrix , \VAutoNiceMatrix et \BAutoNiceMatrix.
These commands take in two mandatory arguments. The first is the format of the matrix, with the
syntax n-p where n is the number of rows and p the number of columns. The second argument is the
pattern (it’s a list of tokens which are inserted in each cell of the constructed matrix).
$C = \pAutoNiceMatrix{3-3}{C_{\arabic{iRow},\arabic{jCol}}}$
C1,1 C1,2 C1,3
C = C2,1 C2,2 C2,3
C3,1 C3,2 C3,3
There exists also \AutoNiceArrayWithDelims similar to {NiceArrayWithDelims}.
66 The reason is that, when the option light-syntax is used, the whole content of the environment is loaded as a TeX
argument to be analyzed. The environment doesn’t behave in that case as a standard environment of LaTeX which
only put TeX commands before and after the content.
67 More precisely, it’s an expansion of type e of L3.
52
$\begin{bNiceMatrix}[delimiters/color=red]
1 & 2 \\
1 2
3 & 4 3 4
\end{bNiceMatrix}$
This color also applies to the delimiters drawn by the command \SubMatrix (cf. p. 40) and for the
delimiters directly specified in the preamble of the environments with preamble (cf. p. 37).
\end{NiceArrayWithDelims}$
68 We recall that TikZ is a layer over PGF. The extension nicematrix loads PGF but does not load TikZ. We speak of
PGF/TikZ nodes to emphase the fact that the PGF nodes created by nicematrix may be used with PGF but also with
TikZ. The final user will probably prefer to use TikZ rather than PGF.
69 One should note that, with that command, the cell is considered as non-empty, which has consequencies for the
continuous dotted lines (cf. p. 31) and the computation of the “corners” (cf. p. 14).
53
However, it’s advisable to use instead the key name 70 . This key gives a name to the current environ-
ment. When the environment has a name, the nodes are accessible with the name “name-i-j” where
name is the name given to the array and i and j the numbers of row and column. It’s possible to
use these nodes with pgf but the final user will probably prefer to use TikZ (which is a convenient
layer upon pgf). However, one should remind that nicematrix doesn’t load TikZ by default. In the
following examples, we assume that TikZ has been loaded.
$\begin{pNiceMatrix}[name=mymatrix]
1 & 2 & 3 \\
4 & 5 & 6 \\
1 2 3
7 & 8 & 9 4 5 6
\end{pNiceMatrix}$ 7 8 9
\tikz[remember picture,overlay]
.....\draw (mymatrix-2-2) circle (2mm) ;
Don’t forget the options remember picture and overlay.
In the \CodeAfter, the things are easier : one must refer to the nodes with the form i-j (we don’t
have to indicate the environment which is of course the current environment).
$\begin{pNiceMatrix}
1 & 2 & 3 \\
4 & 5 & 6 \\
1 2 3
7 & 8 & 9 4 5 6
\CodeAfter 7 8 9
..\tikz \draw (2-2) circle (2mm) ;
\end{pNiceMatrix}$
The nodes of the last column (except the potential “last column” specified by last-col71 ) may also
be indicated by i-last. Similarly, the nodes of the last row may be indicated by last-j.
In the following example, we have stroken all the nodes of the matrix.
a a+b a+b+c
a a a+b
a a a
Since those nodes are PGF nodes, one won’t be surprised to learn that they are drawn by using a
specific PGF style. That style is called nicematrix/cell-node and its definition in the source file
[Link] is as follows:
\pgfset
..{
....nicematrix / cell-node /.style =
.....{
.......inner sep = 0 pt ,
.......minimum width = 0 pt
.....}
..}
The final user may modify that style by changing the values of the keys text/rotate, inner xsep,
inner ysep, inner sep, outer xsep, outer ysep, outer sep, minimum width, minimum height
and minimum size.
For an example of utilisation, see part 18.10, p. 75.
70 The value of the key name is expanded (in the TeX sens).
71 For the exterior columns, cf. part 9, p. 29.
54
15.1.1 The key pgf-node-code
For the experienced users, nicematrix provides the key pgf-node-code which corresponds to
some PGF node that will be executed at the creation, by PGF, of the nodes corresponding to the
cells of the array. More pricisely, the value given to the key pgf-node-code will be passed in the
fifth argument of the command \pgfnode. That value should contain at least an instruction such as
\pgfusepath, \pgfusepathqstroke, \pgfusepathqfill, etc.
Titre
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus
elit, vestibulum ut, placerat ac, adipiscing vitae, felis. Curabitur
dictum gravida mauris. Nam arcu libero, nonummy eget, con-
sectetuer id, vulputate a, magna.
We have used the nodes corresponding to the position of the potential rules, which are described
below (cf. p. 57).
The names of the “large nodes” are constructed by adding the suffix “-large” to the names of the
“normal nodes”. In the following example, we have underlined the “large nodes”. We consider that
this example is self-explanatory.73
a a+b a+b+c
a a a+b
a a a
72 There is also an option create-extra-nodes which is an alias for the conjunction of create-medium-nodes and
create-large-nodes.
73 There is no “large nodes” created in the exterior rows and columns (for these rows and columns, cf. p. 29).
55
The “large nodes” of the first column and last column may appear too small for some usage. That’s
why it’s possible to use the options left-margin and right-margin to add space on both sides of
the array and also space in the “large nodes” of the first column and last column. In the following
example, we have used the options left-margin and right-margin.74
a a+b a+b+c
a a a+b
a a a
It’s also possible to add more space on both side of the array with the options extra-left-margin
and extra-right-margin. These margins are not incorporated in the “large nodes”. It’s possible to
fix both values with the option extra-margin and, in the following example, we use extra-margin
with the value 3 pt.
a a+b a+b+c
a a a+b
a a a
Be careful : These nodes are reconstructed from the contents of the contents cells of the array.
Usually, they do not correspond to the cells delimited by the rules (if we consider that these rules are
drawn).
The nodes we have described are not available by default in the \CodeBefore (described p. 19).
It’s possible to have these nodes available in the \CodeBefore by using the key create-cell-nodes
of the keyword \CodeBefore (in that case, the nodes are created first before the construction of the
array by using information written on the aux file and created a second time during the construction
of the array itself; this mechanism is not activated by default for efficiency reasons).
74 The options left-margin and right-margin take dimensions as values but, if no value is given, the default value
is used, which is \arraycolsep (by default: 5 pt). There is also an option margin to fix both left-margin and
right-margin to the same value.
56
\begin{NiceArray}{c@{\;}c@{\;}c@{\;}c@{\;}c}[create-medium-nodes]
....u_1 &-& u_0 &=& r \\
....u_2 &-& u_1 &=& r \\
....u_3 &-& u_2 &=& r \\
....u_4 &-& u_3 &=& r \\
....\phantom{u_5} & & \phantom{u_4} &\smash{\vdots} & \\
....u_n &-& u_{n-1} &=& r \\[3pt]
....\hline
....u_n &-& u_0 &=& nr \\
\CodeAfter
....\tikz[very thick, red, opacity=0.4, name suffix = -medium]
....\draw ([Link] west) -- ([Link] east)
....([Link] west) -- ([Link] east)
....([Link] west) -- ([Link] east)
....([Link] west) -- ([Link] east)
....([Link] west) -- ([Link] east) ;
\end{NiceArray}
u1 − u0 = r
u2 − u1 = r
u3 − u2 = r
u4 − u3 ..
= r
.
un − un−1 = r
un − u0 = nr
If we use TikZ (we remind that nicematrix does not load TikZ by default, by only pgf, which is a
sub-layer of TikZ), we can access, in the \CodeAfter but also in the \CodeBefore, to the intersection
of the (potential) horizontal rule i and the (potential) vertical rule j with the syntax (i-|j).
\begin{NiceMatrix}
\CodeBefore
..\tikz \draw [fill=red!15] (7-|4) |- (8-|5) |- (9-|6) |- cycle ;
\Body
1 \\
1 & 1 \\
1 & 2 & 1 \\
1 & 3 & 3 & 1 \\
1 & 4 & 6 & 4 & 1 \\
1 & 5 & 10 & 10 & 5 & 1 \\
57
1 & 6 & 15 & 20 & 15 & 6 & 1 \\
1 & 7 & 21 & 35 & 35 & 21 & 7 & 1 \\
1 & 8 & 28 & 56 & 70 & 56 & 28 & 8 & 1
\end{NiceMatrix}
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
The “decimal” nodes (like i.4) may be used, for example to cross a row of a matrix (if TikZ is loaded).
$\begin{pNiceArray}{ccc|c}
2 & 1 & 3 & 0 \\
3 & 3 & 1 & 0 \\
2 1 3 0
3 & 3 & 1 & 0 3 3 1 0
\CodeAfter 3 3 1 0
..\tikz \draw [red] (3.4-|1) -- (3.4-|last) ;
\end{pNiceArray}$
The package nicematrix provides two variables which are internal but public75 :
• \g_nicematrix_code_before_tl ;
• \g_nicematrix_code_after_tl.
These variables contain the code of what we have called the “code-before” (usually specified at the
beginning of the environment with the syntax using the keywords \CodeBefore and \Body) and the
“code-after” (usually specified at the end of the environment after the keyword \CodeAfter). The
75 According to the LaTeX3 conventions, each variable with name beginning with \g_nicematrix ou \l_nicematrix
is public and each variable with name beginning with \g__nicematrix or \l__nicematrix is private.
58
developer can use them to add code from a cell of the array (the affectation must be global, allowing
to exit the cell, which is a TeX group).
One should remark that the use of \g_nicematrix_code_before_tl needs one compilation more
(because the instructions are written on the aux file to be used during the next run).
Example : We want to write a command \crossbox to draw a cross in the current cell. This command
will take in an optional argument between square brackets for a list of pairs key-value which will be
given to TikZ before the drawing.
It’s possible to program such command \crossbox as follows, explicitly using the public variable
\g_nicematrix_code_before_tl.
\ExplSyntaxOn
\cs_new_protected:Nn \__pantigny_crossbox:nnn
..{
....\tikz \draw [ #3 ]
..........( #1 -| \int_eval:n { #2 + 1 } ) -- ( \int_eval:n { #1 + 1 } -| #2 )
..........( #1 -| #2 ) -- ( \int_eval:n { #1 + 1 } -| \int_eval:n { #2 + 1 } ) ;
..}
\NewDocumentCommand \crossbox { ! O { } }
..{
....\tl_gput_right:Ne \g_nicematrix_code_before_tl
......{
........\__pantigny_crossbox:nnn
..........{ \int_use:c { c@iRow } }
..........{ \int_use:c { c@jCol } }
..........{ \exp_not:n { #1 } }
......}
....\ignorespaces
..}
\ExplSyntaxOff
We have used the LaTeX counters iRow and jCol provided by nicematrix (cf. p. 51).
17 Technical remarks
First remark: the package underscore must be loaded before nicematrix. If it is loaded after, an error
will be raised.
76 We speak of the lines created by \Ddots and not the lines created by a command \line in the \CodeAfter.
59
around the left-most extremity of the line). That’s why the position of the instructions \Ddots in
the array can have a marked effect on the final result.
In the following examples, the first \Ddots instruction is written in color.
Example with parallelization (default):
$A = \begin{pNiceMatrix}
1 1
1 & \Cdots & & 1 \\
a + b
a+b & \Ddots & & \Vdots \\
A=
\Vdots & \Ddots & & \\
a+b & \Cdots & a+b & 1 a+b a+b 1
\end{pNiceMatrix}$
$A = \begin{pNiceMatrix}
1 1
1 & \Cdots & & 1 \\
a+b & & & \Vdots \\
a + b
A=
\Vdots & \Ddots & \Ddots & \\
a+b & \Cdots & a+b & 1
a+b a+b 1
\end{pNiceMatrix}$
It’s possible to turn off the parallelization with the option parallelize-diags set to false:
1 1
a + b
The same example without parallelization:
A=
a+b a+b 1
It’s possible to specify the instruction \Ddots which will be drawn first (and which will be used to
draw the other diagonal dotted lines when the parallelization is in force) with the key draw-first:
\Ddots[draw-first].
\begin{pmatrix}
a & b \\
c \\
\end{pmatrix}
• For the columns of type p, m, b, V77 and X78 , the cell is empty if (and only if) its content in the
TeX code is empty (there is only spaces between the ampersands &).
• For the columns of type c, l, r and w{...}{...}, the cell is empty if (and only if) its TeX
output has a width equal to zero.
77 The columns of type V are provided by varwidth, which must be loaded: cf. p. 27.
78 See p. 28
60
• A cell containing the command \NotEmpty is not empty (and a PGF/TikZ node is created in
that cell).
• A cell with only a command \Hspace (or \Hspace*) is empty. This command \Hspace is a
command defined by the package nicematrix with the same meaning as \hspace except that
the cell where it is used is considered as empty. This command can be used to fix the width of
some columns of the matrix without interfering with nicematrix.
17.4 Incompatibilities
There might be incompatibilities between nicematrix and babel with the language which activate (in
the TeX sens) some characters, in particular the character <.
Par exemple, for Spanish, it’s recommended to deactivate the abreviations at load-time with the
instruction:
\usepackage[spanish,es-noshorthands]{babel}
The extension nicematrix is usually not compatible with the classes and packages that redefine the
environments {tabular} and {array}. In particular, it’s the case of the class socg-lipics-v2021.
However, in that case, it’s possible to load the class with the key notab which requires that the
environment {tabular} is not redefined.
The package nicematrix is not compatible with the class ieeeaccess because that class is not compatible
with PGF/TikZ. However, there is a simple workaround by writing:81
\let\TeXyear\year
\documentclass{IEEEaccess}
\let\year\TeXyear
In order to use nicematrix with the class aastex631 (of the American Astronomical Society), you have
to add the following instructions in the preamble of your document :
\BeforeBegin{NiceTabular}{\let\begin\BeginEnvironment\let\end\EndEnvironment}
\BeforeBegin{NiceArray}{\let\begin\BeginEnvironment}
\BeforeBegin{NiceMatrix}{\let\begin\BeginEnvironment}
The package nicematrix is not fully compatible with the packages and classes of LuaTeX-ja: the
detection of the empty corners (cf. p. 14) may be wrong in some circonstances.
The package nicematrix is not fully compatible with the package arydshln (because this package
redefines many internals of array) and does not support the columns V of boldline (because the letter V
is reserved for the columns V of varwidth). By any means, nicematrix provides, with the key custom-
line (cf. part 5.3.5, p. 16), tools to draw dashed rules and rules of different widths.
79 In the documentation of amsmath, we can read: The extra space of \arraycolsep that array adds on each side is a
waste so we remove it [in {matrix}] (perhaps we should instead remove it from array in general, but that’s a harder
task).
80 And not by inserting @{} on both sides of the preamble of the array. As a consequence, the length of the \hline
is not modified and may appear too long, in particular when using square brackets.
81 See [Link]
61
The columns d of dcolumn are not supported (but it’s possible to use the columns S of siunitx).
\DocumentMetadata{tagging = on}
\documentclass{article}
\usepackage{lmodern}
\usepackage{nicematrix}
\begin{document}
\begin{center}
\tagpdfsetup{table/header-rows=1}
\begin{NiceTabular}{ccc}[hvlines]
First name & Last name & Age \\
Paul & Imbert & $66$ \\
John & Sarrus & $23$ \\
Liz & Taylor & $100$ \\
George & Adams & $34$
\end{NiceTabular}
\end{center}
\end{document}
18 Examples
62
In the following example, we use the key tikz to hatch a row of the tabular. Remark that you use
the key transparent of the command \Block in order to have the rules drawn in the block.83
\begin{NiceTabular}{ccc}[hvlines]
\CodeBefore
..\columncolor[RGB]{169,208,142}{2}
\Body
one & two & three \\
\Block[transparent, tikz={pattern = north west lines, pattern color = gray}]{1-*}{}
four & five & six \\
seven & eight & nine
\end{NiceTabular}
\tcbox
..[
....left = 0mm ,
....right = 0mm ,
....top = 0mm ,
....bottom = 0mm ,
....boxsep = 0mm ,
....toptitle = 0.5mm ,
....bottomtitle = 0.5mm ,
....title = My table
..]
..{
....\renewcommand{\arraystretch}{1.2}% <-- the % is mandatory here
....\begin{NiceTabular}{rcl}[hvlines-except-borders,rules/color=blue!50!black]
....\CodeBefore
......\rowcolor{red!15}{1}
....\Body
......One & Two & Three \\
......Men & Mice & Lions \\
......Upper & Middle & Lower
....\end{NiceTabular}
..}
83 By default, the rules are not drawn in the blocks created by the command \Block (cf. section 5 p. 11): with the
key transparent, they are drawn (the block becomes transparent to the exterior rules).
63
My table
One Two Three
Men Mice Lions
Upper Middle Lower
84 Of course, it’s realistic only when there is very few notes in the tabular.
85 In fact: the value of its argument.
64
Last name First name Birth day
Achard? Jacques June 5, 2005
Lefebvre?? Mathilde January 23, 1975
Vanesse Stephany October 30, 1994
Dupont Chantal January 15, 1998
?
Achard is an old family of the Poitou.
??
The name Lefebvre is an alteration of the
name Lefebure.
\setlength{\extrarowheight}{1mm}
\[\begin{vNiceArray}{cccc:ccc}[columns-width=6mm]
a_0 & && &b_0 & & \\
a_1 &\Ddots&& &b_1 &\Ddots& \\
\Vdots&\Ddots&& &\Vdots &\Ddots&b_0 \\
a_p & &&a_0 & & &b_1 \\
&\Ddots&&a_1 &b_q & &\Vdots\\
& &&\Vdots & &\Ddots& \\
& &&a_p & & &b_q
\end{vNiceArray}\]
a0 b0
a1 b1
b0
ap a0 b1
a1 bq
ap bq
\begin{pNiceMatrix}[vlines=-1,nullify-dots,last-col,code-for-last-col=\scriptstyle]
1 & 1 & 1 &\Cdots & & 1 & 0 & \\
0 & 1 & 0 &\Cdots & & 0 & & L_2 \gets L_2-L_1 \\
0 & 0 & 1 &\Ddots & & \Vdots & & L_3 \gets L_3-L_1 \\
& & &\Ddots & & & \Vdots & \Vdots \\
\Vdots & & &\Ddots & & 0 & \\
0 & & &\Cdots & 0 & 1 & 0 & L_n \gets L_n-L_1
\end{pNiceMatrix}$
1 1 1 1 0
0 1 0 0 L2 ←L2 −L1
0 0 1 L3 ←L3 −L1
0
0 0 1 0 Ln ←Ln −L1
65
18.5 Dotted lines which are no longer dotted
The option line-style controls the style of the lines drawn by \Ldots, \Cdots, etc. Thus, it’s
possible with these commands to draw lines which are not longer dotted (TikZ should be loaded).
\setcounter{MaxMatrixCols}{12}
\newcommand{\blue}{\color{blue}}
\[\begin{pNiceMatrix}[last-row,last-col,nullify-dots,xdots/line-style={dashed,blue}]
1& & & \Vdots & & & & \Vdots \\
& \Ddots[line-style=standard] \\
& & 1 \\
\Cdots & & & \blue 0 & \Cdots & & & \blue 1 & & & \Cdots & \blue \leftarrow i \\
& & & & 1 \\
& & &\Vdots & & \Ddots[line-style=standard] & & \Vdots \\
& & & & & & 1 \\
\Cdots & & & \blue 1 & \Cdots & & \Cdots & \blue 0 & & & \Cdots & \blue \leftarrow j \\
& & & & & & & & 1 \\
& & & & & & & & & \Ddots[line-style=standard] \\
& & & \Vdots & & & & \Vdots & & & 1 \\
& & & \blue \overset{\uparrow}{i} & & & & \blue \overset{\uparrow}{j} \\
\end{pNiceMatrix}\]
1
1
0 1 ← i
1
1
1 0 ← j
1
1
↑ ↑
i j
66
In fact, it’s even possible to draw solid lines with the commands \Cdots, \Vdots, etc.86
\NiceMatrixOptions{xdots={horizontal-labels,line-style = <->}}
$\begin{pNiceArray}{ccc|cc}[first-row,last-col,margin]
3 2
\Hdotsfor{3}^{3} & \Hdotsfor{2}^{2} \\
2 & 1 & 1 & 1 & 1 & \Vdotsfor{3}^{3}\\
2 1 1 1 1
1 & 1 & 1 & 1 & 1 \\ 1 1 1 1 1 3
If you want the label on the line, you should use the special token “:” instead of “^”:
\NiceMatrixOptions{xdots={horizontal-labels,line-style = <->}}
$\begin{pNiceArray}{ccc|cc}[first-row,last-col,margin]
\Hdotsfor{3}:{3} & \Hdotsfor{2}:{2} \\
3 2
2 & 1 & 1 & 1 & 1 & \Vdotsfor{3}:{3} \\
2 1 1 1 1
1 & 1 & 1 & 1 & 1 \\ 1 1 1 1 1 3
1 & 1 & 1 & 1 & 1 \\
1 1 1 1 1
\Hline
1 1 1 1 1
1 & 1 & 1 & 1 & 1 & lignes
2
1 1 1 1 1
\Vdotsfor{2}:{2 \rlap{ \smash{rows}}} \\
1 & 1 & 1 & 1 & 1 \\
\end{pNiceArray}$
If one prefers the braces of the library [Link] of TikZ, the best way is to use the
commands \Hbrace and \Vbrace provided by nicematrix (cf. p. 36).87
\NiceMatrixOptions{xdots/horizontal-labels}
$\begin{pNiceArray}{ccc|cc}[first-row,last-col,margin] 3 2
\Hbrace{3}{3} & \Hbrace{2}{2} \\
2 1 1 1 1
2 & 1 & 1 & 1 & 1 & \Vbrace{3}{3} \\ 1 1 1 1 1 3
1 & 1 & 1 & 1 & 1 \\
1 1 1 1 1
1 & 1 & 1 & 1 & 1 \\
1 1 1 1 1
\Hline
2
1 1 1 1 1
1 & 1 & 1 & 1 & 1 & \Vbrace{2}{2} \\
1 & 1 & 1 & 1 & 1 \\
\end{pNiceArray}$
86 Inthis document, the TikZ library [Link] has been loaded, which impacts the shape of the arrow tips.
87 Remark that those commands are available only if TikZ has been loaded with the library [Link]:
\usepackage{TikZ} \usetikzlibrary{[Link]}.
67
It’s possible to change the TikZ style of those braces by changing the TikZ style nicematrix/brace.
In the following example, we assume that the TikZ extension calligraphy has been loaded:
\usetikzlibrary{calligraphy}
\NiceMatrixOptions{xdots/horizontal-labels}
\tikzset
..{
....nicematrix/brace/.style =
......{
........decoration =
..........{
............calligraphic brace ,
............amplitude = 0.4 em ,
............raise = -0.25 em
..........} ,
........line width = 0.1 em ,
........decorate ,
......}
..}
$\begin{pNiceArray}{ccc|cc}[first-row,last-col,margin]
3 2
\Hbrace{3}{3} & \Hbrace{2}{2} \\
2 & 1 & 1 & 1 & 1 & \Vbrace{3}{3} \\
2 1 1 1 1
1 & 1 & 1 & 1 & 1 \\ 1 1 1 1 1 3
If one prefers the curly braces of the current mathematical font of LaTeX, one should use the com-
mands \SubMatrix, \OverBrace and \UnderBrace in the \CodeAfter.
$\begin{pNiceArray}{ccc|cc}[margin,last-col]
2 & 1 & 1 & 1 & 1 & \Block{3-1}{\quad 3} \\
1 & 1 & 1 & 1 & 1 \\ 3 2
1 & 1 & 1 & 1 & 1 \\ z }| { z }| {
\Hline
2 1 1 1 1
1 & 1 & 1 & 1 & 1 & \Block{2-1}{\quad 2} \\ 1 1 1 1 1 3
1 & 1 & 1 & 1 & 1 \\
1 1 1 1 1
\CodeAfter
1 1 1 1 1
2
..\OverBrace[shorten,yshift=1.5mm]{1-1}{1-3}{3} 1 1 1 1 1
..\OverBrace[shorten,yshift=1.5mm]{1-4}{1-5}{2}
..\SubMatrix{.}{1-1}{3-5}{\rbrace}[xshift=3.5mm]
..\SubMatrix{.}{4-1}{5-5}{\rbrace}[xshift=3.5mm]
\end{pNiceArray}$
Or course, the output may seem disappointing. That’s why, for this type of use, we recommend
the use of the commands \Hbrace and \Vbrace (provided by nicematrix), as shown in the previous
examples.
68
0 & 0 & \Block[borders={bottom,top,right,left,tikz=dashed}]{2-2}{}
........7 & 1 & 0 & 0 \\
0 & 0 & -1 & 2 & 0 & 0 \\
0 & 0 & 0 & 0 & \Block[borders={left,top,tikz=dashed}]{2-2}{}
.................3 & 4 \\
0 & 0 & 0 & 0 & 1 & 4
\end{pNiceMatrix}
1 2 0 0 0 0
4 5 0 0 0 0
0 0 7 1 0 0
0 0 −1 2 0 0
0 0 0 0 3 4
0 0 0 0 1 4
\quad $\begin{pNiceMatrix}
12 -8 7 5 3 {} ;
.3 -18 12 1 4 ;
-3 -46 29 -2 -15 ;
.9 10 -5 4 7
\end{pNiceMatrix}$
\smallskip
\quad $\begin{pNiceMatrix}
12 -8 7 5 3 ;
0 64 -41 1 19 { L_2 \gets L_1-4L_2 } ;
0 -192 123 -3 -57 { L_3 \gets L_1+4L_3 } ;
0 -64 41 -1 -19 { L_4 \gets 3L_1-4L_4 } ;
\end{pNiceMatrix}$
\smallskip
\quad $\begin{pNiceMatrix}
12 -8 7 5 3 ;
0 64 -41 1 19 ;
0 0 0 0 0 { L_3 \gets 3 L_2 + L_3 }
\end{pNiceMatrix}$
\smallskip
\quad $\begin{pNiceMatrix}
12 -8 7 5 3 {} ;
0 64 -41 1 19 ;
\end{pNiceMatrix}$
\end{NiceMatrixBlock}
69
12 −8 7 5 3
3 −18 12 1 4
−3 −46 29 −2 −15
9 10 −5 4 7
12 −8 7 5 3
0 64 −41 1 19L2 ←L1 −4L2
0 −192 123 −3 −57L3 ←L1 +4L3
0 −64 41 −1 −19 L4 ←3L1 −4L4
12 −8 7 5 3
0 64 −41 1 19
0 0 0 0 0 L3 ←3L2 +L3
!
12 −8 7 5 3
0 64 −41 1 19
However, one can see that the last matrix is not perfectly aligned with others. That’s why, in LaTeX,
the parenthesis have not exactly the same width (smaller parenthesis are a bit slimer).
In order the solve that problem, it’s possible to require the delimiters to be composed with the
maximal width, thanks to the boolean key delimiters/max-width .
\begin{NiceMatrixBlock}[auto-columns-width]
\NiceMatrixOptions
..{
....delimiters/max-width,
....light-syntax,
....last-col, code-for-last-col = \color{blue}\scriptstyle,
....vlines = 5 ,
....matrix/columns-type = r ,
....no-cell-nodes % only for speedup
..}
\setlength{\extrarowheight}{1mm}
\quad $\begin{pNiceMatrix}
12 -8 7 5 3 {} ;
.3 -18 12 1 4 ;
-3 -46 29 -2 -15 ;
.9 10 -5 4 7
\end{pNiceMatrix}$
...
\end{NiceMatrixBlock}
12 −8 7 5 3
3 −18 12 1 4
−3 −46 29 −2 −15
9 10 −5 4 7
12 −8 7 5 3
0 64 −41 1 19L2 ←L1 −4L2
0 −192 123 −3 −57L3 ←L1 +4L3
0 −64 41 −1 −19 L4 ←3L1 −4L4
12 −8 7 5 3
0 64 −41 1 19
0 0 0 0 0 L3 ←3L2 +L3
!
12 −8 7 5 3
0 64 −41 1 19
70
If you wish an alignment of the different matrices without the same width for all the columns, you can
construct a unique array and place the parenthesis with commands \SubMatrix in the \CodeAfter.
Of course, that array can’t be broken by a page break.
\setlength{\extrarowheight}{1mm}
\[\begin{NiceMatrix}[r, last-col=6, code-for-last-col = \scriptstyle \color{blue}]
12 & -8 & 7 & 5 & 3 \\
.3 & -18 & 12 & 1 & 4 \\
-3 & -46 & 29 &-2 &-15 \\
.9 & 10 &-5 &4 & 7 \\[1mm]
12 & -8 & 7 &5 & 3 \\
0 & 64 &-41 & 1 & 19 & L_2 \gets L_1-4L_2 \\
0 & -192 &123 &-3 &-57 & L_3 \gets L_1+4L_3 \\
0 & -64 & 41 &-1 &-19 & L_4 \gets 3L_1-4L_4 \\[1mm]
12 & -8 &7 &5 & 3 \\
0 & 64 &-41 &1 &19 \\
0 & 0 &0 &0 & 0 & L_3 \gets 3L_2+L_3 \\[1mm]
12 & -8 &7 &5 & 3 \\
0 & 64 &-41 & 1 & 19 \\
\CodeAfter [sub-matrix/vlines=4]
...\SubMatrix({1-1}{4-5})
...\SubMatrix({5-1}{8-5})
...\SubMatrix({9-1}{11-5})
...\SubMatrix({12-1}{13-5})
\end{NiceMatrix}\]
12 −8 7 5 3
3 −18 12 1 4
−3 −46 29 −2 −15
9 10 −5 4 7
12 −8 7 5 3
0 64 −41 1 19 L2 ←L1 −4L2
0 −192 123 −3 −57 L3 ←L1 +4L3
0 −64 41 −1 −19 L4 ←3L1 −4L4
12 −8 7 5 3
64 −41
0 1 19
0 0 0 0 0 L3 ←3L2 +L3
!
12 −8 7 5 3
0 64 −41 1 19
In this tabular, the instructions \SubMatrix are executed after the composition of the tabular and,
thus, the vertical rules are drawn without adding space between the columns.
In fact, it’s possible, with the key vlines-in-sub-matrix , to choice a letter in the preamble of the
array to specify vertical rules which will be drawn in the \SubMatrix only (by adding space between
the columns).
\setlength{\extrarowheight}{1mm}
\[\begin{NiceArray}
...[
.....vlines-in-sub-matrix=I,
.....last-col,
.....code-for-last-col = \scriptstyle \color{blue}
...]
..{rrrrIr}
71
12 & -8 & 7 & 5 & 3 \\
.3 & -18 & 12 & 1 & 4 \\
-3 & -46 & 29 &-2 &-15 \\
.9 & 10 &-5 &4 & 7 \\[1mm]
12 & -8 & 7 &5 & 3 \\
0 & 64 &-41 & 1 & 19 & L_2 \gets L_1-4L_2 \\
0 & -192 &123 &-3 &-57 & L_3 \gets L_1+4L_3 \\
0 & -64 & 41 &-1 &-19 & L_4 \gets 3L_1-4L_4 \\[1mm]
12 & -8 &7 &5 & 3 \\
0 & 64 &-41 &1 &19 \\
0 & 0 &0 &0 & 0 & L_3 \gets 3L_2+L_3 \\[1mm]
12 & -8 &7 &5 & 3 \\
0 & 64 &-41 & 1 & 19 \\
\CodeAfter
...\SubMatrix({1-1}{4-5})
...\SubMatrix({5-1}{8-5})
...\SubMatrix({9-1}{11-5})
...\SubMatrix({12-1}{13-5})
\end{NiceArray}\]
12 −8 7 5 3
3 −18 12 1 4
−3 −46 29 −2 −15
9 10 −5 4 7
12 −8 7 5 3
0 64 −41 1 19 L2 ←L1 −4L2
0 −192 123 −3 −57 L3 ←L1 +4L3
0 −64 41 −1 −19 L4 ←3L1 −4L4
12 −8 7 5 3
64 −41
0 1 19
0 0 0 0 0 L3 ←3L2 +L3
!
12 −8 7 5 3
0 64 −41 1 19
In order to highlight a cell of a matrix, it’s possible to “draw” that cell with the key draw of the
command \Block (this is one of the uses of a mono-cell block88 ).
$\begin{pNiceArray}{>{\strut}cccc}[margin,rules/color=blue,no-cell-nodes]
\Block[draw]{}{a_{11}} & a_{12} & a_{13} & a_{14} \\
a_{21} & \Block[draw]{}{a_{22}} & a_{23} & a_{24} \\
a_{31} & a_{32} & \Block[draw]{}{a_{33}} & a_{34} \\
a_{41} & a_{42} & a_{43} & \Block[draw]{}{a_{44}} \\
\end{pNiceArray}$
a11 a12 a13 a14
a21 a22 a23 a24
a31 a32 a33 a34
a41 a42 a43 a44
88 We recall that, if the first mandatory argument of the command \Block is left empty, that means that the block is
a mono-cell block
72
We should remark that the rules we have drawn are drawn after the construction of the array and thus,
they don’t spread the cells of the array. We recall that, on the other side, the commands \hline and
\Hline, the specifier “|” and the options hlines, vlines, hvlines and hvlines-except-borders
spread the cells.89
It’s possible to color a row with \rowcolor in the code-before (or with \rowcolor in the first cell
of the row.
\begin{pNiceArray}{>{\strut}cccc}% <-- % mandatory
...[margin, extra-margin=2pt,no-cell-nodes]
..\rowcolor{red!15}A_{11} & A_{12} & A_{13} & A_{14} \\
..A_{21} & \rowcolor{red!15}A_{22} & A_{23} & A_{24} \\
..A_{31} & A_{32} & \rowcolor{red!15}A_{33} & A_{34} \\
..A_{41} & A_{42} & A_{43} & \rowcolor{red!15}A_{44}
\end{pNiceArray}
A11 A12 A13 A14
A21 A22 A23 A24
A31 A32 A33 A34
A41 A42 A43 A44
However, it’s not possible to do a fine tuning. That’s why we describe now a method to highlight a
row of the matrix.
That example and the following ones require TikZ (by default, nicematrix only loads pgf, which is
a sub-layer of TikZ) and the TikZ library fit. The following instructions in the preamble of your
document do the job:
\usepackage{tikz}
\usetikzlibrary{fit}
We create a rectangular TikZ node which encompasses the nodes of the second row by using the tools
of the TikZ library fit. Those nodes are not available by default in the \CodeBefore (for efficiency).
We have to require their creation with the key create-cell-nodes of the keyword \CodeBefore.
\tikzset{highlight/.style={rectangle,
...........................fill=red!15,
...........................rounded corners = 0.5 mm,
...........................inner sep=1pt,
...........................fit=#1}}
$\begin{bNiceMatrix}
\CodeBefore [create-cell-nodes]
..\tikz \node [highlight = (2-1) (2-last)] {} ;
\Body
0 & \Cdots & 0 \\
1 & \Cdots & 1 \\
0 & \Cdots & 0 \\
\end{bNiceMatrix}$
0 0
1 1
0 0
We consider now the following matrix. If we want to highlight each row of this matrix, we can use
the previous technique three times.
73
\begin{pNiceArray}{ccc}[last-col, margin = 2pt]
\CodeBefore [create-cell-nodes]
..\begin{tikzpicture}
..\node [highlight = (1-1) (1-last)] {} ;
..\node [highlight = (2-1) (2-last)] {} ;
..\node [highlight = (3-1) (3-last)] {} ;
..\end{tikzpicture}
\Body
a & a + b & a + b + c & L_1 \\
a & a & a + b & L_2 \\
a & a & a & L_3
\end{pNiceArray}
a a + b a + b + c L1
a a a + b L2
a a a L3
The result may seem disappointing. We can improve it by using the “medium nodes” instead of the
“normal nodes”.
\[\begin{pNiceArray}{ccc}[last-col, margin = 2pt, create-medium-nodes]
\CodeBefore [create-cell-nodes]
..\begin{tikzpicture} [name suffix = -medium]
..\node [highlight = (1-1) (1-last)] {} ;
..\node [highlight = (2-1) (2-last)] {} ;
..\node [highlight = (3-1) (3-last)] {} ;
..\end{tikzpicture}
\Body
a & a + b & a + b + c & L_1 \\
a & a & a + b & L_2 \\
a & a & a & L_3
\end{pNiceArray}\]
a a + b a + b + c L1
a a a + b L2
a a a L3
Cj
b11 b1j b1n
bkj
bn1 bnj bnn
a11 a1n
ai1
Li aik ain cij
an1 ann
74
\tikzset{highlight/.style={rectangle,
fill=red!15,
rounded corners = 0.5 mm,
inner sep=1pt,
fit=~#1}}
\[\begin{NiceArray}{*{6}{c}@{\hspace{6mm}}*{5}{c}}[nullify-dots]
\CodeBefore [create-cell-nodes]
..\SubMatrix({2-7}{6-last})
..\SubMatrix({7-2}{last-6})
..\SubMatrix({7-7}{last-last})
..\begin{tikzpicture}
....\node [highlight = (9-2) (9-6)] { } ;
....\node [highlight = (2-9) (6-9)] { } ;
..\end{tikzpicture}
\Body
....& & & & & & & & \color{blue}\scriptstyle C_j \\
....& & & & & & b_{11} & \Cdots & b_{1j} & \Cdots & b_{1n} \\
....& & & & & & \Vdots & & \Vdots & & \Vdots \\
....& & & & & & & & b_{kj} \\
....& & & & & & & & \Vdots \\
....& & & & & & b_{n1} & \Cdots & b_{nj} & \Cdots & b_{nn} \\[3mm]
....& a_{11} & \Cdots & & & a_{1n} \\
....& \Vdots & & & & \Vdots & & & \Vdots \\
\color{blue}\scriptstyle L_i
....& a_{i1} & \Cdots & a_{ik} & \Cdots & a_{in} & \Cdots & & c_{ij} \\
....& \Vdots & & & & \Vdots \\
....& a_{n1} & \Cdots & & & a_{nn} \\
\CodeAfter
\tikz \draw [gray,shorten > = 1mm, shorten < = 1mm] ([Link]) to [bend left] ([Link]) ;
\end{NiceArray}\]
\setlength{\tabcolsep}{0pt}
75
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
76
19 History
The development of the package nicematrix is done in the following GitHub depot:
[Link]
The successive versions of the file [Link] provided by TeX Live are also available on the
svn server of TeX Live:
https:[Link]/svn/texlive/trunk/Master/texmf-dist/tex/latex/nicematrix/[Link]
77
Changes between version 6.23 and 6.24
New command \TikzEveryCell available in the \CodeAfter and the \CodeBefore to apply the same
TikZ instruction to all the cells and blocks of the array.
New key offset in the key tikz of a command \Block.
78
Changes between version 6.14 and 6.15
New key transparent for the command \Block (with that key, the rules are drawn within the block).
79
Changes between versions 6.4 and 6.5
Key custom-line in \NiceMatrixOptions.
Key respect-arraystretch.
80
Index
81
end (key for the rules), 18 left-margin, 55
end-of-row (to be used with light-syntax), 52 left-shorten (key of \OverBrace and
enumitem (package required to use \UnderBrace), 43
\tabularnote), 45, 64 left-xshift (key of \SubMatrix), 40
enumitem-keys (subkey of “notes”), 46, 64 letter (key of “custom-line”), 16
enumitem-keys-para (subkey of “notes”), 46 light-syntax, 52
exterior-arraycolsep, 61 light-syntax-expanded, 52
extra-height (key of \SubMatrix), 40 \line (command of \CodeAfter), 39
extra-left-margin, 56 line-style (key for dotted rules), 35
extra-right-margin, 56 line-style (key for the dotted rules), 66
line-width (key of \Block), 5
F Lines in the tabulars, see Rules
fill
key of \Block, 5 M
key of \RowStyle, 26 mathdots (package), 31
first-col, 29 max-width (subkey of “delimiters”), 70
first-row, 29, 50 multiplicity (key of “custom-line”), 16
footnote (package), 44
footnote (key), 44 N
footnotehyper (package), 44 name
footnotehyper (key), 44 key for an environment, 54
key of \Block, 6
H key of \SubMatrix, 58
\Hbrace, 36 nb-rows (key of \RowStyle), 26
\Hdotsfor, 33 {NiceArrayWithDelims}, 53
\hdottedline, 17 {NiceMatrixBlock}, 27, 69
highlight (TikZ style defined in \NiceMatrixOptions, 1
an example), 73 {NiceTabularX}, 28
\Hline, 12 no-cell-nodes, 53
hlines, see Rules nocolor, 25
key for an environment, 13 Nodes of PGF/TikZ, 53–58
key of \Block, 5 non empty (key of \TikzEveryCell), 43
key of \SubMatrix, 41 Notes in the tabulars, 45–48, 64
horizontal-label(s) (key for dotted rules), 35 \NotEmpty, 61
\Hspace, 32 notes (key to customize the notes of a
hvlines, see Rules tabular), 64
key for an environment, 14 nullify-dots, 32
key of \Block, 5
key of \SubMatrix, 41 O
hvlines-except-borders, 14, 63 \OnlyMainNiceMatrix, 53
opacity
I key of \Block, 5
\Iddots, 31, 59 key of \RowStyle, 26
Incompatibilities, 61 key of commands such as
inter (key for dotted rules), 35 \rowcolor, etc., 20
iRow (LaTeX counter), 51 \OverBrace (command of \CodeAfter
and \CodeBefore), 42
J
jCol (LaTeX counter), 51 P
para (subkey of “notes”), 46
L parallelize-diags, 59
label (key of {NiceTabular}), 44 \pAutoNiceMatrix, 52
label-in-list (subkey of “notes”), 46 pgf-node-code, 55, 75
label-in-tabular (subkey of “notes”), 46
last-col, 29, 50, 65 R
last-row, 29, 50 radius (key for dotted rules), 35
\Ldots, 31 \rectanglecolor (in \CodeBefore), 19
\left : used by nicematrix for renew-dots, 34
delimiters in the preambles, 37 renew-matrix, 34
82
\resetcolorseries (command of xcolor), 22 key of \Block, 5, 62
respect-arraystretch (key of \Block), 6 key of “borders” de \Block, 5, 68
respect-blocks (key of \rowcolors du key of “custom-line”, 17
\CodeBefore), 21 total-width (key of “custom-line”), 17
restart (key of \rowcolors of \CodeBefore), 21 transparent (key of \Block), 6, 63
\right : used by nicematrix for
delimiters in the preambles, 37 U
right-margin, 55 \UnderBrace (command of \CodeAfter
right-shorten (key of \OverBrace and and \CodeBefore), 42
\UnderBrace), 43
right-xshift (key of \SubMatrix), 40 V
\rotate, 24, 26, 50 V (the columns V of varwidth), 27, 55
rounded-corners v-center (key of \Block), 9
key of \Block, 5 varwidth (package), 27, 55
key of \RowStyle, 26 \VAutoNiceMatrix, 52
key of {NiceTabular}, 49 \vAutoNiceMatrix, 52
\rowcolor \Vbrace, 36
command in tabular, 24, 73 \Vdots, 31
command of \CodeBefore, 19, 63 \Vdotsfor, 33
rowcolor (key of \RowStyle), 26 vlines, see Rules
\rowcolors (command of \CodeBefore), 19 key for an environment, 13
\rowlistcolors (command of \CodeBefore), 19 key of \Block, 5
\RowStyle, 26 key of \SubMatrix, 41
Rules in the tabulars, 11–18 vlines-in-sub-matrix, 71
rules (key for an environment), 12, 63
W
S width
S (the columns S of siunitx), 24, 50 key of {NiceTabular}, 28
sep-color (key of “custom-line”), 16 subkey of “rules”, 12
short-caption, 44 Width of the columns, 26–29
shorten (key for dotted rules), 35
X
shorten-end (key for dotted rules), 35
X (the columns X), 28
shorten-start (key for dotted rules), 35
xdots (and its subkeys), 31
\ShowCellNames (command of \CodeAfter
xshift (key of \SubMatrix), 40
and \CodeBefore), 49
siunitx (package), 50 Y
slim (key of \SubMatrix), 41 yshift (key of \OverBrace and \UnderBrace), 43
small (key for an environment), 51
{smallmatrix} (environment of amsmath), 51
standard-cline, 12
start (key for the rules), 18
style (subkey of “notes”), 46, 64
sub-matrix (key of \CodeAfter, with subkeys), 39
\SubMatrix (command of \CodeAfter
and \CodeBefore), 40, 58, 71, 74
T
\tabularnote, 45, 64
{TabularNote}, 45
tabularnote (key of {NiceTabular}), 45
tabularx (package), 28
Tagging Project, 62
tcolorbox (package), 63
threeparttable (package), 49
tikz-braces (key at load-tome of nicematrix), 36
TikZ (utilisation with nicematrix), 53
\TikzEveryCell (command of \CodeAfter
and \CodeBefore), 43
tikz
83
Contents
1 The environments of this package 2
4 The blocks 4
4.1 General case . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
4.2 The mono-column blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
4.3 The mono-row blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
4.4 The mono-cell blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
4.5 Horizontal position of the content of the block . . . . . . . . . . . . . . . . . . . . . . 7
4.6 Vertical position of the content of the block . . . . . . . . . . . . . . . . . . . . . . . 9
4.7 \\ and & in the blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
5 The rules 11
5.1 Some differences with the classical environments . . . . . . . . . . . . . . . . . . . . 11
5.1.1 The vertical rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
5.1.2 The command \cline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
5.2 The thickness and the color of the rules . . . . . . . . . . . . . . . . . . . . . . . . . 12
5.3 The tools of nicematrix for the rules . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
5.3.1 The keys hlines and vlines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
5.3.2 The keys hvlines and hvlines-except-borders . . . . . . . . . . . . . . . . . . . . 14
5.3.3 The (empty) corners . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
5.3.4 The command \diagbox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
5.3.5 Commands for customized rules . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
12 The \CodeAfter 38
12.1 The command \line in the \CodeAfter . . . . . . . . . . . . . . . . . . . . . . . . . . 39
12.2 The command \SubMatrix in the \CodeAfter (and the \CodeBefore) . . . . . . . . . 40
12.3 The commands \OverBrace and \UnderBrace in the \CodeAfter . . . . . . . . . . . 42
12.4 The command \TikzEveryCell in the \CodeAfter . . . . . . . . . . . . . . . . . . . . 43
84
13 Captions and notes in the tabulars 44
13.1 Caption of a tabular . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
13.2 The footnotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
13.3 The notes of tabular . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
13.4 Customisation of the tabular notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
13.5 Use of {NiceTabular} with threeparttable . . . . . . . . . . . . . . . . . . . . . . . . 49
14 Other features 49
14.1 The key rounded-corners . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
14.2 Command \ShowCellNames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
14.3 Use of the column type S of siunitx . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
14.4 Default column type in {NiceMatrix} . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
14.5 The command \rotate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
14.6 The option small . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
14.7 The counters iRow and jCol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
14.8 The key light-syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
14.9 Color of the delimiters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
14.10 The environment {NiceArrayWithDelims} . . . . . . . . . . . . . . . . . . . . . . . . 53
14.11 The command \OnlyMainNiceMatrix . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
17 Technical remarks 59
17.1 Diagonal lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
17.2 The empty cells . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
17.3 The option exterior-arraycolsep . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
17.4 Incompatibilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
17.5 Compatibility with the Tagging Project of LaTeX . . . . . . . . . . . . . . . . . . . . 62
18 Examples 62
18.1 Utilisation of the key ’tikz’ of the command \Block . . . . . . . . . . . . . . . . . . . 62
18.2 Use with tcolorbox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
18.3 Notes in the tabulars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
18.4 Dotted lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
18.5 Dotted lines which are no longer dotted . . . . . . . . . . . . . . . . . . . . . . . . . 66
18.6 Dashed rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
18.7 Stacks of matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
18.8 How to highlight cells of a matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
18.9 Utilisation of \SubMatrix in the \CodeBefore . . . . . . . . . . . . . . . . . . . . . . 74
18.10 A triangular tabular . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
19 History 77
Index 81
85