为什么“.style n args”在我的 TikZ 图片中不起作用?

为什么“.style n args”在我的 TikZ 图片中不起作用?

我怀疑我忽略了一些简单而明显的东西,但老实说,这并不是因为缺乏尝试:-)

我创建了一张画小哈斯图。我甚至成功地给它提供了可选参数来指定与图表中每个元素相关的文本。这是我得到的:

% Oh, by the way, a separate newbie problem...
% I need my labels (generally lower-case letters) to align on the same baseline.
% But I don't think I can just set the style of node,
% because I don't want to effect every node in my document,
% only those that are labels in my Hasse diagrams.
% My solution is to create this command.
\newcommand{\element}{node[text height=1.5ex, text depth=.25ex]}

\tikzset{
    pics/2+1/.style args={#1/#2/#3}{
    code={
        \draw (-.3,  .5) \element (a) {#1}
              (-.3, -.5) \element (b) {#2}
              ( .3, 0  ) \element (c) {#3};
        \draw (a) -- (b);
        }},
    pics/2+1/.default=$a$/$b$/$c$
}

我调用它

\pic at (6,0) {2+1};

或者

\pic at (6,0) {2+1={$\alpha$/$\beta$/$\gamma$}};

并且两次呼叫都非常有效。

我的新手问题是,为什么这种其他方法不起作用?


\tikzset{
    pics/2+1/.style n args={3} {
    code={
        \draw (-.3,  .5) \element (a) {#1}
              (-.3, -.5) \element (b) {#2}
              ( .3, 0  ) \element (c) {#3};
        \draw (a) -- (b);
        }},
    pics/2+1/.default={$a$}{$b$}{$c$}
}

当我调用第二个

\pic at (6,0) {2+1={$\alpha$}{$\beta$}{$\gamma$}};

我收到错误消息

! Use of \pgfkeys@sp@b doesn't match its definition.
<argument> e
            very \tikz@shape \space node/.try
l.370 ...(6,0) {2+1={$\alpha$}{$\beta$}{$\gamma$}}

我仔细阅读了 PGF 手册这个网站。我错过了什么简单的细节?

答案1

您的 pic 定义中存在多余的空间。更具体地说,

pics/2+1/.style n args={3} {

需要成为

pics/2+1/.style n args={3}{

下面是一个最小的工作示例,说明了这一点

\documentclass[tikz,border=3mm]{standalone}
\tikzset{
    pics/2+1/.style n args={3}{% <- you had an excess space before the laste brace
    code={
        \draw (-.3,  .5) node (a) {#1}
              (-.3, -.5) node (b) {#2}
              ( .3, 0  ) node (c) {#3};
        \draw (a) -- (b);
        }},
    pics/2+1/.default={$a$}{$b$}{$c$}
}

\begin{document}
\begin{tikzpicture}
\pic at (6,0) {2+1={$\alpha$}{$\beta$}{$\gamma$}};
\end{tikzpicture}
\end{document}

附录:如果您希望为节点提供pic一些选项,则可以使用样式。这是众多可能性中的一种。

\documentclass[tikz,border=3mm]{standalone}
\tikzset{
    pics/2+1/.style n args={3}{% <- you had an excess space before the laste brace
    code={
        \draw[nodes=element,pic actions] (-.3,  .5) node (a) {#1}
              (-.3, -.5) node (b) {#2}
              ( .3, 0  ) node (c) {#3}
        (a) -- (b);
        }},
    pics/2+1/.default={$a$}{$b$}{$c$},
    element/.style={text height=1.1em,text depth=0.25ex}
}

\begin{document}
\begin{tikzpicture}
\pic at (6,0) {2+1={$\alpha$}{$\beta$}{$\gamma$}};
\end{tikzpicture}
\end{document}

当然,如果你只需要此处的这些选项,你可以说

    \draw[nodes={text height=1.1em,text depth=0.25ex},pic actions] (-.3,  .5) node (a) {#1}
          (-.3, -.5) node (b) {#2}
          ( .3, 0  ) node (c) {#3}
    (a) -- (b);

相反。还有更多的变化。pic actions只是为了提醒我们自己,人们可以从外面控制图片的外观,在这里它不是真正需要/使用的。

相关内容