tabularray 和 quantikz 不兼容

tabularray 和 quantikz 不兼容

当加载 tabularray(仅在最近的 CTAN 中,我需要在系统中使用 ninecolors 手动复制它)和 quantikz 时,tabularray 就会崩溃,如此处所述https://github.com/lvjr/tabularray/issues/156

ERROR: Misplaced alignment tab character &.

平均能量损失

\documentclass{memoir}
\usepackage{quantikz}
\usepackage{tabularx}
\usepackage{xcolor}
\usepackage{tabularray}
\UseTblrLibrary{booktabs}

\begin{document}

\begin{center}
\begin{tblr}{
  rows = {35pt}, columns = {35pt},
  rowsep = 1pt, colsep = 1pt, stretch = 0,
  cells = {c,font=\Huge},
  cell{odd}{even} = {azure7},
  cell{even}{odd} = {azure7},
  hlines, vlines,
}
a & b & c & d & e & f & g & h \\
a & b & c & d & e & f & g & h \\
   &   &   &   &   &   &   &   \\
   &   &   &   &   &   &   &   \\
   &   &   &   &   &   &   &   \\
   &   &   &   &   &   &   &   \\
a & b & c & d & e & f & g & h \\
\end{tblr}
\end{center}

\end{document}

答案1

quantikz软件包做出了令人生疑的决定,使其在全球范围内活跃,因此可能会与其他期望具有其标准含义的&软件包发生冲突。&

它这样做是为了能够在内部将其tikzcd作为参数调用给其他命令(quantikz实际上是通过吸收其内容的环境\NewEnviron),以避免使用ampersand replacement

您可以通过吸收环境的内容并更改&为其活动副本来修复此行为。 的标准类别代码&已全局恢复。

\documentclass{article}
\usepackage{quantikz}
\usepackage{tabularray}
\UseTblrLibrary{booktabs}


% fix the category code problem
\catcode`&=4

% change the quantikz environment
\ExplSyntaxOn\makeatletter

\RenewDocumentEnvironment{quantikz}{O{} +b}
 {
  \tl_set:Nn \l__quantikz_body_tl { \tikzcd@ [#1] #2 }
  \regex_replace_all:nnN { \& } { \cA\& } \l__quantikz_body_tl
  \tl_use:N \l__quantikz_body_tl
  \endtikzcd
 }{}
\tl_new:N \l__quantikz_body_tl

\makeatother\ExplSyntaxOff

\begin{document}

\begin{quantikz}
\lstick{$\ket{0}$} & \gate{H} & \ctrl{1} & \gate{U} & \ctrl{1} & \swap{2} & \ctrl{1} & \qw \\
\lstick{$\ket{0}$} & \gate{H} & \targ{} & \octrl{-1} & \control{} & \qw & \octrl{1} & \qw \\
&&&&&\targX{} & \gate{U} & \qw
\end{quantikz}

\begin{tblr}{
  rows = {35pt}, columns = {35pt},
  rowsep = 1pt, colsep = 1pt, stretch = 0,
  cells = {c,font=\Huge},
  cell{odd}{even} = {azure7},
  cell{even}{odd} = {azure7},
  hlines, vlines,
}
a & b & c & d & e & f & g & h \\
a & b & c & d & e & f & g & h \\
   &   &   &   &   &   &   &   \\
   &   &   &   &   &   &   &   \\
   &   &   &   &   &   &   &   \\
   &   &   &   &   &   &   &   \\
a & b & c & d & e & f & g & h \\
\end{tblr}


\end{document}

在此处输入图片描述

答案2

问题解决了,感谢 David Carlisle!(如果您希望我接受,请随意写下您自己的答案)

\documentclass{memoir}
\usepackage{quantikz}
\usepackage{tabularx}
\usepackage{xcolor}
\usepackage{tabularray}
\UseTblrLibrary{booktabs}

\begin{document}

\begin{center}
    \catcode`\&=4
    \begin{tblr}{
        rows = {35pt}, columns = {35pt},
        rowsep = 1pt, colsep = 1pt, stretch = 0,
        cells = {c,font=\Huge},
        cell{odd}{even} = {azure7},
        cell{even}{odd} = {azure7},
        hlines, vlines,
      }
      a & b & c & d & e & f & g & h \\
      a & b & c & d & e & f & g & h \\
        &   &   &   &   &   &   &   \\
        &   &   &   &   &   &   &   \\
        &   &   &   &   &   &   &   \\
        &   &   &   &   &   &   &   \\
      a & b & c & d & e & f & g & h \\
    \end{tblr}
\end{center}

\end{document}

相关内容