棋盘:如何将数字写入字段

棋盘:如何将数字写入字段

我正在使用很棒的棋盘包,并且希望将数字写入棋盘的每个字段以得到这样的棋盘:

棋盘上有数字

目前,我问了python

for i in range(64):
    case = f"{chr(97+i%8)}{1+i//8}"
    print(f"   text=\\scriptsize\\bfseries {i},markfield={case},%")

给我写以下64行。

\documentclass{article}
\usepackage{chessboard}
\begin{document}
\setchessboard{showmover=false}
\chessboard[%
   smallboard,%
   pgfstyle={[base,at={\pgfpoint{0pt}{-0.4ex}}]text},%
   text=\scriptsize\bfseries 0,markfield=a1,%
   text=\scriptsize\bfseries 1,markfield=b1,%
   text=\scriptsize\bfseries 2,markfield=c1,%
   text=\scriptsize\bfseries 3,markfield=d1,%
   text=\scriptsize\bfseries 4,markfield=e1,%
   text=\scriptsize\bfseries 5,markfield=f1,%
   text=\scriptsize\bfseries 6,markfield=g1,%
   text=\scriptsize\bfseries 7,markfield=h1,%
   text=\scriptsize\bfseries 8,markfield=a2,%
   text=\scriptsize\bfseries 9,markfield=b2,%
   text=\scriptsize\bfseries 10,markfield=c2,%
   text=\scriptsize\bfseries 11,markfield=d2,%
   text=\scriptsize\bfseries 12,markfield=e2,%
   text=\scriptsize\bfseries 13,markfield=f2,%
   text=\scriptsize\bfseries 14,markfield=g2,%
   text=\scriptsize\bfseries 15,markfield=h2,%
   text=\scriptsize\bfseries 16,markfield=a3,%
   text=\scriptsize\bfseries 17,markfield=b3,%
   text=\scriptsize\bfseries 18,markfield=c3,%
   text=\scriptsize\bfseries 19,markfield=d3,%
   text=\scriptsize\bfseries 20,markfield=e3,%
   text=\scriptsize\bfseries 21,markfield=f3,%
   text=\scriptsize\bfseries 22,markfield=g3,%
   text=\scriptsize\bfseries 23,markfield=h3,%
   text=\scriptsize\bfseries 24,markfield=a4,%
   text=\scriptsize\bfseries 25,markfield=b4,%
   text=\scriptsize\bfseries 26,markfield=c4,%
   text=\scriptsize\bfseries 27,markfield=d4,%
   text=\scriptsize\bfseries 28,markfield=e4,%
   text=\scriptsize\bfseries 29,markfield=f4,%
   text=\scriptsize\bfseries 30,markfield=g4,%
   text=\scriptsize\bfseries 31,markfield=h4,%
   text=\scriptsize\bfseries 32,markfield=a5,%
   text=\scriptsize\bfseries 33,markfield=b5,%
   text=\scriptsize\bfseries 34,markfield=c5,%
   text=\scriptsize\bfseries 35,markfield=d5,%
   text=\scriptsize\bfseries 36,markfield=e5,%
   text=\scriptsize\bfseries 37,markfield=f5,%
   text=\scriptsize\bfseries 38,markfield=g5,%
   text=\scriptsize\bfseries 39,markfield=h5,%
   text=\scriptsize\bfseries 40,markfield=a6,%
   text=\scriptsize\bfseries 41,markfield=b6,%
   text=\scriptsize\bfseries 42,markfield=c6,%
   text=\scriptsize\bfseries 43,markfield=d6,%
   text=\scriptsize\bfseries 44,markfield=e6,%
   text=\scriptsize\bfseries 45,markfield=f6,%
   text=\scriptsize\bfseries 46,markfield=g6,%
   text=\scriptsize\bfseries 47,markfield=h6,%
   text=\scriptsize\bfseries 48,markfield=a7,%
   text=\scriptsize\bfseries 49,markfield=b7,%
   text=\scriptsize\bfseries 50,markfield=c7,%
   text=\scriptsize\bfseries 51,markfield=d7,%
   text=\scriptsize\bfseries 52,markfield=e7,%
   text=\scriptsize\bfseries 53,markfield=f7,%
   text=\scriptsize\bfseries 54,markfield=g7,%
   text=\scriptsize\bfseries 55,markfield=h7,%
   text=\scriptsize\bfseries 56,markfield=a8,%
   text=\scriptsize\bfseries 57,markfield=b8,%
   text=\scriptsize\bfseries 58,markfield=c8,%
   text=\scriptsize\bfseries 59,markfield=d8,%
   text=\scriptsize\bfseries 60,markfield=e8,%
   text=\scriptsize\bfseries 61,markfield=f8,%
   text=\scriptsize\bfseries 62,markfield=g8,%
   text=\scriptsize\bfseries 63,markfield=h8]
\end{document}

然而,我对这个解决方案并不满意。我读过如何绘制带有数字的棋盘?我怎样才能在字段上写数字?但没有找到解决方案。

查看文档的第 13 页和第 54 页,我考虑使用ranklabelfilelabel标签,例如

    text= \scriptsize\bfseries\arabic{ranklabel*8+filelabel},
    markboard,

但没有什么效果(我也在考虑 xparse)...

无论如何,有人有办法避免写 64 行代码,而直接写出漂亮的 LaTeX 代码吗?Perheaps,告诉我如何使用ranklabelfilelabel

我将非常感激您的帮助,杰拉尔德

答案1

\documentclass{article}
\usepackage{chessboard}
\ExplSyntaxOn %requires texlive 2020, in older system load expl3
\cs_new:Npn \getfieldnumber #1 
 {
  \fp_eval:n { (\tl_tail:V #1 -1)*8 + \exp_args:Ne\int_from_alph:n{\tl_head:V #1} -1}
 }
\ExplSyntaxOff
\begin{document}
\setchessboard{tinyboard,color=magenta,clearboard}
\chessboard[
pgfstyle=
{[base,at={\pgfpoint{0pt}{-0.3ex}}]text},
text= \fontsize{1.2ex}{1.2ex}\bfseries
\sffamily\getfieldnumber\currentwq,
markboard]


\end{document}

在此处输入图片描述

相关内容