1

I've been trying to follow the insights from the following posts:

(The user tobiasBora seems to be interested in achieving very similar things to me.) However, I've ended up in a jumble of nothing quite working in all possible cases.

In short, I want to use a tikzcd environment that

  • works as you would expect it to (duh!)
  • also works in conjunction with the externalize package
  • also works if you want to embed it in an amsmath align environment
  • works with the beamer document class

My current attempt is in the MWE below:

\documentclass{article}
\usepackage{tikz}
\usepackage{amsmath,xparse,etoolbox}
\usetikzlibrary{cd,external}
%we don't actually need externalisation to demonstrate the problem, so turn it off for now
%\tikzexternalize

\makeatletter \NewDocumentEnvironment{quantikz}{O{}+b}{% %local redefinition of catcode \begingroup \catcode`&=\active \def@temp{\tikzcd@[#1]\scantokens{#2}}% \expandafter@temp\endtikzcd\endgroup }{}

%replace commands with environment. seems to help with externalize. \patchcmd\tikzcd@{\tikzpicture}{\begin{tikzpicture}}{}{} \patchcmd\endtikzcd{\endtikzpicture}{\end{tikzpicture}}{}{}

\makeatother

\tikzcdset{nodes in empty cells}

%this works %also works with external switched on %also works when documentclass is beamer \begin{document} \begin{quantikz} H & \ar[l] \end{quantikz}

%this does not work %neither does ampersand replacement % \begin{align} % \begin{quantikz} % & \ar[l] % \end{quantikz} % \end{align}

%original environment works with ampersand replacement %but does not externalize \begin{align} \begin{tikzcd}[ampersand replacement=&] H & \ar[l] \end{tikzcd} \end{align}

\end{document}

I had wondered if a simple solution would be to get ampersand replacement working again within the new environment. But even if I directly replace all instances of & with \pgfmatrixnextcell, the quantikz environment doesn't seem to work inside align.

  • The align environment grabs its content at this point the catcode of & is fixed and you can't unfix it easily. (Though, we could possibly write a new tikzcd environment that takes its content and replaced all & with \pgfmatrixnextcell.) Are you actually using the align* environment with alignment? With your example, just using equation* would be enough. – Qrrbrbirlbel Apr 14 '23 at 10:48
  • @Qrrbrbirlbel Yes, I'm actually using it for alignment, but was being minimal in my MWE! – DaftWullie Apr 14 '23 at 10:51

1 Answers1

1

Here's a defintion of quantikz that grabs the body and replaces every & in it with a \pgfmatrixnextcell.

Then, it places a tikzpicture environment in a way that the external accepts it (having \end{tikzpicture} not hidden in another macro) and disables the tikzpicture environment inside tikzcd. For this the options to \tikzcd (\begin{tikzcd}) will be moved to the tikzpicture environment which is where it's usually placed anyway.

This does not patch the tikzcd environment in any regard but only provides a new quantikz environment which should work like the original tikzcd environment but with & even inside an argument to a macro and with activated externalization.

Code

\documentclass{article}
\usepackage{tikz}
\usepackage{amsmath}
\usetikzlibrary{cd, external}
\tikzexternalize
\ExplSyntaxOn
\NewDocumentEnvironment{quantikz}{O{}+b}{
  \begin{tikzpicture}[commutative~diagrams/.cd, every~diagram, #1]
    \tl_set:Nn \l_tmpa_tl { #2 }
    \tl_replace_all:Nnn \l_tmpa_tl { & } { \pgfmatrixnextcell }
    \begingroup
      \def\tikzpicture[##1]{}
      \let\endtikzpicture\relax
      \tikzcd
        \l_tmpa_tl
      \endtikzcd
    \endgroup
  \end{tikzpicture}
}{}
\ExplSyntaxOff
\tikzcdset{
  nodes in empty cells,
  diagrams={baseline=-axis_height}}% https://tex.stackexchange.com/a/678935/16595
\begin{document}
\begin{quantikz}
H & \ar[l] 
\end{quantikz}

\begin{align} \left{\begin{quantikz} H & \ar[l] X \ B \ar[ur] \end{quantikz}\right} & = { x + 2 } \end{align}

\end{document}

Qrrbrbirlbel
  • 119,821
  • Awesome! At first test, it appears to work :) Now I just need to make sure I can integrate it with the rest of the project... – DaftWullie Apr 14 '23 at 11:28
  • @DaftWullie Please do. Note that since AMSmath typesets stuff twice (once for measurement, once for real) every TikZ picture gets externalized twice. There should be a Q&A about this but I can't find it right now, at worst, it only wastes disk space, though. – Qrrbrbirlbel Apr 14 '23 at 12:05