
\drawColoredGraph
쉼표로 구분된 n
색상 목록을 허용하고 정점에 그래프를 그리는 명령을 구현하고 싶습니다 1,...,n
. 여기서 정점은 -th 인수 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}
출력은 다음과 같습니다.