Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

The code

\begin{tikzpicture}
\draw[step=0.5cm,color=gray] (-1,-1) grid (1,1);
\end{tikzpicture}

gives me a nice finite 4x4 grid. Now I want to center one character in each cell, how to do this?

By the way, how can I specify the absolute position of my picture and how to stretch it in one direction (i.e. that it becomes a proper rectangle (not a square)).

share|improve this question
add comment

3 Answers

up vote 8 down vote accepted

You can use \nodes to place text everywhere you want. The are by default centered at the given coordinate which can be changed using the anchor and other options.

\documentclass{article}

\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
\draw[step=0.5cm,color=gray] (-1,-1) grid (1,1);
\node at (-0.75,+0.75) {A};
\node at (-0.25,+0.75) {B};
\node at (+0.25,+0.75) {C};
\node at (+0.75,+0.75) {D};
\node at (-0.75,+0.25) {E};
\node at (-0.25,+0.25) {F};
\node at (+0.25,+0.25) {G};
\node at (+0.75,+0.25) {H};
% ...
\node at (+0.75,-0.75) {Q};
\end{tikzpicture}
\end{document}

Result


Or using a loop:

\documentclass{standalone}

\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
\draw[step=0.5cm,color=gray] (-1,-1) grid (1,1);

\newcounter{mycount}
\setcounter{mycount}{`A}
\foreach \y in {+0.75,+0.25,-0.25,-0.75}
  \foreach \x in {-0.75,-0.25,0.25,0.75}
    \node at (\x,\y) {\char\value{mycount}\addtocounter{mycount}{1}};
\end{tikzpicture}%

% Or
\begin{tikzpicture}
\draw[step=0.5cm,color=gray] (-1,-1) grid (1,1);
\foreach \x/\y/\m in {+0.75/+0.75/A,-0.75/-0.75/X} % etc
    \node at (\x,\y) {\m};
\end{tikzpicture}%

\end{document}

Loop result Loop result 2


You can scale the image in the X or Y direction using xscale=<number> and yscale=<number>, respectively. Both are scaled with scale=<number>.

If you mean with "absolute position" a fixed position on a page you can do this by drawing it relative to the special current page node (needs the remember picture,overlay option on the picture). If you give more specific information about this I can make an example.

share|improve this answer
    
Ok, thanks! How to control the position, how to stretch it? –  student Mar 7 '11 at 14:49
    
@user4011: I added this. What exactly do you mean with "absolute position"? –  Martin Scharrer Mar 7 '11 at 14:51
1  
Is it possible to rewrite the code using looping commands? –  xport Mar 7 '11 at 15:03
2  
@TeX: Yes, it is. –  Martin Scharrer Mar 7 '11 at 15:40
    
Fantastic answer! –  Jimi Oke Oct 8 '11 at 20:56
add comment

I would place the text using a matrix of nodes:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}
\begin{tikzpicture}
\draw[step=0.5cm,color=gray] (-1,-1) grid (1,1);
\matrix[matrix of nodes,nodes={inner sep=0pt,text width=.5cm,align=center,minimum height=.5cm}]{
A & B & C & D \\
E & F &  & H \\
I & J & K & L \\
M & N & O & P\\};
\end{tikzpicture}
\end{document}

matrix

To get rectangular nodes, you can adjust xstep and ystep separately (and text width and minimum height accordingly):

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}
\begin{tikzpicture}
\draw[xstep=0.75cm,ystep=0.5,color=gray] (0,0) grid (3,2);
\matrix[matrix of nodes,
inner sep=0pt,
anchor=south west,
nodes={inner sep=0pt,text width=.75cm,align=center,minimum height=.5cm}
]{
A & B & C & D \\
E & F &  & H \\
I & J & K & L \\
M & N & O & P\\
};
\end{tikzpicture}
\end{document}

matrix rectangular

share|improve this answer
add comment

Using the grid shape from my answer to “Drawing a tree of grids”, you can access the centers of the squares in the grid:

% Code assumes that the grid shape has been added (e.g. in the preamble).
\begin{tikzpicture}
    \node[  draw,
            grid,
            grid lines=3,
            minimum width=2cm,
            minimum height=2cm
         ] (letters) {};
    \node at (letters.center 1 4) {A};
    \node at (letters.center 2 4) {B};
    \node at (letters.center 3 4) {C};
    \node at (letters.center 4 4) {D};
    \node at (letters.center 1 3) {E};
    \node at (letters.center 2 3) {F};
    \node at (letters.center 3 3) {G};
    \node at (letters.center 4 3) {H};
    \node at (letters.center 1 2) {I};
    \node at (letters.center 2 2) {J};
    \node at (letters.center 3 2) {K};
    \node at (letters.center 4 2) {L};
    \node at (letters.center 1 1) {M};
    \node at (letters.center 2 1) {N};
    \node at (letters.center 3 1) {O};
    \node at (letters.center 4 1) {P};
\end{tikzpicture}

result

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.