2

## 利用LaTex套件tikz繪製棋盤

LaTex語法中，有一個套件叫作tikz

\documentclass[12pt]{article}
\usepackage{tikz}
\begin{document}
(裡面放你的正文)
\end{document}


# 用方格與圓畫棋盤棋子

\begin{tikzpicture}
\draw[step=1cm,blue,thick] (0,0) grid (5,5);
\draw (2,3) circle (.4cm); %指定圓的圓心座標與半徑
\fill[white] (2,3) circle (.4cm); %將棋子上色
\draw (0.5,0.5) circle (.4cm);
\fill[black] (0.5,0.5) circle (.4cm); %將棋子上色
\end{tikzpicture}


# 用方格與圓畫棋盤棋子-簡潔的寫法

\newcommand{\blackstone}[2]{
\draw (#1+0.5,#2+0.5) circle (0.4cm);
\fill[black] (#1+0.5,#2+0.5) circle (0.4cm);
}
\newcommand{\whitestone}[2]{\draw (#1+0.5,#2+0.5) circle (0.4cm);
\fill[white] (#1+0.5,#2+0.5) circle (0.4cm);
}


\begin{tikzpicture}
\board{5}{5};
\whitestone{2}{3};
\blackstone{0}{0};
\end{tikzpicture}


# 給棋盤塗上陰影區域

\begin{tikzpicture}
\board{5}{5};
\end{tikzpicture}


# 完整範例

Overleaf(線上Latex編譯器)上可執行

\usepackage{CJKutf8}\begin{document}內部的\begin{CJK}{UTF8}{bsmi}

\documentclass[12pt]{article}
\usepackage{CJKutf8, tikz}

\newcommand{\board}[2]{\draw[step=1cm,black,thick] (0,0) grid (#1,#2);}
\newcommand{\shadow}[2]{\fill[gray!50] (#1,#2) rectangle (#1+1,#2+1);}%在座標(#1,#2)處塗上陰影區域，「(0,0) rectangle (1,1);」指定長方形的對角頂點座標
\newcommand{\blackstone}[2]{\draw (#1+0.5,#2+0.5) circle (0.4cm); \fill[black] (#1+0.5,#2+0.5) circle (0.4cm);}%在座標(#1,#2)處放置棋子
\newcommand{\whitestone}[2]{\draw (#1+0.5,#2+0.5) circle (0.4cm); \fill[white] (#1+0.5,#2+0.5) circle (0.4cm);}%在座標(#1,#2)處放置棋子

\begin{document}
\begin{CJK}{UTF8}{bsmi}
\begin{center}
\textsc{\LARGE LaTex畫棋盤}
\end{center}

\section{用方格與圓畫棋盤棋子}

\begin{verbatim}
\begin{tikzpicture}
\draw[step=1cm,blue,thick] (0,0) grid (5,5);
\draw (2,3) circle (.4cm); %指定圓的圓心座標與半徑
\fill[white] (2,3) circle (.4cm); %將棋子上色
\draw (0.5,0.5) circle (.4cm);
\fill[black] (0.5,0.5) circle (.4cm); %將棋子上色
\end{tikzpicture}
\end{verbatim}
\begin{tikzpicture}
\draw[step=1cm,blue,thick] (0,0) grid (5,5);
\draw (2,3) circle (.4cm);
\fill[white] (2,3) circle (.4cm);
\draw (0.5,0.5) circle (.4cm);
\fill[black] (0.5,0.5) circle (.4cm);
\end{tikzpicture}

\section{用方格與圓畫棋盤棋子-簡潔的寫法}

\begin{verbatim}
\newcommand{\blackstone}[2]{
\draw (#1+0.5,#2+0.5) circle (0.4cm);
\fill[black] (#1+0.5,#2+0.5) circle (0.4cm);
}
\newcommand{\whitestone}[2]{\draw (#1+0.5,#2+0.5) circle (0.4cm);
\fill[white] (#1+0.5,#2+0.5) circle (0.4cm);
}
\end{verbatim}

\begin{verbatim}
\board{5}{5};
\whitestone{2}{3};
\blackstone{0}{0};
\end{verbatim}

\begin{tikzpicture}
\board{5}{5};
\whitestone{2}{3};
\blackstone{0}{0};
\end{tikzpicture}

\section{將棋盤塗上陰影區域}

\begin{verbatim}
\begin{tikzpicture}
\board{5}{5};
\end{tikzpicture}
\end{verbatim}

\begin{tikzpicture}