creando una función en TikZ que puede dividir e interpretar cadenas

creando una función en TikZ que puede dividir e interpretar cadenas

Necesito dibujar un montón de patrones simples con algunos colores, como en el siguiente ejemplo.

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

Producción

Sin embargo, esta es claramente una forma ineficiente de crear la imagen, y lo ideal sería tener una función rowpatternmaker coords stringque rowpatternmaker (-2,0) rgybcreara una fila de cuadrados de colores a partir de (-2,0) que, de izquierda a derecha, son rojos, verdes y amarillos. , azul. De esta manera, podría crear el patrón anterior con

rowpatternmaker (-2,1) grby
rowpatternmaker (-2,0) rgyb
rowpatternmaker (-2,-1) ybrg
rowpatternmaker (-2,-2) brgy

Por supuesto, lo que realmente me gustaría es hacer una función square pattern makerdonde

squarepatternmaker (-2,1) grby rgyb ybrg brgy

crearía el patrón de arriba.

Si bien he creado algunas figuras con TikZ, no estoy seguro de cómo crear una función como esta, por ejemplo una que pueda leer una cadena "grby" e interpretarla como una lista de colores.

Suponiendo que sea posible, ¿cómo haría para crear rowpatternmaker(o squarepatternmaker)?

Respuesta1

Tener palabras simples como comandos es innecesariamente difícil en LaTeX. Por eso presento

\rowpatternmaker (-2,1) grby;
\rowpatternmaker (-2,0) rgyb;
\rowpatternmaker (-2,-1) ybrg;
\rowpatternmaker (-2,-2) brgy;

Espero que esté bien. El primer argumento es la coordenada inicial y el segundo es la secuencia de colores. Si deja la secuencia de colores vacía, el valor predeterminado será rgby. La longitud de la línea está determinada por la longitud de la secuencia de colores, es decir, rgbrgbtendrá una longitud de seis.

El creador de patrones cuadrados también es posible.

\squarepatternmaker (-2,1) grby rgyb ybrg brgy;

Se implementa en términos del creador de patrones de filas.

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

ingrese la descripción de la imagen aquí

información relacionada