TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Hey, I have a function which I would like to plot with latex/tikz. Since I'm just a beginner I really don't know how to do it. Don't get me wrong I don't have any problems with most functions but this one is a killer and the documentation is a little bit overwhelming.

jagged function

It would be great if someone could help me to plot this one.

share|improve this question

You can try the following.

part 1 : I think the difficult part is to get the fractional part of a number. The pgf/tikz function Mod does just that. Note that it is not the same as mod.

part 2 : one problem with the function f and tikz is the fact that tikz uses fixed point arithmetic and for this function you quickly exceed the limits. One way around this is to use the fpu library., but this is not what I will do. A pure tikz approach, noticing that u(100^n*x) is only there to get successive decimal digits of x, is shown below. Also, the sample rate of the plot changes the aspect of the plot dramatically.



[declare function={
    u(\x) = Mod(\x,1);
    j(\x) = 1- abs(u(\x)-0.5);
    f(\x) = j(100*\x)/10 + j(100*u(100*\x))/100 + j(100*u(100*u(100*\x)))/1000;}]

\draw[domain=-1:1,samples=50] plot (\x,{f(\x)});

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.