如何绘制带有数字的棋盘?

如何绘制带有数字的棋盘?

如何绘制一个充满数字的棋盘,如下图所示?

通缉

我尝试过使用chessboard包但没有成功。

答案1

如果您真的想使用棋盘……

棋盘

\documentclass{article}
\usepackage{chessfss}

\setlength{\parindent}{0in}

\newlength{\symsize}\setlength{\symsize}{24pt}
\newlength{\boardwidth}\setlength{\boardwidth}{8\symsize}
\setboardfontsize{\symsize}

\newcommand{\board}[1]{\setlength{\fboxsep}{0pt}%
\fbox{\parbox{\boardwidth}{\setlength{\baselineskip}{\symsize}#1}}}
\newcommand{\row}[1]{\parbox[c][\symsize]{\symsize}{\hfill{#1}}}
\newcommand{\col}[1]{\parbox[b]{\symsize}{\hfil{#1}}}
\newcommand{\chessboard}[1]{\begin{tabular}{cc}
\parbox{\symsize}{\setlength{\baselineskip}{\symsize}
\row{8} \row{7} \row{6} \row{5} \row{4} \row{3} \row{2} \row{1}}&\board{#1}\\
\row{ } &\mbox{\col{a}\col{b}\col{c}\col{d}\col{e}\col{f}\col{g}\col{h}}
\end{tabular}}

\newcommand{\TextOnWhite}[1]{\WhiteEmptySquare\hspace{-\symsize}%
\raisebox{.35\symsize}{\makebox[\symsize][c]{\small #1}}}
\newcommand{\TextOnBlack}[1]{\BlackEmptySquare\hspace{-\symsize}%
\raisebox{.35\symsize}{\makebox[\symsize][c]{\small #1}}}

\begin{document}
\large

\begin{center}\chessboard{
\TextOnWhite{(8,1)}\TextOnBlack{(8,2)}\TextOnWhite{(8,3)}\TextOnBlack{(8,4)}%
\TextOnWhite{(8,5)}\TextOnBlack{(8,6)}\TextOnWhite{(8,7)}\TextOnBlack{(8,8)}\\
\TextOnBlack{(7,1)}\TextOnWhite{(7,2)}\TextOnBlack{(7,3)}\TextOnWhite{(7,4)}%
\TextOnBlack{(7,5)}\TextOnWhite{(7,6)}\TextOnBlack{(7,7)}\TextOnWhite{(7,8)}\\
\TextOnWhite{(6,1)}\TextOnBlack{(6,2)}\TextOnWhite{(6,3)}\TextOnBlack{(6,4)}%
\TextOnWhite{(6,5)}\TextOnBlack{(6,6)}\TextOnWhite{(6,7)}\TextOnBlack{(6,8)}\\
\TextOnBlack{(5,1)}\TextOnWhite{(5,2)}\TextOnBlack{(5,3)}\TextOnWhite{(5,4)}%
\TextOnBlack{(5,5)}\TextOnWhite{(5,6)}\TextOnBlack{(5,7)}\TextOnWhite{(5,8)}\\
\TextOnWhite{(4,1)}\TextOnBlack{(4,2)}\TextOnWhite{(4,3)}\TextOnBlack{(4,4)}%
\TextOnWhite{(4,5)}\TextOnBlack{(4,6)}\TextOnWhite{(4,7)}\TextOnBlack{(4,8)}\\
\TextOnBlack{(3,1)}\TextOnWhite{(3,2)}\TextOnBlack{(3,3)}\TextOnWhite{(3,4)}%
\TextOnBlack{(3,5)}\TextOnWhite{(3,6)}\TextOnBlack{(3,7)}\TextOnWhite{(3,8)}\\
\TextOnWhite{(2,1)}\TextOnBlack{(2,2)}\TextOnWhite{(2,3)}\TextOnBlack{(2,4)}%
\TextOnWhite{(2,5)}\TextOnBlack{(2,6)}\TextOnWhite{(2,7)}\TextOnBlack{(2,8)}\\
\TextOnBlack{(1,1)}\TextOnWhite{(1,2)}\TextOnBlack{(1,3)}\TextOnWhite{(1,4)}%
\TextOnBlack{(1,5)}\TextOnWhite{(1,6)}\TextOnBlack{(1,7)}\TextOnWhite{(1,8)}
}\end{center}

\end{document}

在上面叠加文字\WhiteEmptySquare有点浪费,但是chessfss包装有时采用两种不同的颜色,而不仅仅是黑色和白色。

答案2

\documentclass{report}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}[scale=1.25]
\foreach \x in {1,2,...,8}
\foreach \y in {1,2,...,8}
{
\draw (\x,\y) +(-.5,-.5) rectangle ++(.5,.5);
\draw (\x,\y) node{\small (\y,\x)};
}
\foreach \x in {1,2,...,8}{
\node at (\x,0) {\x};
\node at (0,\x) {\x};
}
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案3

MetaPost 解决方案,绘制一些类似真实棋盘的东西,为了好玩:

input latexmp; setupLaTeXMP(textextlabel = enable, mode = rerun);
u := 1.5cm; 
n := 8;
picture mypoint;
beginfig(1);
  for i = 1 upto n:
    for j = 1 upto n:
      mypoint := thelabel("$(" & decimal j & "," & decimal i & ")$", u*(i-.5, j-.5));
      if (i+j) mod 2 = 0:
        fill ((i-1, j-1) -- (i, j-1) -- (i, j) -- (i-1, j) -- cycle) scaled u;
        draw mypoint withcolor white;
      else: draw mypoint;
      fi;
    endfor;
    label.bot("$" & decimal i & "$", u*(i-.5, 0));
    label.lft("$" & decimal i & "$", u*(0, i-.5));
  endfor;
  draw (origin -- (n, 0) -- (n, n) -- (0, n) -- cycle) scaled u;
  setbounds currentpicture to boundingbox currentpicture enlarged 2bp;
endfig;
end.

如果有兴趣,可以用 Metafun 格式排版。例如,要直接获取 PDF 版本,可以在命令行中输入以下内容:

mptopdf -metafun -latex chessboard.mp

结果是:

在此处输入图片描述

答案4

PSTricks 解决方案使用xfp包裹:

\documentclass{article}

\usepackage{multido}
\usepackage{pstricks}
\psset{dimen = m}
\usepackage{xfp}

% Constants. 
\newcommand*\shift[1]{\fpeval{#1-0.5}}
\newcommand*\size[1]{\fpeval{\squaresize*#1}}
\newcommand*\maximum{\fpeval{\squaresize*\boardsize}}

% Parameters.
\def\squaresize{1}
\def\boardsize{8}

\begin{document}

\begin{pspicture}(-0.3,-0.4)(\maximum,\maximum)
  \multido{\iB = 1+1}{\boardsize}{%
    \uput[180](0,\shift{\iB}){$\iB$}
    \uput[270](\shift{\iB},0){$\iB$}
    \multido{\iA = 1+1}{\boardsize}{%
      \psframe(\size{\iA-1},\size{\iB-1})(\size{\iA},\size{\iB})
      \rput(\size{\shift{\iB}},\size{\shift{\iA}}){$(\iA,\iB)$}}}
\end{pspicture}

\end{document}

输出

\squaresize您所要做的就是选择和的值\boardsize

相关内容