You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TikZ多参数样式无法使用:参数#2非法错误求助

修复TikZ多参数样式的参数错误

问题原因

你定义的mystyle用了默认的.style语法,但这个语法只支持单个参数(#1),你同时写了#1和#2两个参数,TikZ无法识别第二个参数,所以抛出“2 is an illegal parameter number”的错误。

修复方法

要定义接收多个参数的TikZ样式,必须使用.style n args={参数数量}{样式定义}的语法。针对你的需求(2个参数),修改样式定义如下:

\tikzset {
  mystyle/.style n args={2}{
      text = white,
      shape = #1,
      ball color = #2
    }
}

调用样式时,需要把两个参数分别用花括号包裹(或用逗号分隔,花括号包裹更清晰):

\node[mystyle={ellipse}{green}] at (3, 4) (node1) {This is my node};

完整修复代码

\documentclass[tikz,border=10pt]{standalone}
\usepackage{amsmath,amssymb,amsfonts}
\usepackage{float,enumitem,pifont}
\usepackage{hyperref}
\usepackage{tikz,tikzpeople,pgfplots,graphicx,smartdiagram}
\usetikzlibrary{arrows,shapes,shapes.geometric,quotes,positioning,arrows.meta}

\begin{document}
\sffamily

\tikzset {
  mystyle/.style n args={2}{
      text = white,
      shape = #1,
      ball color = #2
    }
}

\begin{tikzpicture}
  \node[mystyle={ellipse}{green}] at (3, 4) (node1) {This is my node};
\end{tikzpicture}
\end{document}

(注:代码中清理了重复加载的tikz包,不影响功能但更整洁)

内容的提问来源于stack exchange,提问作者user17911

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 17:12:48