I would like to know if there is a way of plotting a Lebesgue singular function using LaTeX (Tikz).

Lebesgue singular function $L_{a}\colon[0,1]\to[0,1]$ is defined as follow.

Imagine fipping an unfair coin with probability $0.5\neq a\in(0, 1)$ of heads and probability $1-a$ of tails. Let the binary expansion of $t\in [0, 1]\colon t =\sum_{k=1}^{\infty}\frac{\omega_{k}}{2^{k}}$ be determined by flipping the coin infinitely many times. In particular $\omega_{k}=0$ if the $k$-th toss is heads and $\omega_{k}=1$ if it is tails. Then $$L_{a}(x)\colon=\text{Prob}(t\leq x)$$

I don't know hot to do it manually with Tikz, because I'm at the very beginning at using it. Here is an example how to plot Cantor function. Maybe can be done something similar?

2 Answers 2


Here's a solution using the sagetex package, which gives you access to a computer algebra system and Python programming.

def LSF(binexp):
    a = .6666
    L = [0]
    U = [1]
    M = [1]
    for i in range(1,9):
        M += [(1-a)*L[len(L)-1]+a*U[len(U)-1]]
        if str(binexp)[i] == "1":
            L += [M[len(M)-1]]
            U += [U[len(U)-1]]
            U += [M[len(M)-1]]
            L += [L[len(L)-1]]

    return U[len(U)-1]

def BTD(mystr):
    sum = 0
    for i in range(1,9):
        sum += int(mystr[i])*(.5)^i
    return sum

xcoordsb = ['.{0:08b}'.format(i) for i in range(0,2^9)]
xcoords = [BTD(xcoordsb[i]) for i in range(0,2^9)]
ycoords = [LSF(xcoordsb[j]) for j in range(0,2^9)]
plotpoints = sorted([[xcoords[i],ycoords[i]] for i in range(0,2^9)], key=lambda k: [k[1], k[0]])

output = r""
output += r"\begin{tikzpicture}[scale=.7]"
output += r"\begin{axis}[xmin=0,xmax=1,ymin= 0,ymax=1,"
output += r"title={Lebesgue singular function, $a=.6666$}]"
output += r"\addplot[thin, blue, unbounded coords=jump] coordinates {"
for i in range(0,len(plotpoints)-1):
    output += r"(%s,%s) "%(plotpoints[i][0],plotpoints[i][1])
output += r"};"
output += r"\end{axis}"
output += r"\end{tikzpicture}"

The solution is shown running in Cocalc for when a=.6666. Changing the value of a and then compiling will let you get other values. enter image description here

My programming skills aren't strong, there's almost certainly an easier and more elegant ways to do this. I'm relying on an algorithm from Daniel Bernstein's thesis which I've linked to. The diagram on page 18 matches the output from Sage. The algorithm used is on page 19. Sage is not part of the LaTeX distribution. The quickest way to get started using it is with a free Cocalc account. You can install Sage on your computer so you aren't relying on an internet account. Search this site for sagetex to get more information.


Compute the function value at 2-adic fractions. You can do this by the following:

f(1/2) = a
do induction on n:
    for 0 ≤ k < 2^{n-1}:
        f(k/2^n) = f(k/2^{n-1})*a
    for 2^{n-1} ≤ k < 2^n:
        f(k/2^n) = f((k-2^{n-1})/2^{n-1})*(1-a)+a

So you got the following tex code. The \pgfkeys here acts as data array. The \pgfpath part plot the data point.



\def\pgfkeysgloballet#1#2{\global\expandafter\let\csname pgfk@#1\endcsname#2}
    \foreach\dep in{1,...,\n}{
        \foreach\ind in{0,...,\numexpr\twotodmo-1}{
        \foreach\ind in{\twotodmo,...,\numexpr\twotodep-1}{
    \foreach\ind in{0,...,\numexpr\twoton-1}{


enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .