![tikz 中图片第一个参数的行为不同](https://linux22.com/image/386807/tikz%20%E4%B8%AD%E5%9B%BE%E7%89%87%E7%AC%AC%E4%B8%80%E4%B8%AA%E5%8F%82%E6%95%B0%E7%9A%84%E8%A1%8C%E4%B8%BA%E4%B8%8D%E5%90%8C.png)
我在读这问题并尝试修改如下:
\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。
这意味着第一个参数在图片中实际上处理方式不同。也许我的理解是错误的!
答案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}