# Drawing Cantor Set

I am trying to draw a cantor set in LaTeX and I am having issues doing this. I am trying multiple \put commands, but they just go to the right of one another. Does anyone know how to do this?

Any help would be much appreciated.

-
Welcome to TeX.SE. It is always best to compose a MWE that illustrates the problem including the \documentclass so that those trying to help have something to start with. –  Peter Grill Oct 19 '11 at 3:23
You should also consider using tikz. –  Peter Grill Oct 19 '11 at 3:25
There is code for the Cantor set on page 336 of the Tikz/PGF manual –  DJP Oct 19 '11 at 4:30

Here is my version of the first five rows for the Cantor set:

\begin{picture}(1.1,0.40)(-0.1, -0.1)
\put(0.45, 0.27){$S^c = C$}
\put(0, 0){\line(1, 0){1}}
\put(0, -0.025){\line(0, 1){0.025}}
\put(1, -0.025){\line(0, 1){0.025}}
\put(0, -0.05){\small 0}
\put(1, -0.05){\small 1}

\put(0, 0.25){\line(1, 0){1}}

% (0, 2) / 3
\put(0.0000, 0.20){\line(1, 0){0.3333}}
\put(0.6667, 0.20){\line(1, 0){0.3333}}

% (0, 2, 6, 8) / 9
\put(0.0000, 0.15){\line(1, 0){0.1111}}
\put(0.2222, 0.15){\line(1, 0){0.1111}}
\put(0.6667, 0.15){\line(1, 0){0.1111}}
\put(0.8889, 0.15){\line(1, 0){0.1111}}

% (0, 2, 6, 8, 18, 20, 24, 26) / 27
\put(0.0000, 0.10){\line(1, 0){0.037}}
\put(0.0741, 0.10){\line(1, 0){0.037}}
\put(0.2222, 0.10){\line(1, 0){0.037}}
\put(0.2963, 0.10){\line(1, 0){0.037}}
\put(0.6667, 0.10){\line(1, 0){0.037}}
\put(0.7407, 0.10){\line(1, 0){0.037}}
\put(0.8889, 0.10){\line(1, 0){0.037}}
\put(0.9630, 0.10){\line(1, 0){0.037}}

% (0, 2, 6, 8, 18, 20, 24, 26, 54, 56, 60, 62, 72, 74, 78, 80) / 81
\put(0.0000, 0.05){\line(1, 0){0.0123}}
\put(0.0247, 0.05){\line(1, 0){0.0123}}
\put(0.0741, 0.05){\line(1, 0){0.0123}}
\put(0.0988, 0.05){\line(1, 0){0.0123}}
\put(0.2222, 0.05){\line(1, 0){0.0123}}
\put(0.2469, 0.05){\line(1, 0){0.0123}}
\put(0.2963, 0.05){\line(1, 0){0.0123}}
\put(0.3210, 0.05){\line(1, 0){0.0123}}
\put(0.6667, 0.05){\line(1, 0){0.0123}}
\put(0.6914, 0.05){\line(1, 0){0.0123}}
\put(0.7407, 0.05){\line(1, 0){0.0123}}
\put(0.7654, 0.05){\line(1, 0){0.0123}}
\put(0.8889, 0.05){\line(1, 0){0.0123}}
\put(0.9136, 0.05){\line(1, 0){0.0123}}
\put(0.9630, 0.05){\line(1, 0){0.0123}}
\put(0.9877, 0.05){\line(1, 0){0.0123}}

\end{picture}

-

Although I would highly recommend using tikz, here is an example of using the \put command.

The syntax is \put(x,y){} where (x,y) is the coordinate of the start point, a the object to be placed at this coordinate is passed in as a parameter within the {} braces. Below I specified this object to be a line with the \line(u,v){n} command. In this case (u,v) is the direction vector of the line and the length of this line is passed in as n.

Here is the code for the above picture. If this does not help, please use this as a start to create a small piece of code the illustrates the problem that you are having.

\documentclass{article}
\begin{document}
\setlength{\unitlength}{1cm}
\begin{picture}(2, 2)
\put(0,0){\line(0,1){3}}% Vertical line of length 3 starting at (0,0)
\put(0,2){\line(1,0){1}}% Horizontal line of length 1 starting at (0,2)
\put(0,1){\line(1,0){5}}% Horizontal line of length 5 starting at (0,1)
\end{picture}
\end{document}

-

Yes the pgfmanual gives a way in Fractal Decorations but it's more funny to find a personal way. I don't find very pretty the syntax to use with decoration.

The next code gives a macro \cantor without tikz. The macro needs two arguments : the first one is the step and the second is the length of the line.

To get :

you need to write :

\unitlength=1pt
\linethickness{3mm}
\xlen=270pt
\cantor{1}{270pt}\par
\cantor{2}{270pt}\par
\cantor{3}{270pt}\par
\cantor{4}{270pt}\par
\cantor{5}{270pt}\par
\cantor{6}{270pt}


Here the complete code

\documentclass{article}
\usepackage{fp}
\newcount\cnt \cnt=1
\newcount\ccnt \ccnt=0
\newdimen\xpos
\newdimen\xlen
\newcount\cnti
\makeatletter

%%%%%%%%%%%% main macro %%%%%%%%%%%%%%%%%%%
% get a string composed with 0 and  1.  If 1 a line is drawn.
% The string  is stored in \tmp
% the length of the string is  stored in \ccnt
\def\scan#1#2\end{%
\def\aux{#1}%
\ifx\aux\empty
\else
\ifnum #1=1
\ifnum\ccnt=0  \def\tmp{101}%
\else
\expandafter\def\expandafter\tmp\expandafter{\tmp 101}%
\fi
\else
\expandafter\def\expandafter\tmp\expandafter{\tmp 000}%
\fi
\def\aux{#2}%
\onestep{#1}%
\ifx\aux\empty
\else
\scan#2\end
\fi
\fi
\gdef\compteur{\the\ccnt}%
}%
%%%%%%%%%%%%% draw a line %%%%%%%%%%%%%%%%%%
\def\scandraw#1#2\end{%
\def\aux{#1}%
\ifnum #1=1
\put(\strip@pt\xpos,0){\line(1,0){\strip@pt\xlen}}%
\else
\fi
\def\aux{#2}%
\ifx\aux\empty
\else
\scandraw#2\end
\fi
}%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\def\cantor#1#2{%
\xpos=0pt \xlen=#2
\noexpand\def\tmp{1}
\ifnum #1>1
\cnti=1
\loop
\ccnt=0
\expandafter\scan \tmp\end
\ifnum \cnti<#1
\repeat
\FPeval{\tmpxlen}{\strip@pt\xlen/(3*\compteur)}%
\xlen=\tmpxlen pt
\fi
\begin{picture}(0,15)
\expandafter\scandraw \tmp\end
\end{picture}
}%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\makeatother
\begin{document}

\unitlength=1pt
\linethickness{3mm}
\xlen=270pt
\cantor{1}{270pt}\par
\cantor{2}{270pt}\par
\cantor{3}{270pt}\par
\cantor{4}{270pt}\par
\cantor{5}{270pt}\par
\cantor{6}{270pt}
\end{document}

-