# How to plot Dirichlet Function

I'm writting a document in maths and I want to add the graph of the Dirichlet Function. I was googling to find the tikz code of something like that but unfortunately I found nothing...

The graph is what seems in the picture:

I don't know how to plot it in tikz... and a link I found https://github.com/MartinThoma/LaTeX-examples/blob/master/tikz/dirichlet-function/dirichlet-function.tex

well while it was compiled... the graph was nowhere to be seen.

So, I would be more than happy if someone could provide me a link where I can copy the cody or draw that for me... I know it's a long shot!

• It's this one tex.stackexchange.com/questions/75688/… . What is the condition for this plot? And how do you distinguish a real number from a rational on a computer? – percusse Dec 5 '14 at 12:22
• BTW, as described in e.g. mathworld.wolfram.com/DirichletFunction.html what you've put in as an image isn't actually the Dirichlet function. – Joseph Wright Dec 5 '14 at 12:25
• Well that is what Google picture get.. when I type Dirichlet Function... !! Ok.. for the link.. apparentely I misunderstood..something.. so how do I draw tha graph on the pictue above that I posted? – J.K Dec 5 '14 at 12:27
• That is the same as the "popcorn" function (Thomae's function), see p.56 of the documentation of pst-func. Can easily be changed to draw lines, if needed. – user2478 Dec 5 '14 at 14:49
• @Herbert Thank you although the result is not as nice the picture's above... But I think that will suit me.. – J.K Dec 5 '14 at 18:38

A modification of the popcorn function. \psThomase@i will be part of the next version of pst-func, hopefully.

\documentclass[]{article}
\pagestyle{empty}
\usepackage{pst-func}
\makeatletter
\def\psThomae@i(#1,#2)#3{%
\begin@ClosedObj
1 1 #3 {
dup
/ipSave ED    % save loop value
/ip ED        % dito
1 1 #3 {
dup
/iqSave ED  % save loop value
/iq ED      % dito
{
iq 0 le { exit } if
ip iq mod
/ip iq def
/iq ED
} loop
ip 1 eq {
/xVal ipSave iqSave div def
xVal #1 ge { xVal #2 lt {
\pst@number\pslinewidth SLW
\pst@usecolor\pslinecolor xVal 1 iqSave div \tx@ScreenCoor
2 copy pop 0 moveto lineto } if } if
} if
} for
} for
}%
\end@ClosedObj%
}
\makeatother
\begin{document}

\psset{unit=4cm}
\begin{pspicture}(-0.1,-0.2)(2.5,1.15)
\psaxes{->}(0,0)(2.25,1.1)
\psThomae[linewidth=1pt](0,2){200}
\end{pspicture}

\end{document}


• May I ask you? the graph is ok... but pstrick does not compile... how can I fix it? – J.K Dec 6 '14 at 15:12
• Run it with xelatex or use \usepackage{auto-pst-pdf} and run it with pdflatex -shell-escape <file> – user2478 Dec 6 '14 at 15:42