I would like to draw a 3 x 3 box with LaTeX as seen below in the picture. Is there an easy way to do that? Maybe there is a special command for that?
6 Answers
A PSTricks solution:
\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}
With base LaTeX only, just for fun:
\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}
The aspect ratio of the cells can be adjusted with invisible rules or by adjusting \arraystretch
if needed. This is left as an exercise for the reader. ;-)
For completeness, since no one has used the extremely backward-compatible picture
mode yet:
\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}
The size of the grid can be adjusted by changing the value of \unitlength
(set to 0.5cm
in this example).
-
\def\arraystretch{1.3}
works nicely for aspect ratio fix. Dec 29, 2014 at 21:31
\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}
Without PSTricks just for fun
\documentclass[tikz,12pt]{standalone}
\begin{document}
\begin{tikzpicture}[]
\draw (1,-4) grid (4,-1);
\foreach \y in {0,1,2}{\foreach \x in {0,1,2}{\node at(\x+1.5,-\y-1.5){$\the\numexpr3*\y+\x+1\relax$};}}
\foreach \upper[count=\x from 0] in {7,8,9}{\node at (\x+1.5,-.5){$\upper$};}
\foreach \lower[count=\x from 0] in {1,2,3}{\node at (\x+1.5,-4.5){$\lower$};}
\foreach \left[count=\y from 0] in {3,6,9}{\node at (.5,-\y-1.5){$\left$};}
\foreach \right[count=\y from 0] in {1,4,7}{\node at (4.5,-\y-1.5){$\right$};}
\path (0,0) grid (5,-5);
\end{tikzpicture}
\end{document}
With PSTricks just for fun
\documentclass[pstricks,12pt]{standalone}
\usepackage{pgffor}
\begin{document}
\begin{pspicture}[dimen=m](5,-5)
\multips(1,-1)(1,0){3}{\multips(0,-1)(0,-1){3}{\psframe(1,1)}}
\foreach \y in {0,1,2}{\foreach \x in {0,1,2}{\rput(!\x\space 1.5 add \y\space neg 1.5 sub){$\the\numexpr3*\y+\x+1\relax$}}}
\foreach \upper[count=\x from 0] in {7,8,9}{\rput(!\x\space 1.5 add -.5){$\upper$}}
\foreach \lower[count=\x from 0] in {1,2,3}{\rput(!\x\space 1.5 add -4.5){$\lower$}}
\foreach \left[count=\y from 0] in {3,6,9}{\rput(!.5 \y\space neg 1.5 sub){$\left$}}
\foreach \right[count=\y from 0] in {1,4,7}{\rput(!4.5 \y\space neg 1.5 sub){$\right$}}
\end{pspicture}
\end{document}
Also in plain TeX:
\def\strut{\vrule width 0pt depth 5pt height 11pt}
\def\foo #1 #2 #3{\strut&&&&\cr}
\def\bar{&&\multispan7\hrulefill\cr}
\def\baz #1 #2 #3 #4 #5{\strut&\vrule&\vrule&\vrule&\vrule\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
Tikz solution:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\foreach \x in{1,2,3}{
\pgfmathsetmacro\result{\x + 3}
\node (rect) at (\x,2) [draw,thick,minimum width=1cm,minimum height=1cm] {\x};
\node (rect) at (\x,1) [draw,thick,minimum width=1cm,minimum height=1cm] {\pgfmathprintnumber{\result}};
\pgfmathsetmacro\result{\x + 6}
\node (rect) at (\x,0) [draw,thick,minimum width=1cm,minimum height=1cm] {\pgfmathprintnumber{\result}};
}
\node (rect) at (0,0) [draw,minimum width=0.9cm,minimum height=0.9cm,white] {\color{black}9};
\node (rect) at (0,1) [draw,minimum width=0.9cm,minimum height=0.9cm,white] {\color{black}6};
\node (rect) at (0,2) [draw,minimum width=0.9cm,minimum height=0.9cm,white] {\color{black}3};
\node (rect) at (4,0) [draw,minimum width=0.9cm,minimum height=0.9cm,white] {\color{black}7};
\node (rect) at (4,1) [draw,minimum width=0.9cm,minimum height=0.9cm,white] {\color{black}4};
\node (rect) at (4,2) [draw,minimum width=0.9cm,minimum height=0.9cm,white] {\color{black}1};
\node (rect) at (1,3) [draw,minimum width=0.9cm,minimum height=0.9cm,white] {\color{black}7};
\node (rect) at (2,3) [draw,minimum width=0.9cm,minimum height=0.9cm,white] {\color{black}8};
\node (rect) at (3,3) [draw,minimum width=0.9cm,minimum height=0.9cm,white] {\color{black}9};
\node (rect) at (1,-1) [draw,minimum width=0.9cm,minimum height=0.9cm,white] {\color{black}1};
\node (rect) at (2,-1) [draw,minimum width=0.9cm,minimum height=0.9cm,white] {\color{black}2};
\node (rect) at (3,-1) [draw,minimum width=0.9cm,minimum height=0.9cm,white] {\color{black}3};
\end{tikzpicture}
\end{document}
\begin{tabular}{|c|c|c|}\hline\\ 1 & 2 & 3 \\ \hline 1 & 2 & 3 \\ \hline 1 & 2 & 3 \\ \hline \end{tabular}