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 need to place information below and to the right of a grid. I was not able to rotate the label text of a node. I ended up using \rotatebox, but is there a pure TikZ solution?

\documentclass{scrartcl}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
  \draw[step=1cm] (1,1) grid (4,4);
  \node[label=below:\rotatebox{-90}{a long text}] at (2.5,1) {};
  \node[rotate=-90,label=below:rotate] at (1.5,0.5) {A};
  \node[label=right:a long text] at (4,3.5) {};
\end{tikzpicture}
\end{document}

enter image description here

share|improve this question

2 Answers 2

up vote 7 down vote accepted

It is perfectly possible to use label in this situation. You just need to rearrange a bit the options and to fiddle around with the placement of the label:

\documentclass{scrartcl}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
  \draw[step=1cm] (1,1) grid (4,4);
  \node[label={[label distance=0.5cm,text depth=-1ex,rotate=-90]right:a long text}] at (2.5,1) {};
\end{tikzpicture}
\end{document}

As you can see the idea is to introduce the rotate option as an actual option to the label, not the node. Also, note the options right instead of below and label distance, which are necessary because the system will rotate the label relative to the node. Also, at this point the text might appear a bit off center, so a small hack is needed, to modify the vertical (rotated by -90 degrees at the same time!) position of the label text: text depth=-1ex.

enter image description here

share|improve this answer
    
In other presented solutions, the text box itself is centered. In your solution, the baseline is centered, which looks quite strange. Any fix? –  Josef May 22 '13 at 14:37
    
@Josef: Voilà! :) –  Count Zero May 22 '13 at 15:26
    
(I’d use xshift or yshift for correcting the label placement instead of fiddeling with the text depth, especially if you want to use (drawing/filling/referencing) the label later.) The rotate key rotates around the anchor of the label which is automatically set to west as you use right (or east) for the label’s direction. Instead of labeling an empty node, one should label a coordinate, an additional label distance can be provided: \path (2.5,1) coordinate [label={[rotate=-90]right:{a long text}}]; –  Qrrbrbirlbel May 22 '13 at 19:49
1  
Another solution would be to overwrite the automatically computed anchor (e.g. label={[rotate=-90,anchor=west]below:a long text}, note the change of direction), though in this case, I probably would prefer “real” nodes for their flexibility. –  Qrrbrbirlbel May 22 '13 at 19:57
    
@Qrrbrbirlbel: Certainly, using a separate node is the most straightforward and easily customizable solution. I just took up the challenge to find out whether label is workable. Setting the anchor of the label was the missing piece of the puzzle. Thanks for giving it to me! :) –  Count Zero May 22 '13 at 22:14

You might prefer to use text nodes instead of labels. If you set anchor=west, the nodes will be 'left-aligned', and you can rotate the nodes as per usual.

Code

\documentclass{scrartcl}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
  \draw[step=1cm] (1,1) grid (4,4);
  \tikzset{anchor=west}
  \node at (4,1.5) {a long text};
  \node at (4,2.5) {a long text};
  \node at (4,3.5) {a long text};
  \node[rotate=-90] at (1.5,1) {a long text};
  \node[rotate=-90] at (2.5,1) {a long text};
  \node[rotate=-90] at (3.5,1) {a long text};
\end{tikzpicture}
\end{document}

Output

enter image description here

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.