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 trying to draw a diagram and connect the nodes. Since i want to Place the labels above the lines I use every node/.style={above=.4ex, anchor=base}. However sometimes I draw coordinates at corners, so that I can connect multiple lines. However, these coordinates are nodes and thus are shifted by the above command.

Is there a style that affects only nodes (not coordinates) or only coordinates (not nodes)?

update Along the discussion to the awnser here is an example where I get by with setting above to 0 instead of a negative value

\begin{tikzpicture}[demo/.style={every node/.style={above=3 pt}}, every coordinate node/.style={above=0}]
   \draw[demo] (0, 0) -- node{Label} coordinate(c1) (1,0);
   \node at (c1) [red]{x};
\end{tikzpicture}

samble with overwriting above

FULL CODE: (unnecessary headers included), see it in action at writelatex

\documentclass{article}
\usepackage{tikz}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{bm}
\usepackage[margin=0cm,nohead]{geometry}
\usepackage[active,tightpage]{preview}

\usetikzlibrary{calc}
\usetikzlibrary{positioning}
\usetikzlibrary{shapes}
\usetikzlibrary{arrows}
\usetikzlibrary{circuits}
\usetikzlibrary{patterns}
\usetikzlibrary{shadows}

\PreviewEnvironment{tikzpicture}
\listfiles
\begin{document}
\begin{tikzpicture}[demo/.style={every node/.style={above=3 pt}}, every coordinate node/.style={above=0}]
   \draw[demo] (0, 0) -- node{Label} coordinate(c1) (1,0);
   \node at (c1) [red]{x};
\end{tikzpicture}
\end{document}
share|improve this question
    
Thank you for the extra example. However, when I compile it I don't get the output shown, both the Label and x are raised. THis is even more clear when you replace 3pt by 3cm. So additive behaviour is still present in this case. For reference my tikz verison is "2010/10/13 v2.10 (rcs-revision 1.76)" –  Andrew Swann May 15 '13 at 12:56
    
@AndrewSwann: please check out writelatex.com/183836pjczpg or try the full code I posted in the awnser (I was to lazy to strip down all the headers, but it will compile fine on writelatex). P.S.: my local tikz version is tikz.sty 2010/10/13 v2.10 (rcs-revision 1.76) as well. –  ted May 16 '13 at 9:00
1  
Thanks for the full code. It is the use of the positioning library that it is changing the behaviour to being non-additive. –  Andrew Swann May 16 '13 at 14:24
    
@AndrewSwann: you found that out by try and error? DO you know why it does so/ how it changes the behaviour of above? –  ted May 16 '13 at 21:21
1  
It was by experiment. That this affects the behaviour of above in this way, seems to be in direct contradiction with a statement in the pgfmanual on page 185. Anyway I have added an extra example to my answer to show this behaviour. –  Andrew Swann May 17 '13 at 9:10
add comment

2 Answers

up vote 5 down vote accepted

coordinates are shapes and for each shape there is an every shape node that can be set. For example,

Sample output

\documentclass{article}

\usepackage{tikz}

\begin{document}

\begin{tikzpicture}[every node/.style={red},every coordinate node/.style={above=1cm}]
  \coordinate (A) at (0,0);
  \draw (0,0) node {\( O \)}-- (2,2) node {\( X \)};
  \draw[blue] (A) node {\( A \)} -- (1,1);
\end{tikzpicture}

\end{document}

Here, in the opposite to you use case, coordinate nodes are shifted, whereas other nodes are left in place.

In your use case, you can cancel the above on the coordinate by shifting it back:

\begin{tikzpicture}[every node/.style={above=2ex,anchor=base},every coordinate node/.style={above=-2ex,anchor=base}]
  \coordinate (A) at (0,0);
  \draw (0,0) node {\( O \)}-- (2,2) node {\( X \)};
  \draw[very thick,red] (A) -- (1,1);
\end{tikzpicture}

Sample use case

EDIT As Andrew Stacey notes this additive behave of the above command is unexpected. However, it is not often we see it. In fact if you use two above's in the same node you do indeed see the additive effect:

Additive sample

\begin{tikzpicture}
  \coordinate (A) at (0,0);
  \draw[red] (A) -- (1,0);
  \draw (A) node[anchor=base,above=2ex,above=1ex] {\( x \)};
  \draw[blue] (A) node[anchor=base,above=3ex] {\( O \)};
\end{tikzpicture}

Here we place O with its baseline 3ex above the origin, and place x at with baseline at above=2ex,above=1ex from the origin and obtain the same positioning.

FURTHER EDIT If the tikz library positioning is loaded, then the additive behaviour of above no longer occurs. The last above directive is the one used, and an above for coordinate will override an above for every node:

Sample positioning output

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}

\begin{tikzpicture}[every node/.style={above=2ex,anchor=base},every coordinate node/.style={above=-2ex,anchor=base}]
  \coordinate (A) at (0,0);
  \draw (0,0) node {\( O \)}-- (2,2) node {\( X \)};
  \draw[very thick,red] (A) -- (1,1);
\end{tikzpicture}

\vspace{2cm}

\begin{tikzpicture}
  \coordinate (A) at (0,0);
  \draw[red] (A) -- (1,0);
  \draw (A) node[anchor=base,above=2ex,above=1ex] {\( x \)};
  \draw[red] (A) node[anchor=base,above=10cm,above=1ex] {\( \quad x \)};
  \draw[blue] (A) node[anchor=base,above=3ex] {\( O \)};
\end{tikzpicture}

\end{document}
share|improve this answer
    
Is above really cumulative? –  Loop Space May 14 '13 at 18:06
    
@AndrewStacey I only have empirical evidence: putting the coordinate style to above=0pt in the second example gives a red line starting higher than O. An explanation of why it behaves like this would be nice to have. –  Andrew Swann May 14 '13 at 18:39
    
@AndrewStacey Double above example now added showing additivity. –  Andrew Swann May 14 '13 at 19:07
3  
The above and below keys (and all the others) not only set the anchor, but when a length is given use the basic layer transformations \pgftransformxshift and \pgftransformyshift, so effects will be additive. Thus, above=2ex,above=1ex result in \pgftransformyshift{2ex} followed by \pgftransformyshift{1ex}. The transformations are applied just before the node is typeset. –  Mark Wibrow May 15 '13 at 5:50
    
@MarkWibrow : In my case I miss to see the additivity: I have a style umlTransition/.style={-angle 60, every node/.style={above=.4 ex, anchor=base}} and apply this to draw commands, for the coordinates I fixed the application by adding [above=0] to every coordinate, e.g.: \draw[umlTransition, umlShortened] (joinDiscard) |- coordinate[above=0] (discardCorner) (fetchJoinCorner);. For some reason above seems to be none additive in this case (it fixed my issue). Do you have an explanation (I assume it has something todo with how tikz manages styles and dirket option, but am quite unsure) –  ted May 15 '13 at 7:03
show 4 more comments

I want to propose a few other possibilities.

  1. Use labels. That’s why they exist.
  2. If your “labels” only use the rectangle shape (the default one) and the rectangle shape is only used for label, you can use the every rectangle node style.
  3. The last possibility introduces the ability to use a every not coordinate node that gets applied when the node is not a coordinate. The shape name has to be specified by every not.

Code

\documentclass[tikz,convert=false]{standalone}
\usetikzlibrary{positioning}
\makeatletter
\tikzset{
  every not/.code=%
    \expandafter\def\expandafter\tikz@node@begin@hook\expandafter{%
      \tikz@node@begin@hook
      \def\pgf@tempa{#1}%
      \ifx\tikz@shape\pgf@tempa\else
        \tikzset{every not \pgf@tempa\space node/.try}%
      \fi
    }
}
\makeatother
\begin{document}
\begin{tikzpicture}[demo/.style={every label/.style={label distance=3pt, label position=above}}]
   \draw[demo] (0, 0) -- coordinate[label=Label] (c1) (1,0);
   \node at (c1) [red]{x};
\end{tikzpicture}

\begin{tikzpicture}[demo/.style={every rectangle node/.style={above=3pt}}]
   \draw[demo] (0, 0) -- node {Label} coordinate (c1) (1,0);
   \node at (c1) [red]{x};
\end{tikzpicture}

\begin{tikzpicture}[demo/.style={every not=coordinate, every not coordinate node/.style={above=3pt}}]
   \draw[demo] (0, 0) -- node {Label} coordinate (c1) (1,0);
   \node at (c1) [red]{x};
\end{tikzpicture}
\end{document}

Output

enter image description here

share|improve this answer
    
+1 for pointing me to labels –  ted May 16 '13 at 21:20
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.