100 一个趣味循环编程练习

发布于 2026-01-31 21:51:59

如题,希望实现如下的表格的自动化排版。

代码越简洁、越高度自动化、越具有强拓展性越好。

对具体的实现方法不做任何限制~优雅即可

image.png

划去线的逻辑不必与此相同(我觉得涂色似乎更显眼,在tabularray框架里应该也更容易实现),能体现不同的条件判断即可。

image.png

由于我也会试试,这里暂不给出MWE.

查看更多

关注者
0
被浏览
102
2 个回答
shadow
shadow 5小时前
这个人懒得不得了,竟然啥也没写

用 tikz 实现。

\documentclass[tikz,border=1cm]{standalone}
\begin{document}

\begin{tikzpicture}[
  cell/.style={
    draw,
    line width=0.8pt,
    minimum width=7.2mm,
    minimum height=6mm,
    inner sep=0pt,
    font=\small
  }
]

\def\inset{1mm}

\foreach \x in {1,...,100} {

  
  \pgfmathsetmacro{\row}{int((\x-1)/10)}
  \pgfmathsetmacro{\col}{int((\x-1) - 10*\row)}

  
  \node[cell] (N\x) at ({7.2mm*\col},{-6mm*\row}) {\x};

  
  \ifnum\x=2\relax\else
  \ifnum\x=3\relax\else
  \ifnum\x=5\relax\else
  \ifnum\x=7\relax\else

    
    \pgfmathtruncatemacro{\mii}{mod(\x,2)}
    \ifnum\mii=0
      \draw[thick]
        ([xshift=\inset,yshift=-\inset]N\x.north west) --
        ([xshift=-\inset,yshift=\inset]N\x.south east);

    \else
     
      \pgfmathtruncatemacro{\miii}{mod(\x,3)}
      \ifnum\miii=0
        \draw[thick]
          ([xshift=-\inset,yshift=-\inset]N\x.north east) --
          ([xshift=\inset,yshift=\inset]N\x.south west);

      \else
       
        \pgfmathtruncatemacro{\mv}{mod(\x,5)}
        \ifnum\mv=0
          \draw[thick]
            ([xshift=\inset,yshift=-\inset]N\x.north west) --
            ([xshift=-\inset,yshift=\inset]N\x.south east);
          \draw[thick]
            ([xshift=-\inset,yshift=-\inset]N\x.north east) --
            ([xshift=\inset,yshift=\inset]N\x.south west);

        \else
         
          \pgfmathtruncatemacro{\mvii}{mod(\x,7)}
          \ifnum\mvii=0
            \draw[thick]
              ([xshift=\inset]N\x.west) --
              ([xshift=-\inset]N\x.east);
          \fi
        \fi
      \fi
    \fi

  \fi\fi\fi\fi
}

\end{tikzpicture}

\end{document}

image.png

Sagittarius Rover
Sagittarius Rover 1小时前
这家伙很懒,什么也没写!

基于tabularray的一个自动化方案...

\documentclass{article}
\usepackage{fourier}
\usepackage{xcolor}
\usepackage{tabularray}
\UseTblrLibrary{functional}
\IgnoreSpacesOn
\prgNewFunction \makeHundredCells { } {
  \tlSet \lTmpaTl { } 
  \intStepOneInline {1} {100} {
    \intCaseF { ##1 } {
        { 2 } {  }
        { 3 } {  }
        { 5 } {  }
        { 7 } {  }
    } {
        \intCompareTF { \intMathMod{##1}{2}  } = { 0 } {
          \tlPutRight \lTmpaTl { \SetCell{bg=magenta!50} } 
        }{
            \intCompareTF { \intMathMod{##1}{3} } = { 0 } {
              \tlPutRight \lTmpaTl { \SetCell{bg=cyan!50} } 
            }
            {
                \intCompareTF { \intMathMod{##1}{5} } = { 0 } {
                  \tlPutRight \lTmpaTl { \SetCell{bg=violet!50} } 
                }
                {
                    \intCompareT { \intMathMod{##1}{7} } = { 0 } {
                      \tlPutRight \lTmpaTl { \SetCell{bg=orange!50} } 
                    }
                }
            }
        }
      }
     \intCompareTF { ##1 } = { 1 } {
        \tlPutRight \lTmpaTl { } 
      }{
        \tlPutRight \lTmpaTl { ##1 }
      }
    \intCompareTF { \intMathMod{##1}{10} } = { 0 } {
      \tlPutRight \lTmpaTl { \\ } 
    }{
      \tlPutRight \lTmpaTl { & }
    }
  }
  \prgReturn { \expValue \lTmpaTl }
}
\IgnoreSpacesOff

\begin{document}

\begin{tblr}[evaluate=\makeHundredCells]{
        colspec = {*{10}{X[c]}},
        hlines, vlines,
        rows = {m, ht=3em},
    }
    \makeHundredCells 
\end{tblr}

\end{document}

或者等价的,用逻辑划分更清晰的evaluate = \makeTableBodyprocess = \myTblrProcessor分别控制内容和样式

\documentclass{article}
\usepackage{fourier}
\usepackage{xcolor}
\usepackage{tabularray}
\UseTblrLibrary{functional}
\IgnoreSpacesOn
\tlNew \lBodyTl
\tlNew \lCellValueTl
\prgNewFunction \makeTableBody { } {
  \tlClear \lBodyTl
  \intStepOneInline {1} {100} {
    \tlPutRight \lBodyTl { ##1 }
    \intCompareTF { \intMathMod{##1}{10} } = { 0 } {
      \tlPutRight \lBodyTl { \\ }
    }{
      \tlPutRight \lBodyTl { & }
    }
  }
  \prgReturn { \expValue \lBodyTl }
}
\prgNewFunction \myTblrProcessor { } {
  \intStepOneInline {1} {\arabic{rowcount}} {
    \intStepOneInline {1} {\arabic{colcount}} {
      \tlSet \lCellValueTl { \cellGetText {##1} {####1} }
        \intCompareT { \lCellValueTl } = { 1 } {
          \cellSetText {##1} {####1} { }
        }
        \intCaseF { \lCellValueTl } {
            { 2 } { } { 3 } { } { 5 } { } { 7 } { }
        } {
            \intCompareTF { \intMathMod{\lCellValueTl}{2} } = { 0 } {
              \cellSetStyle {##1} {####1} { bg=magenta!50 }
            }{
              \intCompareTF { \intMathMod{\lCellValueTl}{3} } = { 0 } {
                \cellSetStyle {##1} {####1} { bg=cyan!50 }
              }{
                \intCompareTF { \intMathMod{\lCellValueTl}{5} } = { 0 } {
                  \cellSetStyle {##1} {####1} { bg=violet!50 }
                }{
                  \intCompareT { \intMathMod{\lCellValueTl}{7} } = { 0 } {
                    \cellSetStyle {##1} {####1} { bg=orange!50 }
                  }
                }
              }
            }
        }
    }
  }
}
\IgnoreSpacesOff

\begin{document}

\begin{tblr}[
    evaluate = \makeTableBody
]{
  colspec = {*{10}{X[c]}},
  hlines, vlines,
  rows = {m, ht=3em},
  process = \myTblrProcessor
}
  \makeTableBody
\end{tblr}

\end{document}

image.png

撰写答案

请登录后再发布答案,点击登录

发布
问题

分享
好友

手机
浏览

扫码手机浏览