tikz.dev / nicematrix Manual

The nicematrix package

16 API for the developers

16.1 Public sets of keys

The extension nicematrix provides several public sets of keys:

  • nicematrix/environments which applies to all the environments of nicematrix;

  • nicematrix/NiceMatrix which applies specifically to the environment {NiceMatrix} and its variants with delimiters ({pNiceMatrix}, etc.);

  • nicematrix/NiceTabular which applies specifically to the environements {NiceTabular}, {NiceTabularX} and {NiceTabular*}.

For example, if you wish to be able to fix the parameter \arraystretch with a key arraystretch, you only have to add the following instructions in the preamble of the LaTeX document.

\DeclareKeys[nicematrix/environments]{arraystretch.store=\arraystretch}

Then, it’s possible to write, for example:

Compiled output

\begin{NiceTabular}{cc}[hvlines,arraystretch=2]
a & b \\
c & d
\end{NiceTabular}
16.2 \CodeBefore and \CodeAfter

The package nicematrix provides two variables which are internal but public1:

  • \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 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.

We use \g_nicematrix_code_before_tl instead of \g_nicematrix_code_after_tl because we want to draw the cross before the horizontal aud vertical rules (in order to have a perfect output).

\ExplSyntaxOn
\cs_new_protected:Nn \__pantigny_crossbox:nnn
{
\tikz \draw [ #3 ]
( #1 -| \inteval { #2 + 1 } ) -- ( \inteval { #1 + 1 } -| #2 )
( #1 -| #2 ) -- ( \inteval { #1 + 1 } -| \inteval { #2 + 1 } ) ;
}

\NewDocumentCommand \crossbox { ! O { } }
{
\tl_gput_right:Ne \g_nicematrix_code_before_tl
{
\__pantigny_crossbox:nnn
{ \arabic { iRow } }
{ \arabic { jCol } }
{ \exp_not:n { #1 } }
}
\ignorespaces
}
\ExplSyntaxOff

We have used the LaTeX counters iRow and jCol provided by nicematrix (cf. 14.8).

Here is an example of use:

Compiled output

\begin{NiceTabular}{ccc}[hvlines]
\CodeBefore
\arraycolor{gray!10}
\Body
merlan & requin & cabillaud \\
baleine & \crossbox[red] & morue \\
mante & raie & poule
\end{NiceTabular}

1 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.