How can one obtain something like in the photo bellow using Tikz?enter image description here

Look what I've got using tikz decoration library enter image description here


\usetikzlibrary {decorations.fractals}
\begin{tikzpicture}[line width=20pt, decoration=Cantor set]
{ \sffamily \bfseries \Huge
  \clip (0,.5) rectangle +(1,-4.85);
  \draw decorate{ (0,0) -- (3,0) } node[white] at (0.5,0) { C};
  \draw decorate{ decorate{ (0,-.75) -- (3,-.75) }} node[white] at (0.5,-0.75) {A};
  \draw decorate{ decorate{ decorate{ (0,-1.5) -- (3,-1.5) }}} node[white] at (0.5,-1.5) {N};
  \draw decorate{ decorate{ decorate{ decorate{ (0,-2.25) -- (3,-2.25) }}}} node[white] at (0.5,-2.25) {T};
  \draw decorate{ decorate{ decorate{ decorate{ decorate{ (0,-3) -- (3,-3) }}}}} node[white] at (0.5,-3) {O};
  \draw decorate{ decorate{ decorate{ decorate{ decorate{ decorate { (0,-3.75) -- (3,-3.75) }}}}}} node[white] at (0.5,-3.75) {R};

This solutions it seams to be of brute force type. I am asking you if there exists a different type of clipping to achieve this effect.

  • 1
    Should this only work with capital letters? If you include all letters that are smaller or have depth, the lines won't align as neat. Mar 21, 2023 at 20:14

3 Answers 3


Here's a recursive clipping solution.

It seems that starting with the fifth applications of the recursive calculation either the precision of TeX/PGFMath is not enough or my renderer just doesn't like it anymore.

For the row of Xs the resolution of the PDF to PNG convertes is already too low. I've added an enormous M to the examples instead.

The solution given below clips each letter individually.

Similar to dexteritas' answer it would be possible to install a clipping over the whole area but this doesn't increase the efficiency since the parts that are clipped away in A are also getting clipped away in N. Even though this solution needs a scope for each letter it doesn't appear to be any slower.


\documentclass[varwidth, border=1pt]{standalone}
\usetikzlibrary{calc, chains}
    start chain=going base right, node distance=+0mm, baseline=(chain-begin.base)},
    text=black, inner sep=+0pt, outer sep=+0pt, node font=\sffamily\bfseries\Huge},
  cantor/cantor/.style n args={3}{insert path={
      coordinate (@1-#1) at ({$(#2)!.33333!(#3)$}-|#3)
      \ifnum#1=1 (#2) rectangle (@1-#1)
      \else      [cantor/cantor/.expanded={\pgfinteval{#1-1}}{#2}{@1-#1}] \fi
      coordinate (@2-#1) at ({$(#2)!.66667!(#3)$}-|#2)
      \ifnum#1=1 (@2-#1) rectangle (#3)
      \else      [cantor/cantor/.expanded={\pgfinteval{#1-1}}{@2-#1}{#3}] \fi}}}
  \foreach[count=\CNT from \pgfkeysvalueof{/tikz/cantor/start}]\LTR in{#2}{
    \node[cantor/node,on chain]{\phantom{\LTR}};
      \ifnum\CNT>0 \clip[cantor/cantor={\CNT}{chain-end.south west}
                                             {chain-end.north east}];\fi
\Huge\bfseries\sffamily % just to have the same text in document as in TikZ
\cantortext{C, A, N, T, O, R}\par
\cantortext[cantor/start=3]{X, X, X, X, X, X}
%\cantortext[transform shape, scale=5, cantor/start=5]{M}


enter image description here

enter image description here


I came up with this solution using plain pdfTeX. I didn't notice that you specified TikZ before I started working on this solution and by the time I had, I had already sunk some time into it. Sorry if this doesn't help answer your question.

Cantorized Cantor

%% Useful TeX macros
    \lccode`\?=`\p \lccode`\!=`\t
% \idim{<dimension expression>} -> the evaluation of the expression without a dimension
\def\idim#1{\expandafter\rmpt\the\dimexpr #1\relax}


% \map<\macro>{<general text>} -> applies \macro to every group in <general text>

%% Helper macros for \cantorize

    \ifnum#1>0 %
        #2 #3 \cantorwd, #4 re
        \cantorizeA{\the\numexpr #1-1\relax}%
                  {\idim{#2pt + \cantorwd,pt}}%
                  {\idim{#4pt / 3}}%
        \cantorizeA{\the\numexpr #1-1\relax}%
                  {\idim{#2pt + \cantorwd,pt}}%
                  {\idim{#3pt + #4pt * 2 / 3}}%
                  {\idim{#4pt / 3}}%

% Typeset character with width \cantorwd,
\def\cantorsetwidth#1{\hbox to\cantorwd,pt{\hss#1\hss}}
% Update cantorlen and cantorwd
    \advance\cantorlen by 1\relax%
% vertical buffer around text in \cantorize

    \edef\cantorht,{\idim{\ht0 + \cantorbuf,pt * 2}}%
        \pdfliteral{.96859 0 0 .96859 0 0 cm \cantorizeA{\cantorlen}{0}{-\cantorbuf,}{\cantorht,} W n}%

%% Showcasing \cantorize

% Bigger font for showcasing the macro
\font\bigss=cmssbx10 at 50pt


Instead of drawing white rectangles, this clips the text so you don't need to alter anything if you plan on having a background color as well.



At first, each character is drawn in its own node. As I didn't find a way to invert the result of the cantor set decoration, I started from scratch and defined a recursive function \drawmiddleline. It draws white lines over the text. Its four parameters are described in the code. In the image below, I used different colors (from light red to dark red) to show how it is drawn step by step.

enter image description here

I saved the current level with \cslet{dml-#2}{\level} as the first recursive call would change the variable \level, that is still needed for the second recursive call.

To get the height of a letter without depth, I used:

\settoheight{\myheight}{\huge A}%

\myspacing defines the space between the character nodes.

Of course, you could change the font to something different, but I thought that's not the main problem here.


enter image description here



    % #1 = char index
    % #2 = current level
    % #3 = current width
    % #4 = current yshift
%       \pgfmathsetmacro{\c}{20*#1}
%       \draw[red!\c!white, line width=#3/3] ([yshift=#4] c#1.west) -- ([yshift=#4] c\n.east);
        \draw[white, line width=#3/3] ([yshift=#4] c#1.west) -- ([yshift=#4] c\n.east);

    \foreach \char [count=\i from 0] in {#1} {
        \node[anchor=south west, inner sep=.3pt, font=\huge] (c\i) at (\i*\myspacing,0) {\char};
    \foreach \i in {1, ..., \n} {

\settoheight{\myheight}{\huge A}%

You must log in to answer this question.

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