
以下の例のように、いくつかの色を使ってたくさんのシンプルなパターンを描く必要があることに気が付きました。
\begin{tikzpicture}[scale=1.5]
\draw[style=help lines,step=1cm] (-2.3,-2.3) grid (2.3,2.3);
\draw[fill=green] (-2,1) rectangle (-1,2);
\draw[fill=red] (-1,1) rectangle (0,2);
\draw[fill=blue] (0,1) rectangle (1,2);
\draw[fill=yellow] (1,1) rectangle (2,2);
\draw[fill=red] (-2,0) rectangle (-1,1);
\draw[fill=green] (-1,0) rectangle (0,1);
\draw[fill=yellow] (0,0) rectangle (1,1);
\draw[fill=blue] (1,0) rectangle (2,1);
\draw[fill=yellow] (-2,-1) rectangle (-1,0);
\draw[fill=blue] (-1,-1) rectangle (0,0);
\draw[fill=red] (0,-1) rectangle (1,0);
\draw[fill=green] (1,-1) rectangle (2,0);
\draw[fill=blue] (-2,-2) rectangle (-1,-1);
\draw[fill=red] (-1,-2) rectangle (0,-1);
\draw[fill=green] (0,-2) rectangle (1,-1);
\draw[fill=yellow] (1,-2) rectangle (2,-1);
\end{tikzpicture}
しかし、これは明らかに画像を作成する非効率的な方法であり、理想的には、rowpatternmaker coords string
(
rowpatternmaker (-2,0) rgyb
-2,0)から始まる色のついた正方形の列を左から右に赤、緑、黄、青で作成する関数が必要です。この方法で、上記のパターンを作成できます。
rowpatternmaker (-2,1) grby
rowpatternmaker (-2,0) rgyb
rowpatternmaker (-2,-1) ybrg
rowpatternmaker (-2,-2) brgy
もちろん、私が本当にやりたいのはsquare pattern maker
、
squarepatternmaker (-2,1) grby rgyb ybrg brgy
上記のパターンが作成されます。
TikZ でいくつかの図を作成しましたが、このような関数の作成方法、ましてや文字列「grby」を読み取ってこれを色のリストとして解釈できる関数の作成方法がわかりません。
可能だと仮定すると、rowpatternmaker
(またはsquarepatternmaker
) を作成するにはどうすればよいでしょうか?
答え1
LaTeXでは、コマンドとして単語を使うのは不必要に難しい。そこで、
\rowpatternmaker (-2,1) grby;
\rowpatternmaker (-2,0) rgyb;
\rowpatternmaker (-2,-1) ybrg;
\rowpatternmaker (-2,-2) brgy;
大丈夫だと思います。最初の引数は開始座標で、2 番目はカラー シーケンスです。カラー シーケンスを空のままにすると、デフォルトで になりますrgby
。線の長さはカラー シーケンスの長さによって決まります。つまり、rgbrgb
長さは 6 になります。
スクエアパターンメーカーも可能
\squarepatternmaker (-2,1) grby rgyb ybrg brgy;
行パターンメーカーの観点から実装されます。
\documentclass{article}
\pagestyle{empty}
\usepackage{tikz,xparse}
\ExplSyntaxOn
\seq_new:N \l_cookie_rows_seq
\int_new:N \l_cookie_rowcount_int
\seq_new:N \l_cookie_colors_seq
\int_new:N \l_cookie_count_int
\cs_new_protected:Npn \cookie_map_colors:Nn #1#2
{
\seq_clear:N #1
\seq_set_split:Nnn \l_tmpa_seq { } { #2 }
\seq_map_inline:Nn \l_tmpa_seq
{
\str_case:nn { ##1 }
{
{ r } { \seq_put_right:Nn #1 { red } }
{ g } { \seq_put_right:Nn #1 { green } }
{ b } { \seq_put_right:Nn #1 { blue } }
{ y } { \seq_put_right:Nn #1 { yellow } }
}
}
}
\cs_new_protected:Npn \cookie_row_pattern_maker:nnn #1#2#3
{
\cookie_map_colors:Nn \l_cookie_colors_seq { #3 }
\int_zero:N \l_cookie_count_int
\seq_map_inline:Nn \l_cookie_colors_seq
{
\draw[fill=##1] (#1+\int_use:N \l_cookie_count_int,#2) rectangle +(1,1);
\int_incr:N \l_cookie_count_int
}
}
\cs_new_protected:Npn \rowpatternmaker (#1,#2) #3;
{
\tl_if_empty:nTF { #3 }
{ \cookie_row_pattern_maker:nnn { #1 } { #2 } { rgby } }
{ \cookie_row_pattern_maker:nnn { #1 } { #2 } { #3 } }
}
\cs_generate_variant:Nn \seq_set_split:Nnn { Nnx }
\cs_new_protected:Npn \cookie_square_pattern_maker:nnn #1#2#3
{
\seq_set_split:Nnx \l_cookie_rows_seq { ~ } { \tl_trim_spaces:n { #3 } }
\int_zero:N \l_cookie_rowcount_int
\seq_map_inline:Nn \l_cookie_rows_seq
{
\cookie_row_pattern_maker:nnn { #1 } { #2 - \int_use:N \l_cookie_rowcount_int } { ##1 }
\int_incr:N \l_cookie_rowcount_int
}
}
\cs_new_protected:Npn \squarepatternmaker (#1,#2) #3;
{
\cookie_square_pattern_maker:nnn { #1 } { #2 } { #3 }
}
\ExplSyntaxOff
\begin{document}
\begin{tikzpicture}
\draw[style=help lines,step=1cm] (-2.3,-2.3) grid (2.3,2.3);
\rowpatternmaker (-2,1) grby;
\rowpatternmaker (-2,0) rgyb;
\rowpatternmaker (-2,-1) ybrg;
\rowpatternmaker (-2,-2) brgy;
\end{tikzpicture}
\begin{tikzpicture}
\draw[style=help lines,step=1cm] (-2.3,-2.3) grid (2.3,2.3);
\squarepatternmaker (-2,1) grby rgyb ybrg brgy;
\end{tikzpicture}
\end{document}