5

How can one draw orthogonal tick marks on a function? For instance in the following plot I would like to make orthogonal tick marks on the function y=x^2, say.

\documentclass{article}
\usepackage{tikz} 
\begin{document}
\begin{tikzpicture}
      \draw[->] (-3,0) -- (4.2,0) node[right] {$x$};
      \draw[->] (0,-3) -- (0,4.2) node[above] {$y$};
      \draw[scale=0.5,domain=-3:3,smooth,variable=\x,blue] plot ({\x},{\x*\x});
      \draw[scale=0.5,domain=-3:3,smooth,variable=\y,red]  plot ({\y*\y},{\y});
    \end{tikzpicture}
\end{document}

I kind of know it will involve \foreach, but I am not sure how to proceed.

To make things clear, I would like to have the tick marks "following" the graph of the function. Also if there are two or more functions on the same plot, then each should have a separate option for decoration. Please see comments below.

1 Answers1

6

You can use a markings decoration for this.

Note that, for some reason, this doesn't work with the smooth option.

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

\begin{document}
\begin{tikzpicture}[
    decoration={
        markings,
        mark={
            between positions 0 and 1 step 0.4cm
            with {\draw (0,-1mm) -- (0,1mm);}
        }}
    ]
      \draw[->] (-3,0) -- (4.2,0) node[right] {$x$};
      \draw[->] (0,-3) -- (0,4.2) node[above] {$y$};
      \draw[scale=0.5,domain=-3:3,blue, postaction=decorate] plot ({\x},{\x*\x});
      \draw[scale=0.5,domain=-3:3,variable=\y,red, postaction=decorate]  plot ({\y*\y},{\y});
    \end{tikzpicture}
\end{document}
Jake
  • 232,450
  • Such an elegant solutions Jake, thank you very much! – Physics_maths Nov 13 '13 at 13:56
  • Do you think it is possible with two different styles/types of decoration (one for each line, say) in the same graph? – Physics_maths Nov 13 '13 at 13:59
  • 1
    Yes, that's possible. What styles were you thinking of? – Jake Nov 13 '13 at 14:05
  • Similar to the one above but when I change you code to: between positions 0 and 1 step 0.4cm with {\draw (0,0mm) -- (0,1mm);}

    then this applies to both lines. It would be nice if this only applied to one of the line and then for the other line we would have something like:

    between positions 0 and 1 step 0.4cm with {\draw (0,0) -- (0,-1mm);}

    Notice the -1mm in the second parenthesis.

    – Physics_maths Nov 13 '13 at 14:11
  • @LoveLearning Add the decoration style to each \draw commands. –  Nov 14 '13 at 00:04
  • Could you please show what you mean more explicitly @HarishKumar? – Physics_maths Nov 14 '13 at 01:37
  • 1
    Some thing like \draw[scale=0.5, domain=-3:3, blue, decoration={ markings, mark={ between positions 0 and 1 step 0.4cm with {\draw (0,-1mm) -- (0,1mm);} }}, postaction=decorate] plot ({\x},{\x*\x}); and change what you want. –  Nov 14 '13 at 01:41
  • 1
    @LoveLearning: That will be a duplicate. \draw[scale=0.5, domain=-3:3, variable=\x,blue, decoration={ markings, mark={ between positions 0 and 1 step 0.4cm with {\draw (0,0) -- (0,1mm);} }}, postaction=decorate] plot ({\x},{\x*\x}); and \draw[scale=0.5, domain=-3:3, variable=\y,red, decoration={ markings, mark={ between positions 0 and 1 step 0.4cm with {\draw (0,0) -- (0,-1mm);} }}, postaction=decorate] plot ({\y*\y},{\y}); should do what you want. Remove the options for tikzpicture of course. –  Nov 14 '13 at 13:11
  • It worked great the way you described it. I want to give you the solution credit. Thanks for the help. – Physics_maths Nov 14 '13 at 17:26