I was looking for a possibility to adjust the anchor of the text of a pin. Tikz does not allow this out of the box.
This question was already asked on the mailing list pgf-users@lists.sourceforge.net
, Sept. 2009 without any reply till today or feature addition to pgf/tikz
:
\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}[
every pin/.style={anchor=west},
every pin edge/.style={anchor=west}]
\node [pin=60:test] at (0,1) {};
\node [pin={[anchor=west]60:test}] at (0,0) {};
\end{tikzpicture}
\end{document}
There is also a question with a suggested workaround here on tex.stackexchange.com by defining an aligned pin
style consisting of another node positioned at the end of the pin using \tikzlastnode
:
\tikzset{
aligned pin/.style args={[#1,#2]#3:#4}{
pin={[inner sep=0pt,%
pin distance=#2,% new option, default = 3ex
label={[append after command={%
node[inner sep=0pt,%
xshift=-0.1cm,% modified, default = 0
at=(\tikzlastnode.#3),%
anchor=#1,%
]{#4}%
}%
]center:{}}%
]#3:{}}%
}
}
In this code, I have added a second unnamed option for pin distance
within the square brackets.
MWE using the workaround showing the desired result:
\documentclass[tikz]{standalone}
\tikzset{
aligned pin/.style args={[#1,#2]#3:#4}{
pin={[inner sep=0pt,%
pin distance=#2,% new option, default = 3ex
label={[append after command={%
node[%
inner sep=0pt,%
xshift=-0.1cm,% modified, default = 0
at=(\tikzlastnode.#3),%
anchor=#1,%
]{#4}%
}%
]center:{}}%
]#3:{}}%
}
}
\begin{document}
\tikz[pin distance=15mm, every pin/.style={fill=blue,text=white}]
\node [circle,draw,
aligned pin={[west,3ex]0:X-Value},%
aligned pin={[west,3ex]45:Y-Value},%
aligned pin={[west,3ex]90:Z-Value}] {my circle};
\end{document}
I would like to add further options such as pin edge
to adjust the style of the edge. Another addition would be xshift
to finetune the position of the text (and yshift
).
Is it possible to change the workaround to allow named options with default values if not specified?
Usage could look like this (without using necessarily all options):
node[aligned pin={[pin anchor=west,pin distance=1cm,pin edge={thick}]90:{txt}}]{};
If someone is interested in the implementation of the pin feature in tikz see pgf/base/tex/generic/frontendlayer/tikz.code.tex and do a textsearch for pin
.
Update after answer by Qrrbrbirlbel:
To achieve almost the same result as my MWE use following code
% use preamble from Qrrbrbirlbel answer
\begin{document}
\begin{tikzpicture}[every pin/.append style=draw, every pin edge/.style={thick}]
\node[c] [
pin={[pin anchor=south west,pin edge pin anchor=south west,pin distance=6ex]90:Z-Value},
pin={[pin anchor=west,pin edge pin anchor=west,pin distance=6ex]45:Y-Value},
pin={[pin anchor=west,pin edge pin anchor=west,pin distance=6ex]0:X-Value}
] at (0,0) {};
\end{tikzpicture}
\end{document}
However, I would like to finetune the text positioning using inner sep=0
and a slight shift:
\newlength{\xshiftlength}
\setlength{\xshiftlength}{0.5\widthof{Z}}
Following works:
\tikzset{
pin inner xsep/.style={tikz@pin@post/.append style={inner xsep=#1}}
}
Apparently xshift
cannot be added in the same way. It shifts more than only the text. How could a pin text xshift
be added?
my label
asaligned pin
) or do not give me the desired result (accepted answer). The accepted answer looked promising but my MWE with\tikzset{pin anchor/.style={tikz@pin@post/.append style={anchor=#1}}}
did not put the anchor of the text to the correct position. As you can see I have a solution which gives me the desired otuput. However, I want to adjust the pin as documented. I really do not want to change the usage pattern.