4

This should be a simple question about positioning text with nodes using pgfplots.

To make a skew node line with text I'd use

\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{compat = newest}

\begin{document}

\begin{figure}
\begin{tikzpicture}
\begin{axis}[
    title = DPPH,
    xlabel = Applied Magnetic Field (I Guess) / G,
    xmax = 8,
    xmin = -8,
    ymax = 300,
    ymin = -300,
    ytick = \empty,
    xtick pos = left]

    \addplot[black, % Plotting the data
    no marks]
    table[x=xaxis,y=yaxis] {dpph2.dat};

    \node[coordinate,
    pin = {45:g factor = 1.96}
    ] at (0.2,224) {};

\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}

for example.

To make a skew node with rotated text, where I can position the text about the node line using above, below, left, right or a combination of those, I'd change the \node part above to

\node[coordinate,
    pin = {[rotate=45]right:g factor = 1.96}
    ] at (-0.3,250) {};

My question: how do I use above, below, right, etc in the first code (without rotating the text as well as the node line)? I tried things like 45right, 45, right, [45]right, but none of my guesses worked and I can't seem to find an example online.

EDIT

Here's what the first bit of code I put above gives me

enter image description here

and here's what I'd like to be able to do (just move where the text is with respect to the node line)

enter image description here Thanks.

3
  • 1
    Welcome to TeX.SX! Please add a complete minimal working example (MWE) starting with \documentclass{...} and ending with \end{document} so that users can reproduce your problem and try to help you. – Pier Paolo Feb 20 '15 at 13:09
  • What is, e.g. 45 left supposed to mean? Can you give an example/hand drawn sketch of what that should do? – darthbith Feb 20 '15 at 13:09
  • I think that by default pin draws a line from center of pin node to main node. What you can select is where position the pin node but not it's anchor. But you can use a label node and draw a connection line later. – Ignasi Feb 20 '15 at 13:44
3

(Ab)using the code from Jake's answer to How can I force TikZ pin angle?, we can define a new style for the pins that uses a label whose anchor we set to label the text, and an empty pin to draw the line. You supply the arguments like [anchor for label]angle:text to make it similar to the default pin usage. Presumably, there is some way to automatically choose the anchor based on the angle, but my TikZ-fu is not strong enough for that :-) Note that the % at the end of the lines are necessary because of how TikZ interprets spaces.

\documentclass{standalone}

\usepackage{pgfplots}
\pgfplotsset{compat = newest}
\tikzset{
    aligned pin/.style args={[#1]#2:#3}{
        pin={[%
            inner sep=0pt,%
            label={[%
                append after command={%
                    node[%
                        inner sep=0pt,%
                        at=(\tikzlastnode.#2),%
                        anchor=#1,%
                    ]{#3}%
                }%
            ]center:{}}%
        ]#2:{}}%
    }
}
\begin{document}

\begin{tikzpicture}
\begin{axis}[
    title = DPPH,
    xlabel = Applied Magnetic Field (I Guess) / G,
    xmax = 8,
    xmin = -8,
    ymax = 300,
    ymin = -300,
    ytick = \empty,
    xtick pos = left
    ]

    \node[coordinate,
    aligned pin={[west]45:g factor=1.96},
    ] at (0,0) {};

\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

2
  • Can aligned pin style be extended to allow for additional options of pin, for instance, pin distance or pin edge=[dashed]? – Hotschke May 25 '15 at 13:00
  • I would have in mind an identical only extended interface as pin: aligned pin={[pin distance=<default=3ex>, pin anchor=<west,east,center,north,south>, pin edge=<options>]<angle>:text} with pin anchor=center returning to standard pin behaviour. – Hotschke May 25 '15 at 14:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.