用分隔符明确定义节点的矩阵与节点矩阵的不同行为

用分隔符明确定义节点的矩阵与节点矩阵的不同行为

当我试图回答的时候另一个问题我发现当节点有分隔符时matrix of nodes会出现错误:! TeX capacity exceeded, sorry [grouping levels=255].

matrix如果节点明确定义,则不会发生这种情况:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\tikzset{
    mynode/.style={
        left delimiter=(,
        right delimiter=),      
        text width=1cm
    }
}
\begin{document}
    Why this works:

    \begin{tikzpicture}
        \matrix{
            \node[mynode] {long text 1};&
            \node[mynode] {long text 2};\\
        };
    \end{tikzpicture}

    whereas this one gives \texttt{! TeX capacity exceeded, sorry [grouping levels=255].}:

    \begin{tikzpicture}
    \matrix[matrix of nodes, nodes={mynode}]{
         long text 1 &
         long text 2 \\
    };
    \end{tikzpicture}

    ?
\end{document}

请注意,我希望我的示例中的第二个矩阵给出与第一个矩阵相同的结果,并且每个单元格周围都有分隔符,而不是整个矩阵周围都有分隔符。

答案1

问题是,一般来说,你不能在样式中添加left delimiter或右移,因为分隔符在内部添加了一个节点,所以这会产生无限循环。你可以在这个简单的例子中检查这一点,它也会抛出right delimiterevery node! TeX capacity exceeded, sorry [grouping levels=255].

\documentclass[tikz,border=7pt]{standalone}
\usetikzlibrary{matrix}
\begin{document}
  \begin{tikzpicture}[every node/.style={left delimiter=(}]
    \node {Some text here};
  \end{tikzpicture}
\end{document}

为了解决这个问题,你可以在创建内部节点之前\tikz@delimiter重置样式来解决这个问题(或者提交一个错误请求) ,就像这样:every node

\def\tikz@delimiter#1#2#3#4#5#6#7#8{%
  \bgroup
    \pgfextra{\let\tikz@save@last@fig@name=\tikz@last@fig@name\tikzset{every node/.style={}}}% <-- added reset `every node` style
    node[outer sep=0pt,inner sep=0pt,draw=none,fill=none,anchor=#1,at=(\tikz@last@fig@name.#2),#3]
    {%
      {\nullfont\pgf@process{\pgfpointdiff{\pgfpointanchor{\tikz@last@fig@name}{#4}}{\pgfpointanchor{\tikz@last@fig@name}{#5}}}}%
      $\left#6\vcenter{\hrule height .5#8 depth .5#8 width0pt}\right#7$%
    }
    \pgfextra{\global\let\tikz@last@fig@name=\tikz@save@last@fig@name}%
  \egroup%
} 

所以我的第一个代码中不再有错误:

\def\tikz@delimiter#1#2#3#4#5#6#7#8{%
  \bgroup
    \pgfextra{\let\tikz@save@last@fig@name=\tikz@last@fig@name\tikzset{every node/.style={}}}% <-- added reset `every node` style
    node[outer sep=0pt,inner sep=0pt,draw=none,fill=none,anchor=#1,at=(\tikz@last@fig@name.#2),#3]
    {%
      {\nullfont\pgf@process{\pgfpointdiff{\pgfpointanchor{\tikz@last@fig@name}{#4}}{\pgfpointanchor{\tikz@last@fig@name}{#5}}}}%
      $\left#6\vcenter{\hrule height .5#8 depth .5#8 width0pt}\right#7$%
    }
    \pgfextra{\global\let\tikz@last@fig@name=\tikz@save@last@fig@name}%
  \egroup%
}
\makeatother
\begin{document}
  \begin{tikzpicture}[every node/.style={left delimiter=(}]
    \node {Some text here};
  \end{tikzpicture}
\end{document}

在此处输入图片描述

所以你的代码也可以工作。

%
\documentclass[tikz,border=7pt]{standalone}
\usetikzlibrary{matrix}
\makeatletter
\def\tikz@delimiter#1#2#3#4#5#6#7#8{%
  \bgroup
    \pgfextra{\let\tikz@save@last@fig@name=\tikz@last@fig@name\tikzset{every node/.style={}}}% <-- added reset `every node` style
    node[outer sep=0pt,inner sep=0pt,draw=none,fill=none,anchor=#1,at=(\tikz@last@fig@name.#2),#3]
    {%
      {\nullfont\pgf@process{\pgfpointdiff{\pgfpointanchor{\tikz@last@fig@name}{#4}}{\pgfpointanchor{\tikz@last@fig@name}{#5}}}}%
      $\left#6\vcenter{\hrule height .5#8 depth .5#8 width0pt}\right#7$%
    }
    \pgfextra{\global\let\tikz@last@fig@name=\tikz@save@last@fig@name}%
  \egroup%
}
\makeatother

\tikzset{
    mynode/.style={
        left delimiter=(,
        right delimiter=),
        text width=1cm
    }
}
\begin{document}
  \begin{tikzpicture}
    \matrix[matrix of nodes, nodes={mynode}]{
         long text 1 &
         long text 2 \\
    };
  \end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容