1

I would like to implement a command \drawColoredGraph which accepts a comma separated list of n colours and draws a graph on the vertices 1,...,n, in which the vertex k is coloured with the colour provided as the k-th argument.

I tried to implement this functionality using the \forcsvlist command from the very useful etoolbox package. I came up with the following

%!TEX program = lualatex
\documentclass{article}
\usepackage{tikz,etoolbox}
\usetikzlibrary{graphs,graphdrawing}
\usegdlibrary{circular}

\newcounter{countNodes}

\newcommand\drawColoredGraph[1]{%
  \setcounter{countNodes}{0}
  \begin{tikzpicture}
  \graph[simple necklace layout] {
    \forcsvlist{\drawNode}{#1} 1;
  };
  \end{tikzpicture}
}
\newcommand{\drawNode}[1]{\stepcounter{countNodes}\thecountNodes[fill = #1] -- }%

\newcommand\writeColoredNodes[1]{%
  \setcounter{countNodes}{0}
  \forcsvlist{\writeNode}{#1}1
}
\newcommand{\writeNode}[1]{\stepcounter{countNodes}{\color{#1}\thecountNodes} -- }


\begin{document}

\begin{tikzpicture}
\graph[simple necklace layout] {
  1[fill = red] -- 2[fill = blue] -- 3[fill = yellow] -- 4[fill = blue] -- 1;
};
\end{tikzpicture}

\writeColoredNodes{red,blue,yellow,blue} % writes 1–2–3–4–1 in the correct colors 

% \drawColoredGraph{red,blue,yellow,blue} % DOES NOT COMPLILE

\end{document}

The tikzpicture environment contains the example graph I am trying to create automatically. The writeColoredNodes command shows that the forcsvlist command principally does exactly what I want it to do.

For some reason \drawColoredGraph produces an endless list of error messages (either Undefined control sequence. or Missing \endcsname inserted., both of which I don't understand). What am I doing wrong?

I could imagine that the problem lies in the fact that the compiler first converts -- into a dash, after which Tikz does not recognise -- anymore as an edge in the graph but I neither know whether this is the core of the problem, nor how to fix it.

Any suggestions? I would also be open to other approaches for the above problem.

1

Rather than using \forcsvlist I would use \foreach from tikz/pgf but I think that the real problem is building the path and then giving it to \graph, which involves expansion issues.

Here is a way around both of these problems:

\documentclass{article}
\usepackage{tikz}
\usepackage{etoolbox}% for \xappto
\usetikzlibrary{graphs,graphdrawing}
\usegdlibrary{circular}

\newcommand\drawColoredGraph[1]{%
  % build the graph specifications
  \def\graphspecs{}%
  \foreach \colour [count=\g] in {#1} {
      \xappto\graphspecs{\g [fill=\colour] -- }
  }%
  % expand specs but not \graph -- and close off graph specs by adding 1
  \xdef\graphspecs{\noexpand\graph[simple necklace layout]{\graphspecs 1}}%
  \begin{tikzpicture}% draw the graph
    \graphspecs;
  \end{tikzpicture}%
}

\begin{document}

   \drawColoredGraph{red,blue,yellow,blue}

\end{document}

Here is the output:

enter image description here

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.