我想编写一个background
接受 2 个参数的宏,每个参数可以包含一个以 分隔的数字列表,
,以便下面的代码绘制两个带有边框的框。
\usepackage{tikz}
\begin{document}
\begin{center}
\begin{tikzpicture}[]
% a box has 3 rows and 4 columns,
% the heights of the rows are respectively 1, 2 and 1;
% the widths of the columns are respectively 1, 2, 2 and 1:
\background{1,2,1}{1,2,2,1}
\end{tikzpicture}
\end{center}
\begin{center}
\begin{tikzpicture}[]
% a box has 4 rows and 3 columns,
% the heights of the rows are respectively 1, 2, 3 and 1;
% the widths of the columns are respectively 1, 2 and 1:
\background{1,2,3,1}{1,2,1}
\end{tikzpicture}
\end{center}
\end{document}
绘制边界不是问题,困难的是如何遍历各个参数中的数字background
,它们的数量是多种多样的。
有谁知道这是怎么做到的吗?
编辑1:预期第一个框:
预期的第二个框:
答案1
你可以使用它\foreach
来完成这项工作。下面我修改了@percusse的代码这个答案调整单元格尺寸。
平均能量损失
\documentclass{article}
\usepackage{tikz,etoolbox}
\usetikzlibrary{matrix}
\newcommand\background[2]{
\let\mymatrixcontent\empty
\newcommand{\row}{%
\foreach \j in {#1}{
\foreach \i in {#2} {%
\begingroup\edef\x{\endgroup
\noexpand\gappto\noexpand\mymatrixcontent{|[minimum height=\j cm,minimum width=\i cm,draw]| \&}}\x
}%
\gappto\mymatrixcontent{\\}%
}
}
\row
\matrix[matrix of nodes,ampersand replacement=\&,row sep=-\pgflinewidth,column sep=-\pgflinewidth]{
\mymatrixcontent
};
}
\begin{document}
\begin{center}
\begin{tikzpicture}[]
% a box has 3 rows and 4 columns,
% the heights of the rows are respectively 1, 2 and 1;
% the widths of the columns are respectively 1, 2, 2 and 1:
\background{1,2,1}{1,2,2,1}
\end{tikzpicture}
\begin{tikzpicture}[]
% a box has 4 rows and 3 columns,
% the heights of the rows are respectively 1, 2, 3 and 1;
% the widths of the columns are respectively 1, 2 and 1:
\background{1,2,3,1}{1,2,1}
\end{tikzpicture}
\end{center}
\end{document}