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.

Possible Duplicate:
How to label a path drawn using tikz with \draw plot?

Consider plotting a parabola using the following code:

\begin{scope}[domain=-2:2]
  \draw[<->,color=green,line width=1pt] plot[id=func1] function{x*x} node[right]{$t$};
\end{scope}

This code will label the parabola right to the upper right end of it. However, if I want to put the label somewhere in the middle, I tried the following:

\begin{scope}[domain=-2:2]
  \draw[<->,color=green,line width=1pt] plot[id=func1] function{x*x} node[pos=0.4]{$t$};
\end{scope}

which produces some unexpected positioning of the label. Is there a nice way out of it?

share|improve this question

marked as duplicate by Jake, percusse, Stefan Kottwitz Feb 1 '12 at 9:21

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
@Altermundus: Yes, I think it is. –  Jake Jan 16 '12 at 11:48
add comment

1 Answer

up vote 4 down vote accepted

You can use the decorations.markings library to place your label. Here's an example of a label style that's adapted from the manual. It takes two arguments, one for the position along the path, and one for the node text.

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{decorations.markings}

\begin{document}
\begin{tikzpicture}
\begin{scope}[
    domain=-2:2,
    label/.style 2 args={
        postaction={
            decorate,
            decoration={
                markings,
                mark=at position #1 with \node [right]{#2};
            }
        }
    }
]
\draw[<->,line width=1pt, label={0.6}{$t$}] plot[id=func1] function{x*x};
\end{scope}

\end{tikzpicture}
\end{document}

You can get exactly the same output using the current version (1.5.1) of PGFplots, which supports the pos key:

\documentclass{article}

\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    domain=-2:2,
    hide axis
]
\addplot [no markers,line width=1pt,<->] {x^2} node [pos=0.6,right] {$t$};
\end{axis}

\end{tikzpicture}
\end{document}
share|improve this answer
    
Is the a way to turn your first solution into a more global one, that is I want to use it for more then one function with different labels and labels' locations for each such function? Or should I copy-paste `\begin{scope}...\end{scope} for each function separately? –  Dror Jan 16 '12 at 12:24
    
You can add the label/.style 2 args... code to your \begin{axis}[<options], then you can add different labels to all your plots (or other paths). –  Jake Jan 16 '12 at 12:27
add comment

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