使用 tikz 跳过计数网格制作 beamer 文件

使用 tikz 跳过计数网格制作 beamer 文件

我正在尝试制作一个从 1 到 120 的表格,其中我可以识别每个单元格并为其着色以显示跳过计数。

下面是我尝试实现的从 30 到 75 的以 5 为单位跳过计数的前几张幻灯片。

幻灯片 1 - 没有突出显示数字

幻灯片 2 - 30 深色突出显示

幻灯片 3 - 30 亮,35 暗

幻灯片 4 - 30 和 35 亮,40 暗

网格本身需要大量代码来生成:

\newcommand{\OneTwentyGrid}{\node (1) at (0.7,6){\footnotesize{1}};
\node (2) at (1.4,6){\footnotesize{2}};
\node (3) at (2.1,6){\footnotesize{3}};
\node (4) at (2.8,6){\footnotesize{4}};
\node (5) at (3.5,6){\footnotesize{5}};
\node (6) at (4.2,6){\footnotesize{6}};
\node (7) at (4.9,6){\footnotesize{7}};
\node (8) at (5.6,6){\footnotesize{8}};
\node (9) at (6.3,6){\footnotesize{9}};
\node (10) at (7,6){\footnotesize{10}};
\node (11) at (0.7,5.5){\footnotesize{11}};
\node (12) at (1.4,5.5){\footnotesize{12}};
\node (13) at (2.1,5.5){\footnotesize{13}};
\node (14) at (2.8,5.5){\footnotesize{14}};
\node (15) at (3.5,5.5){\footnotesize{15}};
\node (16) at (4.2,5.5){\footnotesize{16}};
\node (17) at (4.9,5.5){\footnotesize{17}};
\node (18) at (5.6,5.5){\footnotesize{18}};
\node (19) at (6.3,5.5){\footnotesize{19}};
\node (20) at (7,5.5){\footnotesize{20}};
...
\node (118) at (5.6,0.5){\scriptsize{118}};
\node (119) at (6.3,0.5){\scriptsize{119}};
\node (120) at (7,0.5){\scriptsize{120}};
}

然后,对跳过计数进行突出显示也需要大量代码:

\GridCountOneTwenty{\only<3->{\node[box,fill=titlebg!20] at (30){};}\only<2>{\node[box,fill=titlebg!60] at (30){};}\only<3->{\node[box,fill=titlebg!20] at (35){};}\only<3>{\node[box,fill=titlebg!60] at (35){};}\only<4->{\node[box,fill=titlebg!20] at (40){};}\only<4>{\node[box,fill=titlebg!60] at (40){};}\only<5->{\node[box,fill=titlebg!20] at (45){};}\only<5>{\node[box,fill=titlebg!60] at (45){};}\only<6->{\node[box,fill=titlebg!20] at (50){};}\only<6>{\node[box,fill=titlebg!60] at (50){};}\only<7->{\node[box,fill=titlebg!20] at (55){};}\only<7>{\node[box,fill=titlebg!60] at (55){};}\only<8->{\node[box,fill=titlebg!20] at (60){};}\only<8>{\node[box,fill=titlebg!60] at (60){};}\only<9->{\node[box,fill=titlebg!20] at (65){};}\only<9>{\node[box,fill=titlebg!60] at (65){};}\only<10->{\node[box,fill=titlebg!20] at (70){};}\only<10>{\node[box,fill=titlebg!60] at (70){};}\only<11->{\node[box,fill=titlebg!20] at (75){};}\only<11>{\node[box,fill=titlebg!60] at (75){};}}{30}{up}{5}{\onslide<12->\node[text width=3cm] at (10,5) {$60+5=\onslide<13->{\alert<14>{65}}$};\onslide<14->\node[text width=3cm] at (10,4) {$65+5=\onslide<15->{\alert<16>{70}}$};\onslide<16->\node[text width=3cm] at (10,3) {$30+5=\onslide<17->{\alert<18>{35}}$};}

有没有更高效的方法?使用此方法会导致 overleaf 在编译时超时。

答案1

\documentclass{article}
\usepackage{ifthen}
\usepackage{listofitems}
\usepackage{multicol}
\usepackage{tikz}
\usepackage{pgfplots}

% commands to go to and from a number to (row,col)
\newcommand{\getrow}[1]{int((#1-0.1)/10)+1}
\newcommand{\getcol}[1]{ifthenelse(int(mod(#1,10))==0,10,int(mod(#1,10)))}
\newcommand{\getpos}[2]{(#2*\bw,-#1*\bh)}
\newcommand{\getnum}[2]{int(#2+10*(#1-1))}

% draws the grid
% argument 1: primary highlight
% argument 2: secondary highlight
\newcommand{\drawgrid}[2]{%
\begin{tikzpicture}[inner sep=0 cm]
    \pgfmathsetmacro{\bw}{0.5} % box width in cm
    \pgfmathsetmacro{\bh}{\bw} % box height in cm
    \tikzstyle{box} = [minimum width=\bw cm, minimum height=\bh cm, rectangle, draw]
    \tikzstyle{primary highlight} = [minimum width=\bw cm, minimum height=\bh cm, rectangle, fill=green!70]
    \tikzstyle{secondary highlight} = [minimum width=\bw cm, minimum height=\bh cm, rectangle, fill=green!25]
    
    % first highlight the required cells
    \ifthenelse{\equal{#1}{}}{% do nothing if empty
    }{\readlist*{\primaryhighlight}{#1}
        \foreachitem \n \in \primaryhighlight{%
            \pgfmathparse{\getrow{\n}}
            \pgfmathsetmacro\row{\pgfmathresult}
            \pgfmathparse{\getcol{\n}}
            \pgfmathsetmacro\col{\pgfmathresult}
            \node [primary highlight] at \getpos{\row}{\col} {};
    }}
    
    \ifthenelse{\equal{#2}{}}{% do nothing if empty
    }{\readlist*{\secondaryhighlight}{#2}
    \foreachitem \n \in \secondaryhighlight{%
        \pgfmathparse{\getrow{\n}}
        \pgfmathsetmacro\row{\pgfmathresult}
        \pgfmathparse{\getcol{\n}}
        \pgfmathsetmacro\col{\pgfmathresult}
        \node [secondary highlight] at \getpos{\row}{\col} {};
    }}
    
    % now put the numbers
    \foreach \col in {1,2,...,10}{%
        \foreach \row in {1,2,...,12}{%
            \node [box] at \getpos{\row}{\col} {\footnotesize{\pgfmathparse{\getnum{\row}{\col}}\pgfmathresult}};
        }
    }
\end{tikzpicture}
}

\begin{document}
    \begin{multicols}{2}
        \drawgrid{}{}
        
        \drawgrid{30}{}
        
        \drawgrid{35}{30}
        
        \drawgrid{45}{30,35,40}
    \end{multicols}
\end{document}

在此处输入图片描述

相关内容