3 x 3 方框绘图,周围有数字

3 x 3 方框绘图,周围有数字

我想用 LaTeX 画一个 3 x 3 的方框,如下图所示。有没有简单的方法可以做到这一点?也许有专门的命令?

通缉

答案1

PSTricks 解决方案:

\documentclass{article}

\usepackage{multido,pstricks}

\begin{document}

\begin{pspicture}(-0.35,-0.4)(3.35,3.4)
\psset{dimen = m}
  \multido{\iA = 1+1}{3}{
    \multido{\iB = 1+1}{3}{
      \psframe(!\iA\space 1 sub \iB\space 1 sub)(\iA,\iB)}}
  \multido{\i = 1+1, \r = 0.5+1}{3}{\rput(\r,2.5){$\i$}}
  \multido{\i = 4+1, \r = 0.5+1}{3}{\rput(\r,1.5){$\i$}}
  \multido{\i = 7+1, \r = 0.5+1}{3}{\rput(\r,0.5){$\i$}}
  \multido{\i = 1+1, \r = 0.5+1}{3}{\rput(\r,-0.3){$\i$}}
  \multido{\i = 7+1, \r = 0.5+1}{3}{\rput(\r,3.3){$\i$}}
  \multido{\i = 1+3, \r = 2.5+-1}{3}{\rput(3.3,\r){$\i$}}
  \multido{\i = 3+3, \r = 2.5+-1}{3}{\rput(-0.3,\r){$\i$}}
\end{pspicture}

\end{document}

输出

答案2

仅使用基础 LaTeX,只是为了好玩:

\documentclass{article}
\newcommand{\nolines}[1]{\multicolumn{1}{c}{#1}}
\begin{document}
\begin{tabular}{c|c|c|c|c}
\nolines{} & \nolines{7} & \nolines{8} & \nolines{9} & \\ \cline{2-4}
3 & 1 & 2 & 3 & 1 \\ \cline{2-4}
6 & 4 & 5 & 6 & 4 \\ \cline{2-4}
9 & 7 & 8 & 9 & 7 \\ \cline{2-4}
\nolines{} & \nolines{1} & \nolines{2} & \nolines{3} &   
\end{tabular}
\end{document}

在此处输入图片描述

单元格的纵横比可以通过隐形规则或\arraystretch根据需要进行调整来调整。这留给读者练习。;-)

为了完整起见,由于还没有人使用过极其向后兼容的picture模式:

\documentclass{article}
\setlength{\unitlength}{0.5cm}
\newcommand{\placenum}[1]{\makebox(1,1){#1}}

\begin{document}
\begin{picture}(5,5)
  \multiput(1,1)(1,0){4}{\line(0,1){3}}
  \multiput(1,1)(0,1){4}{\line(1,0){3}}
  \multiput(1,0)(0,3){2}{\placenum{1}}
  \multiput(2,0)(0,3){2}{\placenum{2}}
  \multiput(3,0)(0,3){2}{\placenum{3}}
  \multiput(1,1)(0,3){2}{\placenum{7}}
  \multiput(2,1)(0,3){2}{\placenum{8}}
  \multiput(3,1)(0,3){2}{\placenum{9}}
  \multiput(1,2)(3,0){2}{\placenum{4}}
  \multiput(0,2)(3,0){2}{\placenum{6}}
  \put(2,2){\placenum{5}}
  \put(0,1){\placenum{9}}
  \put(0,3){\placenum{3}}
  \put(4,1){\placenum{7}}
  \put(4,3){\placenum{1}}
\end{picture}
\end{document}

在此处输入图片描述

\unitlength可以通过改变的值(在此示例中设置为)来调整网格的大小0.5cm

答案3

\documentclass[tikz]{standalone}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
    \matrix (m) [
  matrix of nodes,
  nodes={outer sep=0pt}]
  {
     & 7 & 8 & 9 &  \\
   3 & 1 & 2 & 3 & 1 \\
   6 & 4 & 5 & 6 & 4 \\
   9 & 7 & 8 & 9 & 7 \\
     & 1 & 2 & 3 &  \\
  };
  \foreach \x in {2,3,4,5}{
  \draw[line cap=round] (m-\x-2.north west) -- (m-\x-4.north east);
  \draw[line cap=round] (m-2-\x.north west) -- (m-4-\x.south west);
  }
\end{tikzpicture}

\end{document}

答案4

同样在纯 TeX 中:

在此处输入图片描述

\def\strut{\vrule width 0pt depth 5pt height 11pt}
\def\foo #1 #2 #3{\strut&&&#1&&#2&&#3\cr}
\def\bar{&&\multispan7\hrulefill\cr}
\def\baz #1 #2 #3 #4 #5{\strut&#1&\vrule&#2&\vrule&#3&\vrule&#4&\vrule&#5\cr}
$$\vbox{\offinterlineskip\halign{&#&\hbox to 16pt{\hss#\hss}\cr
\foo   7 8 9   \bar
\baz 3 1 2 3 1 \bar
\baz 6 4 5 6 4 \bar
\baz 9 7 8 9 7 \bar
\foo   1 2 3   
}}$$
\bye

相关内容