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.

In my example, I have to simple curves y = sqrt(x) and y = x. I would like to be able to attach a label along each curve, say at the middle of the curve. Right now, I am doing it by trial and error until it looks "right." How may I use node positioning to achieve what I have?

\documentclass{article}
\usepackage{tikz}
\begin{document}
    \begin{tikzpicture}[scale=2,>=latex,x=1.5cm]
        \begin{scope}
            \draw[->] (-0.2,0) -- (1.5,0) node[above left]{\footnotesize $x$};
            \draw[->] (0,-0.2) -- (0,1.5) node[below right]{\footnotesize $y$};
            \draw[-,domain=0:1,samples=250] plot (\x,{sqrt(\x)});
            \draw[-,domain=0:1,samples=250] plot (\x,{\x});
            \node at (0.35,0.75) {\footnotesize $y=\sqrt{x}$};
            \node at (0.65,0.5) {\footnotesize $y=x$};
        \end{scope}
    \end{tikzpicture}
\end{document}
share|improve this question

4 Answers 4

up vote 5 down vote accepted

Here is a solution that mylabel style is defined and it takes 3 arguments {at #1 #2 with #3} which is translated into `at #1 (position) #2 (above/below/left/right) with #3 (a label)

enter image description here

Code

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}
\begin{document}

\tikzset{mylabel/.style  args={at #1 #2  with #3}{
    postaction={decorate,
    decoration={
      markings,
      mark= at position #1
      with  \node [#2] {#3};
 } } } }

    \begin{tikzpicture}[scale=2,>=latex,x=1.5cm]
    \begin{scope}[domain=0:1]
            \draw[->] (-0.2,0) -- (1.5,0) node[above left]{\footnotesize $x$};
            \draw[->] (0,-0.2) -- (0,1.5) node[below right]{\footnotesize $y$};
            \draw[-,domain=0:1,samples=250,mylabel=at 0.7 above left with {$y=\sqrt{x}$}] plot (\x,{sqrt(\x)});
            \draw[-,domain=0:1,samples=250,mylabel=at 0.5 below right with {$y=x$}] plot (\x,{\x});
%            \node at (0.35,0.75) {\footnotesize $y=\sqrt{x}$};
%            \node at (0.65,0.5) {\footnotesize $y=x$};
        \end{scope}
    \end{tikzpicture}
\end{document}
share|improve this answer

For graphs I would recommend you use the axis environment from pgfplots instead and place the graph labels with nodes specifying the position along the line with pos=:

enter image description here

Code:

\documentclass[border=2pt]{standalone}
\usepackage{pgfplots}
\begin{document}
    \begin{tikzpicture}
    \begin{axis}[
        axis lines=center,
        xmax = 1.1,
        ymax = 1.1,
        ylabel=$y$,
        xlabel=$x$,
        ]
        \addplot [domain=0:1,samples=250, ultra thick, blue] {sqrt(x)}
            node [pos=0.9, above left] {$y=\sqrt{x}$};
        \addplot [domain=0:1,samples=250, ultra thick, red ] {x}
            node [pos=0.3, below right] {$y=x$};
    \end{axis}
    \end{tikzpicture}
\end{document}
share|improve this answer

Would you be satisfied with :

\documentclass{article}
\usepackage{tikz}
\begin{document}
    \begin{tikzpicture}[scale=2,>=latex,x=1.5cm]
        \begin{scope}
            \draw[->] (-0.2,0) -- (1.5,0) node[above left]{\footnotesize $x$};
            \draw[->] (0,-0.2) -- (0,1.5) node[below right]{\footnotesize $y$};
            \draw[-,domain=0:1,samples=250] plot (\x,{sqrt(\x)});
            \draw[-,domain=0:1,samples=250] plot (\x,{\x});
            \node[above left] at (0.5,{sqrt(0.5)}) {\footnotesize $y=\sqrt{x}$};
            \node[below right] at (0.5,0.5) {\footnotesize $y=x$};
        \end{scope}
    \end{tikzpicture}
\end{document}

I just position the node by,

at (X,{F(X)})

I chose X in the middle of the domain here. And I specify if the text should be above/below, left/right...

share|improve this answer

Just for typing exercise with PSTricks.

Option 1

\documentclass[pstricks,border=12pt,12pt]{standalone}
\usepackage{pst-plot}
\psset{unit=4cm}

\def\f{sqrt(x)}
\def\g{x}

\begin{document}
\multido{\r=0.0+0.1}{15}{%
\begin{pspicture}[algebraic,plotpoints=200](-1,-1)(2.25,2.25)
    \psaxes{->}(0,0)(-1,-1)(2,2)[$x$,0][$y$,90]
    \psplot[linecolor=red]{0}{1.5}{\f}
    \psplot[linecolor=blue]{-.5}{1.5}{\g}
    \uput[45](*1.5 {\g}){\textcolor{blue}{$y=x$}}
    \uput[90](*\r\space {\f}){\textcolor{red}{$y=\sqrt x$}}
\end{pspicture}}
\end{document}

enter image description here

Option 2

\documentclass[pstricks,border=12pt,12pt]{standalone}
\usepackage{pst-plot,pst-text}
\psset{unit=4cm}

\def\f{sqrt(x)}
\def\g{x}

\begin{document}
\multido{\r=-1.0+0.1}{18}{%
\begin{pspicture}[algebraic,plotpoints=200](-1,-1)(2.25,2.25)
    \psaxes{->}(0,0)(-1,-1)(2,2)[$x$,0][$y$,90]%
    \pstextpath[r](0,.1){\psplot[linecolor=blue]{-.5}{1.5}{\g}}{\textcolor{blue}{$g(x)= x$}}%
    \pstextpath[c](\r,.1){\psplot[linecolor=red]{0}{1.5}{\f}}{\textcolor{red}{$f(x)=\sqrt x$}}%
\end{pspicture}}
\end{document}

enter image description here

Note: the red small horizontal line is given as a bonus. Is it a bug? I don't know!

share|improve this answer

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.