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.

Consider the following TikZ code:

\filldraw[fill=black!40!white,thick] (0,1.5) node[above left] {start} rectangle (3.2,2) node[below right] {end} node [midway] {cp} ;

It produces a rectangle at the required co-ordinates, with text cp inside the rectangle. I require text labels to the left and the right of the rectangle. I have attempted to produce these with the text attached to the above left and below right nodes. However, the end label is positioned slightly lower than the start label. How can I fix this, or, is there a better/easier way to position text next to rectangles. I have looked through the tutorials in the first section of the 700+ TikZ manual.

Edit: Additional information in response to percusses's answer below:

\filldraw[fill=black!40!white,thick] (0,2.5) rectangle (3.2, 3) node [midway] {cp} ;
\node[fill=black!40!white,thick,draw,minimum height=0.5cm,minimum width=3.2cm,label=0:end,label=west:start] at (0,1.5) {cp};

Note the different x positions. Your code (the second line above) places the entire structure such that start is positioned at position 0. For some reason, the structure produced by my code is shifted to the right so that it starts in the middle of the rectangle relative to yours.

share|improve this question

1 Answer 1

up vote 8 down vote accepted

The manual is indeed long but it's very well structured so you can skip many irrelevant parts. For this specific example, you can use a node and put labels around it.

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\node[fill=black!40!white,
      thick,
      draw,
      minimum height=0.5cm,
      minimum width=3.2cm,
      label=0:end,%< -- This uses angle zero degree on the border for the location
      label=west:start %< -- This uses an anchor of the node for the location
] at (0,1.5) {cp};
\end{tikzpicture}
\end{document}

enter image description here

share|improve this answer
    
Thanks for the answer. I need to produce several such rectangles (above each other vertically, but at different starting x positions and of different x sizes) all to scale and positioned according to the x value. Your solution places the rectangle at a different x position to mine, so I'm figuring that out first. I think in your example, the labels are counting towards the width and/or the positioning of the entire structure. –  SabreWolfy Oct 4 '12 at 16:26
    
I've added a note to my question with an example illustrating what I mean about the positioning. –  SabreWolfy Oct 4 '12 at 17:59
    
@SabreWolfy Add anchor=south west option to the node properties which would place the left lower corner at the given coordinate. –  percusse Oct 4 '12 at 18:10
    
This solved it. –  SabreWolfy Oct 4 '12 at 18:20
1  
@SabreWolfy You can use text width=3.2cm,align=center to let the text have line breaks such that you can spot them. –  percusse Oct 6 '12 at 14:27

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.