tikz 中图片第一个参数的行为不同

tikz 中图片第一个参数的行为不同

我在读问题并尝试修改如下:

\documentclass[border=2pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{chains,positioning,fit,calc}  
\makeatletter
\pgfdeclareshape{cross}{
    \inheritsavedanchors[from=circle]
    \inheritanchorborder[from=circle]
    \inheritbackgroundpath[from=circle]
    \foreach \i in {center,north,south,east,west} {
        \inheritanchor[from=circle]{\i}
    }
    \backgroundpath{
        \pgfpathcircle{\pgfpointorigin}{\radius}%
    }
    \foregroundpath{
        \pgfpathmoveto{\pgfpointpolar{-135}{\radius}}%
        \pgfpathlineto{\pgfpointpolar{45}{\radius}}%
        \pgfpathmoveto{\pgfpointpolar{135}{\radius}}%
        \pgfpathlineto{\pgfpointpolar{-45}{\radius}}%
        \pgfusepath{draw}
    }
}
\makeatother
\tikzset{
    pics/PINH/.style n args={1}{
        code = {
            \message{PINH params: #1}
            \foreach \s [count=\i] in {#1} {
                \message{count: \i,\s}
                \node[draw,cross,minimum size=0.5cm,label={above:\s},xshift={\i*0.6cm}] (-\i) {};
            }
            \node[fit=(-1)(-\i),draw] () {};
}}}
\tikzset{
    pics/PINV/.style n args={2}{
        code = {
            \message{PINV params: #1,#2}
            \foreach \s [count=\i] in {#2} {
                \node[draw,cross,minimum size=0.5cm,label={left:\s},yshift={-\i*0.6cm}] (-\i) {};
            }
            \node[fit=(-1)(-\i),draw] () {};
}}}
\begin{document}
    \begin{tikzpicture}
    \pic (A) {PINH={A,B,C,D}};
    \message{test here:}
    \pic (B) [below=1em of A.south west] {PINV={}{E,F,G,H}};
    \end{tikzpicture}
\end{document}

您可以看到,PINH 和 PINV 实际上只使用 1 个参数,但 PINH 只解析第一个字母 A,而 PINV 解析所有字母 E、F、G、H。

这意味着第一个参数在图片中实际上处理方式不同。也许我的理解是错误的!

enter image description here

答案1

据我所知,它n args={1}不受官方支持,事实上,如果你忽略它,你会得到

\documentclass[border=2pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{chains,positioning,fit,calc}  
\makeatletter
\pgfdeclareshape{cross}{
    \inheritsavedanchors[from=circle]
    \inheritanchorborder[from=circle]
    \inheritbackgroundpath[from=circle]
    \foreach \i in {center,north,south,east,west} {
        \inheritanchor[from=circle]{\i}
    }
    \backgroundpath{
        \pgfpathcircle{\pgfpointorigin}{\radius}%
    }
    \foregroundpath{
        \pgfpathmoveto{\pgfpointpolar{-135}{\radius}}%
        \pgfpathlineto{\pgfpointpolar{45}{\radius}}%
        \pgfpathmoveto{\pgfpointpolar{135}{\radius}}%
        \pgfpathlineto{\pgfpointpolar{-45}{\radius}}%
        \pgfusepath{draw}
    }
}
\makeatother
\tikzset{
    pics/PINH/.style={
        code = {
            \message{PINH params: #1}
            \foreach \s [count=\i] in {#1} {
                \message{count: \i,\s}
                \node[draw,cross,minimum size=0.5cm,label={above:\s},xshift={\i*0.6cm}] (-\i) {};
            }
            \node[fit=(-1)(-\i),draw] () {};
}}}
\tikzset{
    pics/PINV/.style n args={2}{
        code = {
            \message{PINV params: #1,#2}
            \foreach \s [count=\i] in {#2} {
                \node[draw,cross,minimum size=0.5cm,label={left:\s},yshift={-\i*0.6cm}] (-\i) {};
            }
            \node[fit=(-1)(-\i),draw] () {};
}}}
\begin{document}
    \begin{tikzpicture}
    \pic (A) {PINH={A,B,C,D}};
    \message{test here:}
    \pic (B) [below=1em of A.south west] {PINV={}{E,F,G,H}};
    \end{tikzpicture}
\end{document}

enter image description here

相关内容