我使用该\multiput
命令创建了一个例如 4 x 4 的表格:
\documentclass[a4paper,12pt]{report}
\usepackage{graphicx} % inserting images
\begin{document}
\setlength{\unitlength}{2mm}
\begin{picture}(4,4)
\multiput(25,0)(5,0){5}%
{\line(0,1){20}}
\multiput(25,0)(0,5){5}%
{\line(1,0){20}}
\end{picture}
\end{document}
我怎样才能将文字放入每个小方块中?
答案1
如果你真的在画一幅画(虽然你的例子看起来像一张表格),那么最好使用一个现代绘图包,例如tikz
:
代码:
\documentclass{report}
\usepackage{tikz}
\newcommand*{\NodeWidth}{1.0cm}%
\newcommand*{\NodeHeight}{1.0cm}%
\tikzset{My Node Style/.style={rectangle, ultra thick, minimum width=\NodeWidth, minimum height=\NodeHeight}}
\newcommand*{\FourNodes}[6][draw=black]{%
\node [My Node Style, #1] at (0,#2*\NodeHeight) {#3\strut};
\node [My Node Style, #1] at (\NodeWidth,#2*\NodeHeight) {#4\strut};
\node [My Node Style, #1] at (2*\NodeWidth,#2*\NodeHeight) {#5\strut};
\node [My Node Style, #1] at (3*\NodeWidth,#2*\NodeHeight) {#6\strut};
}%
\begin{document}
\begin{tikzpicture}
\FourNodes{0.0cm}{a}{b}{c}{d}
\FourNodes{-1}{x}{y}{z}{w}
\FourNodes{-2}{1}{2}{3}{4}
\FourNodes{-3}{r}{g}{t}{u}
\end{tikzpicture}
\end{document}
答案2
这个使用matrix
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\tikzset{
table/.style={
matrix of nodes,
row sep=-\pgflinewidth,
column sep=-\pgflinewidth,
nodes={rectangle,minimum size = 1cm,align=center,inner sep=0pt},
text depth=1.25ex,
text height=2.5ex,
nodes in empty cells
},
}
\begin{document}
\begin{tikzpicture}
% the matrix entries
\matrix (mat) [table]
{
a & b & c & d \\
x & y & z & w \\
1 & 2 & 3 & 4 \\
r & g & t & u \\
};
\foreach \x in {1,...,4}
{
\draw[ultra thick]
([xshift=-.5\pgflinewidth]mat-\x-1.south west) --
([xshift=.5\pgflinewidth]mat-\x-4.south east);
}
\draw[ultra thick]
([xshift=-.5\pgflinewidth]mat-1-1.north west) --
([xshift=-.5\pgflinewidth]mat-1-4.north east);
\foreach \x in {1,...,4}
{
\draw[ultra thick]
([yshift=.5\pgflinewidth]mat-1-\x.north east) --
([yshift=.5\pgflinewidth]mat-4-\x.south east);
}
\draw[ultra thick]
([yshift=.5\pgflinewidth]mat-1-1.north west) --
([yshift=.5\pgflinewidth]mat-4-1.south west);
\end{tikzpicture}
\end{document}
更简单的版本
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\tikzset{
table/.style={
matrix of nodes,
row sep=-4\pgflinewidth, %% adjust
column sep=-4\pgflinewidth, %% adjust
nodes={draw, ultra thick,rectangle,minimum size = 1cm,align=center,inner sep=0pt},
text depth=1.25ex,
text height=2.5ex,
nodes in empty cells
},
}
\begin{document}
\begin{tikzpicture}
% the matrix entries
\matrix (mat) [table]
{
a & b & c & d \\
x & y & z & w \\
1 & 2 & 3 & 4 \\
r & g & t & u \\
};
\end{tikzpicture}
\end{document}