
\drawColoredGraph
カンマで区切られた色のリストを受け取りn
、頂点にグラフを描画するコマンドを実装したいと思います。1,...,n
そのグラフでは、頂点は- 番目の引数k
として指定された色で色付けされますk
。
\forcsvlist
私は非常に便利なetoolboxパッケージのコマンドを使用してこの機能を実装しようとしました。私は次のように考えました。
%!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}
環境tikzpicture
には、自動的に作成しようとしているサンプル グラフが含まれています。コマンドは、基本的に、コマンドがまさに私が望んでいることを実行することwriteColoredNodes
を示しています。forcsvlist
何らかの理由で、\drawColoredGraph
エラー メッセージの無限のリストが生成されます ( またはUndefined control sequence.
、Missing \endcsname inserted.
どちらも理解できません)。何が間違っているのでしょうか?
--
問題は、コンパイラが最初にダッシュに変換し、その後 Tikz がグラフ内のエッジとして認識しなくなることにあると想像できます--
が、これが問題の核心であるかどうか、またそれを修正する方法もわかりません。
何か提案はありますか? 上記の問題に対する他のアプローチも検討します。
答え1
を使用するのではなく、 tikz/pgf から\forcsvlist
を使用します\foreach
が、実際の問題はパスを構築してから に渡すことであり\graph
、これには拡張の問題が伴うと思います。
これら両方の問題を回避する方法は次のとおりです。
\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}
出力は次のとおりです。