8

I have to draw multiple node like this :

\node[circle, minimum size=2cm] (P0) at (0,0) {$1$};
\node[circle, minimum size=2cm, fill=red] (P1) at (1,0) {$2$};
\node[circle, minimum size=2cm, fill=black] (P2) at (2,0) {$3$};

But each time i have circle and minimum size in common, i try to do something like that :

\newcommand{\NodeClassic}{circle, minimum size=2cm}
\newcommand{\NodeRed}{circle, minimum size=2cm, fill=red}
\newcommand{\NodeBlack}{circle, minimum size=2cm, fill=black}

And after :

\node[\NodeClassic] (P0) at (0,0) {$1$};
\node[\NodeRed] (P1) at (1,0) {$2$};
\node[\NodeBlack] (P2) at (2,0) {$3$};

Like this, i can add some parameters :

\node[\NodeClassic, yellow] (P0) at (0,0) {$1$};

My document :

\documentclass[14pt]{article}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[utf8]{inputenc}

\usepackage{tikz}

\newcommand{\NodeClassic}{circle, minimum size=2cm}
\newcommand{\NodeRed}{circle, minimum size=2cm, fill=red}
\newcommand{\NodeBlack}{circle, minimum size=2cm, fill=black}

\begin{document}

\begin{tikzpicture}
    \node[\NodeClassic] (P0) at (0,0) {$1$};
    \node[\NodeRed] (P1) at (1,0) {$2$};
    \node[\NodeBlack] (P2) at (2,0) {$3$};
\end{tikzpicture}

\end{document}

Error message on Overleaf :

 Package pgfkeys Error: I do not know the key '/tikz/circle, minimum size=2cm, fill=red' and I am going to ignore it. Perhaps you misspelled it.

See the pgfkeys package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              
                                                  
l.16     \node[\NodeRed]
                         (P1) at (1,0) {$2$};

I have already try \def\command{params} or \tikz{params} inside \newcommand Thank you,

1
  • There is no expansion in option, so that will not work. Use styles... – Rmano Mar 7 at 21:47
7

The variables you are looking for are called 'styles'. The styles can also have parameters. As an example,

  mynode/.style={circle,minimum size=2cm,fill=#1},
  mynode/.default={white}

defines a style mynode with one parameter, the fill color. Its default value is white.

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[
  mynode/.style={circle,minimum size=2cm,fill=#1},
  mynode/.default={white}
  ]
  \node[mynode] (P0) at (0,0) {$1$};
  \node[mynode=red] (P1) at (1,0) {$2$};
  \node[mynode=black] (P2) at (2,0) {$3$};
\end{tikzpicture}
\end{document}
5

You should use styles:

\documentclass{article}

\usepackage{tikz}

\tikzset{
  NodeClassic/.style={draw,circle, minimum size=2cm},
  NodeRed/.style={circle, minimum size=2cm, fill=red},
  NodeBlack/.style={circle, minimum size=2cm, fill=black!20},
}

\begin{document}

\begin{tikzpicture}
    \node[NodeClassic] (P0) at (0,0) {$1$};
    \node[NodeRed] (P1) at (2,0) {$2$};
    \node[NodeBlack] (P2) at (4,0) {$3$};
\end{tikzpicture}

\end{document}

I changed the options to draw the white circle and to have less black in the black node, just in order to see the node contents.

enter image description here

There is no 14pt option for article.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.