5

Why is the following MWE not placing the label along the correct angle?

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}

\begin{tikzpicture}[every path/.style={line width=1pt}]

  \coordinate (X) at (2,4) {};

  \foreach \x in { 100,107,114}    
    { 
      \node (tmp\x) at ($(X)+(\x:3)$) {};      
      \draw[orange!30,fill=orange!30] (X) -- (tmp\x);
    }

  \node[circle,inner sep=2pt,draw,label=107:X] at (X) {};
  \node[inner sep=0pt,fill=none] (label/A) at ($(X)+(107:1.0)$) {Y};

\end{tikzpicture}

\end{document}

enter image description here

The Y has been placed where I expect and want the X to be.

A.Ellett
  • 50,533

1 Answers1

3

The label nodes are only placed in 45° steps, similar to nodes placed using the auto option.

You can disable this and get degree-accuracy by redefining the internal macro that is responsible for the snapping behaviour.

The following code (taken from https://tex.stackexchange.com/a/85669/2552) does that:

\makeatletter
\def\tikz@auto@anchor{%
    \pgfmathtruncatemacro\angle{atan2(\pgf@x,\pgf@y)-90}
    \edef\tikz@anchor{\angle}%
}

\def\tikz@auto@anchor@prime{%
    \pgfmathtruncatemacro\angle{atan2(\pgf@x,\pgf@y)+90}
    \edef\tikz@anchor{\angle}%
}
\makeatother


\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}



\makeatletter
\def\tikz@auto@anchor{%
    \pgfmathtruncatemacro\angle{atan2(\pgf@x,\pgf@y)-90}
    \edef\tikz@anchor{\angle}%
}

\def\tikz@auto@anchor@prime{%
    \pgfmathtruncatemacro\angle{atan2(\pgf@x,\pgf@y)+90}
    \edef\tikz@anchor{\angle}%
}
\makeatother

\begin{tikzpicture}[every path/.style={line width=1pt}]

  \coordinate (X) at (2,4) {};

  \foreach \x in { 100,107,114}    
    { 
      \node (tmp\x) at ($(X)+(\x:3)$) {};      
      \draw[orange!30,fill=orange!30] (X) -- (tmp\x);
    }

  \node[circle,inner sep=2pt,draw,label=107:X] at (X) {};
  \node[inner sep=0pt,fill=none] (label/A) at ($(X)+(107:1.0)$) {Y};

\end{tikzpicture}

\end{document}
Jake
  • 232,450
  • So does this code change just the way labels and pins work? Or does it also change auto? It seems to me that it's probably best to steer clear of using label and just create and place my own nodes for labeling purposes. Nevertheless, thank you for the explanation of what's happening. – A.Ellett Sep 18 '13 at 18:27