答案1
这些都是使用 的有趣练习\foreach
。下面定义的宏\LabGrid
和\ChemGrid
都接受可变数量的行/列,并且\LabGrid
具有颜色的可选参数。下面的 MWE 产生:
以下是代码:
\documentclass{article}
\usepackage{tikz}
%usage: \LabGrid[colour]{CAPITAL letter for last row/column}
\newcommand\LabGrid[2][purple]{%
\begin{tikzpicture}[xscale=1]
\foreach \row [count=\r,remember=\r] in {A,...,#2} {
\draw[fill=#1!10!white](0,-\r) rectangle ++(2,1);
\node at (1,0.5-\r){$\row$};
\foreach \c in {1,...,\r} {
\draw[thick](2*\c,-\r) rectangle ++(2,1);
}
}
\draw[fill=#1!10!white](0,-1-\r) rectangle ++(2,1);
\foreach \row [count=\c] in {A,...,#2} {
\draw[fill=#1!10!white](2*\c,-1-\r) rectangle ++(2,1);
\node at (2*\c+1,-0.5-\r) {\row};
}
\end{tikzpicture}%
}
% usage: \ChemGrid{comma separated list of chemicals}
\newcommand\ChemGrid[1]{%
\begin{tikzpicture}[xscale=1.8,
header/.style={rectangle,minimum width=2cm,minimum height=1cm}]
\foreach \row [count=\r, remember=\r] in {#1} {
\node[header] at (1.5,0.2-2*\r){\row};
\draw(1,-2*\r)rectangle++(1,2);
\foreach \chem [count=\c] in {#1} {
\draw[thick](2*\c,-2*\r)--++(2,0)--++(0,2);
\draw(2*\c+1,2-2*\r)--++(0,-1)--++(1,0);
}
}
\foreach \row [count=\c, remember=\c] in {#1} {
\node[header] at (2*\c+1,-0.3-2*\r) {\row};
\draw(2*\c,-0.5-2*\r)--++(0,0.5);
}
\draw(2*\c+2,-0.5-2*\r)--++(0,0.5);
\end{tikzpicture}
}
\begin{document}
\LabGrid{C}\bigskip
\LabGrid[green]{E}\bigskip
\ChemGrid{NH$_4$O$_2$, BACl$_2$, CuSo$_4$}
\end{document}
编辑
我意识到节点无法随和扩展xscale
。yscale
我已修复此问题。
如图所示,微调盒子大小的最简单方法是在环境中设置xscale
和。yscale
tikzpicture