答案1
钛钾是
这是一个通过 TikZ 和矩阵节点的快速解决方案,您必须自己指定列数和行数。
不,TikZ 并不是真正必要的,而且可以改进……
两个全局计数器用于跟踪所需的药丸数量。
代码
\documentclass{article}
\usepackage{tikz}
\newcounter{friendsPositive}
\newcounter{friendsNegative}
\tikzset{
friends of positive/.style={draw, fill=blue!50},
friends of negative/.style={draw, fill=none},
friends of/.style args={#1+#2}{
/utils/exec=\pgfmathsetcounter{friendsPositive}{#1}%
\pgfmathsetcounter{friendsNegative}{#2},
matrix, x=+5mm, y=+5mm, % xy scaling
every outer matrix/.append style={% tight invisible matrix
shape=rectangle, path only, inner sep=+0pt, outer sep=+0pt},
row sep=+-\pgflinewidth, column sep=+-\pgflinewidth,
execute at empty cell={
\draw (-.5,-.5) rectangle (.5,.5);
\ifnum\value{friendsPositive}>0
\addtocounter{friendsPositive}{-1}
\path[friends of positive] (0,0) circle[radius=.3];
\else
\ifnum\value{friendsNegative}>0
\addtocounter{friendsNegative}{-1}
\path[friends of negative] (0,0) circle[radius=.3];
\fi
\fi}}}
\begin{document}
Friends of $10 = \mathcolor{blue!50}{ 2} + 8$:
\tikz\matrix[friends of = 2 + 8]{ & \\ & \\ & \\ & \\ & \\};\par
Friends of $ 9 = \mathcolor{blue!50}{ 4} + 5$:
\tikz\matrix[friends of = 4 + 5]{ & & \\ & & \\ & & \\};\par
Friends of $16 = \mathcolor{blue!50}{ 7} + 9$:
\tikz\matrix[friends of = 7 + 9]{ & & & \\ & & & \\ & & & \\ & & & \\};\par
Friends of $24 = \mathcolor{blue!50}{12} + 12$:
\tikz\matrix[friends of = 12 + 12]{
& & & & \\ & & & & \\ & & {} & & \\ & & & & \\ & & & & \\};
\end{document}
输出
tabular
+ 一点 LaTeX3
LaTeX3 并不是真正必要的(\int_mod:nn
虽然很方便)——它只是整数算术——否则只使用tabular
。
宏\friendsofYes
指定\friendsofNo
各个单元格的外观。
这不使用全局计数器或整数变量,而是进行内联数学运算(如果数字大得多,则效率会变得低下)。
代码
\documentclass{article}
\usepackage{array, xcolor}
\ExplSyntaxOn
\NewDocumentCommand{\friendsof}{m m m}{{
% #1 = columns (2), #2 = yes (2), #3 = nos (8)
\setlength\tabcolsep{.4em}
\begin{tabular}{|*{#1}{c|}}
\hline
\__friendsof_do:nnnn {#1}{#1}{#2}{#3}
\int_compare:nNnT {\int_mod:nn{#2+#3}{#1}} > 0 { \\ \hline }
\end{tabular}
}}
\cs_new:Nn \__friendsof_do:nnnn {
\int_compare:nNnTF { #1 } = { 1 }
{ \__friendsof_do_:nnnnn { #2 }{ #2 }{ #3 }{ #4 }{ \\ \hline } }
{ \__friendsof_do_:nnnnn {#1-1}{ #2 }{ #3 }{ #4 }{ & } } }
\cs_new:Nn \__friendsof_do_:nnnnn {
\int_compare:nNnTF { #3 } > { 0 }
{ \friendsofYes #5 \__friendsof_do:nnnn {#1}{#2}{#3-1}{#4} }
{ \int_compare:nNnT { #4 } > { 0 }
{ \friendsofNo #5 \__friendsof_do:nnnn {#1}{#2}{#3}{#4-1} } } }
\ExplSyntaxOff
\newcommand*\friendsofYes{{\color{blue!50}$\bullet$}\llap{$\circ$}}
\newcommand*\friendsofNo{$\circ$}
\begin{document}
Friends of $10 = \mathcolor{blue!50}{ 2} + 8$: \friendsof{2}{2}{8}\par
Friends of $ 9 = \mathcolor{blue!50}{ 4} + 5$: \friendsof{3}{4}{5}\par
Friends of $16 = \mathcolor{blue!50}{ 7} + 9$: \friendsof{4}{7}{9}\par
Friends of $24 = \mathcolor{blue!50}{12} + 12$: \friendsof{5}{12}{12}\par
\end{document}