在 TikZ 中建立可以分解和解釋字串的函數

在 TikZ 中建立可以分解和解釋字串的函數

我發現自己需要用幾種顏色繪製一堆簡單的圖案,如下例所示。

\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;

我希望沒關係。第一個參數是起始座標,第二個參數是顏色序列。如果將顏色序列留空,它將預設為rgby。線條長度由顏色序列的長度決定,即rgbrgb長度為六。

方形圖案製作器也是可以的

\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}

在此輸入影像描述

相關內容