latextikzpgf

Multiple argument style doesn't work in Tikz


I have the following code in Tikz

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

\begin{document}
\sffamily

\tikzset {
  mystyle/.style = {
      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}

But when I run this I get an error message indicating 2 is an illegal parameter number in the declaration of my style. I don't see how to fix this. As far as I remember, arguments of styles are preceded by # character followed by a number in the style definition, no?


Solution

  • Ok I found the problem. Here is the correct syntax:

    \tikzset {
      mystyle/.style 2 args = {
          text = white,
          shape = #1,
          ball color = #2
        }
    }
    
    \begin{tikzpicture}
    
      \node[mystyle = {ellipse}{green}]
      at (3, 4) (node1) {This is my node};
    \end{tikzppicture}
    

    Apparently, there is a more general syntax:

    For a style for with n arguments (n >= 1 and n <= 9) you write :

    \tikzset { StyleName/.style n args = {n} 
        { <... Your n arguments go here ...> } }
    

    So if we want to rewrite the same example above with, let's say, 4 arguments, this is how we can proceed:

    \tikzset {
      mystyle/.style n args = {4}{
          font = #1,
          text = #2,
          shape = #3,
          ball color = #4
        }
    }
    
    \begin{tikzpicture}
      \node[mystyle = {\sffamily\bfseries}{blue}{ellipse}{green}]
      at (3, 4) (node1) {This is my node};
    \end{tikzpicture}
    

    I hope this might help those who might have encountered the same problem.