[Tex/LaTex] How to label the curve of a function at any point along it in tikz

labelsnodespositioningtikz-pgf

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}

Best Answer

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}