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.

I am new to using tikz. I have the following grid:

enter image description here

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usetikzlibrary{patterns}
\usetikzlibrary{shapes}
\usetikzlibrary{plotmarks}

\newcommand{\nvar}[2]{
    \newlength{#1}
    \setlength{#1}{#2}
}

\nvar{\dx}{1cm}

\begin{document}
\begin{tikzpicture}
    % fill outside cells
    \fill [orange!40] (-1,-1) rectangle ( 3, 0); 
    \fill [orange!40] (-1, 2) rectangle ( 3, 3); 
    \fill [orange!40] (-1, 0) rectangle ( 0, 2); 
    \fill [orange!40] ( 2, 0) rectangle ( 3, 2); 
% pressure cells
\draw [semithick][help lines, step=\dx] (-1,-1) grid ( 3, 3);
\draw [thin,dashed][help lines, step=.5*\dx] (-1,-1) grid ( 3, 3); 
\draw [red,thick] ( 0, 0) rectangle ( 2, 2);
\draw [red,thick] (-1,-1) rectangle ( 3, 3);

\end{tikzpicture}
\end{document}

I want to annotate the edges of my grid the to generate a picture that looks like this in tikz: enter image description here

share|improve this question
    
Welcome to TeX.SX! –  Adam Liter Jul 29 at 23:19

3 Answers 3

Insert the code below at the end of the tikzpicture environment (just before \end{tikzpicture}).

\foreach \a in {.5,1.5}{
 \foreach \b in {0,1,2}{
   \node at (\a,\b){$\otimes$};
   \node[green] at (\b,\a){$\times$};
 }
}

enter image description here

share|improve this answer
    
(+1) Huh. Never occurred to me to do the green second... –  cfr Jul 29 at 23:30
    
@cfr, do you see any advantage on that? –  Sigur Jul 29 at 23:34
1  
No. Sorry. It was a joke. I didn't see your answer before I posted mine and then realised it was basically identical except for the ordering. I should remember I have a twisted sense of humour. Also, I have left my answer because I also showed a way to make the other code more concise. But I'll delete if you wish since you posted first. –  cfr Jul 29 at 23:38
1  
@cfr, no! Don't worry. You noticed about the even/odd rule. It was nice. –  Sigur Jul 29 at 23:39

Because why not ? :P

\documentclass[tikz]{standalone}
\usetikzlibrary{plotmarks}
\begin{document}
\begin{tikzpicture}
\def\dx{1cm}\def\mylista{}\def\mylistb{}
\foreach \x in {0,1,2}{
  \foreach \y in {.5,1.5}{
     \xdef\mylista{\mylista (\x,\y)}\xdef\mylistb{\mylistb (\y,\x)}
  }
}
\draw[thick,red,double distance between line centers=\dx,double=orange!40] (-0.5,-0.5) rectangle (2.5,2.5);
\draw [semithick][help lines, step=\dx] (-1,-1) grid ( 3, 3);
\draw [thin,dashed][help lines, step=.5*\dx] (-1,-1) grid ( 3, 3); 
\draw --plot [only marks,mark=otimes] coordinates \mylistb;
\draw --plot [only marks,mark=x,mark options={color=green}] coordinates \mylista;
\end{tikzpicture}
\end{document}

enter image description here

share|improve this answer
    
Nice! The marks now have the same line width as the rectangles. I was trying to do this drawing the cross by hand. –  Sigur Jul 30 at 0:18
    
@Sigur Oooh, that's a good one. I wonder how to automate that. I better ask the wizards later about the glyph widths to match the characters with line width. –  percusse Jul 30 at 0:22
    
(+1) I like the double distance.... –  cfr Jul 30 at 0:53

Something like this?

\documentclass[tikz]{standalone}

\newcommand{\nvar}[2]{
    \newlength{#1}
    \setlength{#1}{#2}
}

\nvar{\dx}{1cm}

\begin{document}
\begin{tikzpicture}
    \path [draw=red, thick, fill=orange!40, even odd rule,] (-1,-1) rectangle (3,3) (0,0) rectangle (2,2);
    \draw [semithick][help lines, step=\dx] (-1,-1) grid ( 3, 3);
    \draw [thin,dashed][help lines, step=.5*\dx] (-1,-1) grid ( 3, 3);
    \foreach \i in {0,...,2}
      \foreach \j in {.5, 1.5}
      {
        \node [green] at (\i,\j) {$\times$};
        \node [black!80] at (\j,\i) {$\otimes$};
      }
\end{tikzpicture}
\end{document}

The initial command replaces the 4 rectangles and the drawing of the borders by using the even odd rule with two rectangles. (I just think this is a neat trick and recently discovered it works in tikz and not just metapost. ;) So I couldn't resist...)

To annotate, I use two loops over variables, nesting them and using each value for both an iteration over the x coordinate and an iteration over the y coordinate. Basically, tikz will run through these loops with all possible pairs of \i/\j which saves typing.

Annotated diagram

share|improve this answer

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.