# TikZ externalize and nicematrix package

How to use TikZ external library in combination with the nicematrix package?

I tried to use the external library in combination with the nicematrix package (version August 12, 2018) to increase the compilation speed. But it does not work as expected.

Hier a minimal example:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{external}
%\tikzexternalize % activate!
\usepackage{nicematrix}
\begin{document}

% Test 1
\begin{flalign}
\begin{bNiceMatrix}
0 & \Cdots & 0 \\\Vdots & & \Vdots \\0 & \Cdots & 0
\end{bNiceMatrix}
\end{flalign}

% Test 2
\begin{flalign}
\begin{tikzpicture}[baseline,inner sep = 0pt]
\node {$\begin{bNiceMatrix} 0 & \Cdots & 0 \\\Vdots & & \Vdots \\0 & \Cdots & 0 \end{bNiceMatrix}$};
\end{tikzpicture}
\end{flalign}

% Test 3
\begin{flalign}
\begin{tikzpicture}[baseline,inner sep = 0pt]
\node {$\begin{pNiceArrayRC}{CC}% C_1 & C_2 \\1 & 2 & L_1\\4 & 5 & L_2\\ \end{pNiceArrayRC}$};
\end{tikzpicture}
\end{flalign}

\end{document}


Here the results of the tests:

• Not external, test 1: No problem.
• Not external, test 2: No problem. Same result as for test 1.
• Not external, test 3: No problem.
• External, test 1: Error.
• Line 1: Extra }, or forgotten \$. ...nt }]\bgroup \box_use:N \l_tmpa_box \egroup
• External, test 2: No error, but output not as expacted.
• External, test 3: Error.
• Package tikz Error: Sorry, the system call 'pdflatex -shell-escape -halt-on-e

Embedding the nicematrix in a tikz picture does not really solve the problem. Which might be doe to nested tikz pictures (see pgfmanual 50.2).

Does anyone have an idea how to overcome this issue?

• As far as I can tell nicearray uses TikZ pictures with the overlay and remember picture options which is not supported in terms of externalization. – TeXnician Sep 14 '18 at 10:07
• @TeXnician : the name of the package is nicematrix and not nicearray. Indeed, all the lines are drawn with the options overlay and remember picture and thus, I think that externalization is not possible. – F. Pantigny Sep 14 '18 at 17:26
• @F.Pantigny Sorry, mixed it up with the environment names, but still the same applies. Externalization is impossible for overlays. – TeXnician Sep 14 '18 at 20:03
• As it uses tikz pictures, one option would be to temporarily suspend externalisation for the nicematrix and then turn it back on afterwards. – Andrew Stacey Sep 15 '18 at 13:41

Since the extension nicematrix uses Tikz with the options overlay and remember picture to draw the dotted lines, it's not possible to use the Tikz externalization with these elements. In fact, in versions until 2.1, we even have TeX errors when trying to use externalization with nicematrix.

In the version 2.1.1 of 2018/09/18 (available in CTAN), the processus of externalization is now deactivated in the environments of nicematrix. Of course, the only benefit is in a document with other Tikz pictures interesting to externalize.

From the source code:

\group_begin:
\tikzset{every~picture/.style = {overlay,
remember~picture,
name~prefix = nm-\int_use:N \g_@@_env_int-}}


As you can see the package draws each matrix with overlay and remember picture. So the short answer to your question how to externalize them is: you can't.

Pictures with overlay and remember picture usually require two compilations. However, the externalization mechanism is rather simple and rather than trying to produce something undefined the developers decided not to support this.

You still have the option to externalize other pictures, but if you use many nicematrix environments I would recommend to locally enable externalization for the other pictures.

• Thank you for the answer. Maybe it would be more elegant to deactivate externalization for all nicematrix environments by default. This would solve the compatibility issue with the external library in general. At the begin of the document, it could be checked for the external library by \ifcsname tikz@library@external@loaded\endcsname <...>. – T. Jaschke Sep 15 '18 at 13:42
• @T.Jaschke It would be much better if this could be done at package level (but F. Pantigny should evaluate that). Deactivating for some environments is not that trivial (you had to redefine them all). – TeXnician Sep 15 '18 at 14:52
• Yes, exactly that was my intention. However, I don't know how many people struggle with this incompatibility and thus the effort is worthwhile. – T. Jaschke Sep 15 '18 at 15:18
• @T.Jaschke Just ask the author (he has commented on your question post) if he wants to tackle this problem. Asking does not hurt :) – TeXnician Sep 15 '18 at 15:33
• I will write a new version of nicematrix which deactivates externalization in the environments of the extension. – F. Pantigny Sep 16 '18 at 8:00

I found a solution using the standalone package, that solves the issue and improves the speed. Following a short description of the solution.

Create a seperate standalone file for each matrix.

% matrix_N.tex
\documentclass{standalone}
\usepackage{nicematrix}
\begin{document}%
\ensuremath{<code of matrix>}%
\end{document}


And then include the standalone files in the main document:

% main.tex
\documentclass{article}
\usepackage[mode=buildnew]{standalone}
\usepackage{nicematrix}
\begin{document}

% Test 1
\begin{flalign}
[S]&=\tikz[baseline]{\node[anchor=center]{\includestandalone{Test1}};}
\end{flalign}

% Test 3
\begin{flalign}
[S]&=\tikz[baseline]{\node[anchor=center,yshift=1em]{\includestandalone{Test3}};}
\end{flalign}

\end{document}


Problems/Restrictions:

• Alignment in equation not perfect (manual adjustment)
• Labels and references are not supported
• Manually control the consistency of the main document and figures (font, etc.)
• Attention, if the standalone figure needs several compilations to display correctly. Compile with mode=build until the figure is correct and then change mode=buildnew or save the standalone file with a new modification date and compile until the figure is correct.

Of cause, this solution is not restricted to the nicematrix package and can be used in general. For example, if externalization with tikz is not possible.