![新编辑](https://linux22.com/image/389847/%E6%96%B0%E7%BC%96%E8%BE%91.png)
我借用了这里这个问题。代码用于绘制六角星(例如䷊),其中每一笔对应列表中的二进制数字(例如 {1,0,1,1,1,1})。针对列表的 foreach 循环用于绘制完整笔画(⚊ 如果为 1)或断笔画(⚋ 如果为 0)。我如何使用列表中的前 3 个数字作为前 3 个笔画的颜色 [例如 rgb(0,0,0)],并使用后 3 个笔画作为第二组三元组的颜色。
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{math}
\usepackage{ifthen}
\begin{document}
\begin{tikzpicture}
\tikzset{
hexagram/.pic={
\edef\set{#1}
\foreach \d [count=\n] in \set{
% \foreach \d [count=\n] in {#1}{
\ifthenelse{\d=0}
{\draw [yshift=\n*2cm, line width=1cm] (-6,0) -- (-1,0) (1,0) -- (6,0);}
{\draw [yshift=\n*2cm, line width=1cm] (-6,0) -- (6,0);}
}}
}
\foreach [count=\n] \m in {
{0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 1},
{0, 0, 0, 1, 1, 1}}{
\pic at (\n*15,0) {hexagram = \m};
}
\end{tikzpicture}
\end{document}
答案1
这是一项提议。我首先将其踢出,ifthen
因为它不需要。然后我通过添加以下内容使代码看起来更加复杂但这些都是细节。\smuggle
。你可以用更短的代码来牺牲定义颜色的中间列表,但我不想这么做。
这个问题(更新后)通过一个宏来解决\parsesplit
,它将列表拆分为两个子列表。它的工作原理如下
\parsesplit(<long list with (at least) 6 entries>)->(<first>,<second>)
其中first
和second
是用于存储子列表的宏的名称。在下面的 MWE 中,它们是\mycolone
和\mycoltwo
,然后可以通过以下方式使用它们来定义颜色
\definecolor{mycol1}{rgb}{\mycolone}
\definecolor{mycol2}{rgb}{\mycoltwo}
这是 MWE。在这个更新的 MWE 中,我删除了走私,并添加了一些允许您使用简单缩放图片的内容scale=...
。
\documentclass{standalone}
\usepackage{tikz}
\def\parsesplit(#1,#2,#3,#4,#5,#6)->(#7,#8){%
\edef#7{#1,#2,#3}%
\edef#8{#4,#5,#6}%
}
\begin{document}
\begin{tikzpicture}
\tikzset{
trigram/.pic={
\edef\temp{\noexpand\parsesplit(#1)->(\noexpand\mycolone,\noexpand\mycoltwo)}
\temp
\pgfgettransformentries{\tmp}{\tmp}{\tmp}{\yscale}{\tmp}{\tmp}
\definecolor{mycol1}{rgb}{\mycolone}
\definecolor{mycol2}{rgb}{\mycoltwo}
\foreach \d [count=\n] in #1{
\ifnum\d=0
\ifnum\n<4
\draw [color=mycol1,yshift=\n*2cm, line width=\yscale*1cm] (-6,0) -- (-1,0) (1,0) -- (6,0);
\else
\draw [color=mycol2,yshift=\n*2cm, line width=\yscale*1cm] (-6,0) -- (-1,0) (1,0) -- (6,0);
\fi
\else
\ifnum\n<4
\draw [color=mycol1,yshift=\n*2cm, line width=\yscale*1cm] (-6,0) -- (6,0);
\else
\draw [color=mycol2,yshift=\n*2cm, line width=\yscale*1cm] (-6,0) -- (6,0);
\fi
\fi
}
},}
\foreach [count=\n] \m in {
{0, 1, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 1},
{1, 0, 0, 1, 1, 1}}{
\pic[scale=0.5] at (\n*15,0) {trigram = \m};
}
\end{tikzpicture}
\end{document}
老东西(可能对某些人有用):我不确定这是否是正确的解释,所以这里是另一个。
\documentclass{standalone}
\usepackage{tikz}
%% smuggling from https://tex.stackexchange.com/a/470979/121799
\newcounter{smuggle}
\DeclareRobustCommand\smuggleone[1]{%
\stepcounter{smuggle}%
\expandafter\global\expandafter\let\csname smuggle@\arabic{smuggle}\endcsname#1%
\aftergroup\let\aftergroup#1\expandafter\aftergroup\csname smuggle@\arabic{smuggle}\endcsname
}
\DeclareRobustCommand\smuggle[2][1]{%
\smuggleone{#2}%
\ifnum#1>1
\aftergroup\smuggle\aftergroup[\expandafter\aftergroup\the\numexpr#1-1\aftergroup]\aftergroup#2%
\fi
}
\begin{document}
\begin{tikzpicture}
\tikzset{
hexagram/.pic={
\foreach \d [count=\n] in #1{
\ifnum\d=0
\draw [yshift=\n*2cm, line width=1cm] (-6,0) -- (-1,0) (1,0) -- (6,0);
\else
\draw [yshift=\n*2cm, line width=1cm] (-6,0) -- (6,0);
\fi
}},
trigram/.pic={
\foreach \d [count=\n] in #1{
\ifnum\n<4
\ifnum\n=1
\edef\collst{\d}
\else
\edef\collst{\collst,\d}
\fi
\else
\pgfmathsetmacro{\mycol}{{\collst}[\n-4]}
\ifnum\d=0
\draw [color=\mycol,yshift=\n*2cm, line width=1cm] (-6,0) -- (-1,0) (1,0) -- (6,0);
\else
\draw [color=\mycol,yshift=\n*2cm, line width=1cm] (-6,0) -- (6,0);
\fi
\fi
\smuggle{\collst}
}},
}
\foreach [count=\n] \m in {
{0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 1},
{0, 0, 0, 1, 1, 1}}{
\pic at (\n*15,0) {hexagram = \m};
}
\begin{scope}[yshift=-15cm]
\foreach [count=\n] \m in {
{"blue","red","blue", 0, 0, 0},
{"black","red","red", 0, 0, 1},
{"blue","black","orange", 1, 1, 1}}{
\pic at (\n*15,0) {trigram = \m};
}
\end{scope}
\end{tikzpicture}
\end{document}
答案2
我对您的代码做了一些修改,我假设您可以在颜色和数字之间添加另一个分隔符,即/
。
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{math}
\usepackage{ifthen}
\begin{document}
\begin{tikzpicture}
\tikzset{
hexagram/.pic={
\def\get##1/##2\null{\edef\set{##2}\definecolor{barcolor}{rgb}{##1}}
\expandafter\get#1\null
\foreach \d [count=\n] in \set{
\ifthenelse{\d=0}
{\draw [yshift=\n*2cm, line width=1cm, barcolor] (-6,0) -- (-1,0) (1,0) -- (6,0);}
{\draw [yshift=\n*2cm, line width=1cm, barcolor] (-6,0) -- (6,0);}
}}
}
\foreach [count=\n] \m in {
{1, 0, 0/0, 0, 0},
{0, 1, 0/0, 0, 1},
{0, 0, 1/1, 1, 1}}{
\pic at (\n*15,0) {hexagram = \m};
}
\end{tikzpicture}
\end{document}
新编辑
我希望以下评论的新版本能够解决您的问题。
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{math}
\usepackage{ifthen}
\begin{document}
\begin{tikzpicture}
\tikzset{
hexagram/.pic={
\def\get##1/##2\null{\edef\set{##1,##2}%
\definecolor{topcolor}{rgb}{##1}%
\definecolor{bottomcolor}{rgb}{##2}%
}
\expandafter\get#1\null
\foreach \d [count=\n] in \set{
\ifthenelse{\d=0}
{\draw [yshift=\n*2cm, line width=1cm, \ifnum\n>3 topcolor\else bottomcolor\fi] (-6,0) -- (-1,0) (1,0) -- (6,0);}
{\draw [yshift=\n*2cm, line width=1cm, \ifnum\n>3 topcolor\else bottomcolor\fi] (-6,0) -- (6,0);}
}}
}
\foreach [count=\n] \m in {
{1, 0, 0/0, 0, 0},
{0, 1, 0/0, 0, 1},
{0, 0, 1/1, 1, 1}}{
\pic at (\n*15,0) {hexagram = \m};
}
\end{tikzpicture}
\end{document}
PS 由于纸张颜色为白色,因此白色将不可见!最好用其他颜色填充页面,例如灰色。