我需要一个 4*4 的网格来填充选定的颜色。基本上,我正在寻找一个\newcommand
,如下所示:
\newcommand{\mygrid}{11/blue, 32/brown} %coordinates/color is passed
这将产生以下网格:
之前也有人问过类似的问题:
我尝试从所有问题的答案开始,但中途迷失了方向。任何帮助都将不胜感激。
编辑这是我尝试的最后一个可行版本(来自这里):
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\pgfmathdeclarerandomlist{MyRandomColors}{%
{red}%
{red!25}%
{magenta}%
{magenta!25}%
{olive}%
{olive!25}%
{brown}%
{brown!10}%
{violet}%
{violet!25}%
{gray}%
{purple}%
{yellow}%
{orange}%
{orange!25}%
{cyan}%
{green}%
}%
\newcommand*{\GridSize}{4}
\newcommand*{\ColorCells}{%
\foreach \y in {1,...,\GridSize} {
\foreach \x in {1,...,\GridSize} {
\pgfmathrandomitem{\RandomColor}{MyRandomColors}
\node [fill=\RandomColor, fill opacity=0.4, draw=none, thick, minimum size=1cm]
at (\x-.5,\y-.5)
{\x\y};
}%
}%
}%
%%%%%
%\listfiles
\begin{document}
\begin{tikzpicture}[scale=1]
\begin{scope}[thick,]
\ColorCells
\draw (0, 0) grid (\GridSize, \GridSize);
\coordinate (input);
\end{scope}
\end{tikzpicture}
\end{document}
编辑2这是另一个(基于这)[论点 2 支持将网格放置到首选位置]:
\documentclass{standalone}
\usepackage{tikz,amsmath}
\usetikzlibrary{positioning,backgrounds,shadings,calc,patterns, decorations.pathreplacing, fit, arrows}
\newcommand{\vrect}[3]{
% \foreach \mark/\colorname [count=\k] in {#3}{
% \ifnum\mark=34 \def\mycolorname{\colorname}\fi
% }
\foreach \x in {1,...,4}{
\foreach \y in {1,...,4} {
\foreach \px\py in {#2}{
\node[draw=gray, fill=red!30,thick, minimum size=1cm, anchor=mid,
] (z#1\x\y\px\py) at ($(\y,-\x)+(\px,\py)$) {#1\x\y\px\py}; %
}
}
}
}
\begin{document}
\begin{tikzpicture}[scale=1]
\vrect{0}{6/6}{12/red,11/green}
\end{tikzpicture}
\end{document}
答案1
这是基于第一次编辑。注意:我name
为网格指定了名称,以便您可以使用坐标(name.south east)
等。
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\newcommand*{\GridSize}{4}
\newcommand*{\ColorCells}[1]{% #1 = list of x/y/color
\foreach \x/\y/\color in {#1} {
\node [fill=\color, draw=none, thick, minimum size=1cm]
at (\x-.5,\GridSize+0.5-\y) {};
}%
}%
%%%%%
%\listfiles
\begin{document}
\begin{tikzpicture}[scale=1]
\begin{scope}[thick,local bounding box=name]
\ColorCells{1/1/blue, 2/3/red, 3/2/green, 4/4/yellow}
\draw (0, 0) grid (\GridSize, \GridSize);
\end{scope}
\end{tikzpicture}
\end{document}
答案2
\definegrid[rows,cols]
定义\grows
和\gcols
并继续定义网格的所有单元格\grid[i,j]
,其中i
和j
是行和列索引,为\wh
(白色)。请注意,\grid[i,j]
定义为,\csname
因为i
和j
是 catcode 12。
然后,\setgrid[i,j]{color}
用于将单元格重新定义\grid[i,j]
为所需的颜色。
最后,将细胞以堆栈的形式\drawgrid
重新吐出。\grid[i,j]
每个单元格都是一个\fbox
,顺便说一下,尺寸可以根据需要设置。
修订内容standalone
:
\documentclass{standalone}
\usepackage{xcolor,stackengine,pgffor}
\def\block#1{\kern-\fboxrule\fboxsep=0pt\fbox{\color{#1}\rule{1ex}{1ex}}}
\def\wh{\block{white}}
\setstackgap{S}{-\fboxrule}
\setstackEOL{\\}
\makeatletter
\def\definegrid[#1,#2]{%
\def\grows{#1}%
\def\gcols{#2}%
\foreach\i in {1,...,#1}%
{%
\foreach\j in {1,...,#2}%
{%
\expandafter\gdef\csname grid[\i,\j]\endcsname{\wh}%
}%
}%
\ignorespaces}
\newcommand\drawgrid{%
\def\stackbuild{}%
\foreach\i in {1,...,\grows}%
{%
\ifnum\i=1\else\g@addto@macro\stackbuild{\\}\fi%
\foreach\j in {1,...,\gcols}%
{%
\expandafter\g@addto@macro\expandafter\stackbuild\expandafter{%
\csname grid[\i,\j]\endcsname}%
}%
}%
\kern\fboxrule%
\expandafter\Shortstack\expandafter{\stackbuild}%
}
\def\setgrid[#1,#2]#3{\expandafter\gdef\csname grid[#1,#2]\endcsname{\block{#3}}%
\ignorespaces}
\makeatother
\begin{document}
\definegrid[4,4]
%
\setgrid[1,1]{blue}
\setgrid[3,2]{red}
\setgrid[4,4]{green}
%
\drawgrid
\end{document}
这是一个版本,其中\definegrid[rows,cols]{default-color}
采用强制尾随参数来设置网格的默认颜色。
\documentclass{standalone}
\usepackage{xcolor,stackengine,pgffor}
\def\block#1{\kern-\fboxrule\fboxsep=0pt\fbox{\color{#1}\rule{1ex}{1ex}}}
\setstackgap{S}{-\fboxrule}
\setstackEOL{\\}
\makeatletter
\def\definegrid[#1,#2]#3{%
\def\grows{#1}%
\def\gcols{#2}%
\foreach\i in {1,...,#1}%
{%
\foreach\j in {1,...,#2}%
{%
\expandafter\gdef\csname grid[\i,\j]\endcsname{\block{#3}}%
}%
}%
\ignorespaces}
\newcommand\drawgrid{%
\def\stackbuild{}%
\foreach\i in {1,...,\grows}%
{%
\ifnum\i=1\else\g@addto@macro\stackbuild{\\}\fi%
\foreach\j in {1,...,\gcols}%
{%
\expandafter\g@addto@macro\expandafter\stackbuild\expandafter{%
\csname grid[\i,\j]\endcsname}%
}%
}%
\kern\fboxrule%
\expandafter\Shortstack\expandafter{\stackbuild}%
}
\def\setgrid[#1,#2]#3{\expandafter\gdef\csname grid[#1,#2]\endcsname{\block{#3}}%
\ignorespaces}
\makeatother
\begin{document}
\definegrid[4,4]{yellow}
%
\setgrid[1,1]{blue}
\setgrid[3,2]{red}
\setgrid[4,4]{green}
%
\drawgrid
\end{document}