Hsin-Po's Website

TikZ TeX TalK


a spinning color wheel that seems to be drawing cycloid

% cycloid.tex
\foreach\f in{1,...,90}{ % frame
        \wheel[shift={(\dx,\dy)},rotate=-14]; % help wheel
        \wheel[shift={(-\dx,-\dy)},rotate=14]; % help wheel
        \wheel[]; % main wheel

Convert pdf to gif with terminal command (with ImageMagick installed)

convert -delay 2 cycloid.pdf cycloid.gif

or with this command that does anti-aliasing

convert -delay 2 -density 300 -resize 300x300 cycloid.pdf cycloid.gif

This is inspired by じゃがりきん’s twitter post.


A former pangram with certain word glowing

% diffuse.tex
    \pdfliteral{q 1 J 1 j 1 Tr}%
    \foreach\mixture in{5,10,...,45}{\relax%
    \pdfliteral{q 4 Tr}%
    \pdfliteral{1 Tr}%
    \foreach\mixture in{95,90,...,55}{\relax%
    Quick \textdiffuse{Blurred Fox}    \\
    Jumps over    \\
    the \textdiffuse{Laser Dog}

Convert pdf to jpg (or png in exchange for file size) with terminal command (with ImageMagick installed)

convert -density 144 -resize 2000 diffuse.pdf diffuse.jpg

Related to TeX.SX question How to make a glowing text? Borderline related to Make tikz arrows glow. See also the KTV version.

A similar contraption is used to increase text contrast

\def\KTV[#1][#2]#3{ %
    \special{pdf: literal direct q 1 j 1 J 1 Tr}%
    \special{pdf: literal direct Q}%

and hide future text in my polar code tutorial slides.

\def\focus<#1>#2{ %
    \alt<#1>{ %
    }{ %
        \special{pdf: literal direct 1 Tr}%

Usages are \KTV[red][blue]{your beautiful words} and \focus<2->{your beautiful words}.


Applying inversion transformation to Lenna the image

% inversion.tex
    \expandafter\xdef\csname Inv(\u,\v)x\endcsname{\xx}
    \expandafter\xdef\csname Inv(\u,\v)y\endcsname{\yy}
    \expandafter\xdef\csname#1x\endcsname{\csname Inv(#2,#3)x\endcsname}
    \expandafter\xdef\csname#1y\endcsname{\csname Inv(#2,#3)y\endcsname}
    \draw circle(.05)circle(5)(3,0)node{\LennaIpsum}(12.5,0);
    \foreach\u in{-10,...,10}{
        \foreach\v in{-10,...,10}{
            % Affine transformation of (u, v), unit mm
            % Take inversion, unit mm
            % Remember the coordinates
    \foreach\u in{-10,...,9}{
        \foreach\v in{-10,...,9}{
            % For every square, recall the coordinates of the four corners
            \RecallInversion NW(\u,\V)\RecallInversion NE(\U,\V)
            \RecallInversion SW(\u,\v)\RecallInversion SE(\U,\v)
            % The lower left triangle ◺
                \pgfsettransformentries\aa\ab\ba\bb{\SWx mm}{\SWy mm}
                \path(-\u mm,-\v mm)node{\LennaIpsum};
            % The upper right triangle ◹
                \pgfsettransformentries\aa\ab\ba\bb{\NEx mm}{\NEy mm}
                \path(-\U mm,-\V mm)node{\LennaIpsum};

lenna.png is taken from Wikipedia. Learn more about Lenna’s history.

Convert pdf to jpg with terminal command (with ImageMagick installed)

convert -density 300 inversion.pdf inversion.jpg

This is the final form of a series of answers on TeX.SX:

See also Jelani Nelson’s slides.


Pixelated galaxy is rotating

% galaxy.tex
        % X Y
        1 index      % X Y X
        floor .5 add % X Y U:=floor(X)+.5
        1 index      % X Y U Y
        floor .5 add % X Y U V:=floor(Y)+.5
        2 copy       % X Y U V U V
        2 copy       % X Y U V U V U V
        25 div       % X Y U V U V U v:=V/25
        dup mul      % X Y U V U V U v²
        exch         % X Y U V U V v² U
        25 div       % X Y U V U V v² u:=U/25
        dup mul      % X Y U V U V v² u²
        add sqrt     % X Y U V U V r:=√u²+v²
        360 mul      % X Y U V U V R:=360r
        3 1 roll     % X Y U V R U V
        atan         % X Y U V R θ
        ~ add        % add the phase
        add          % X Y U V τ:=R+θ+phase
        sin          % X Y U V sin(τ)
        dup mul      % X Y U V s:=sin(τ)²
        5 1 roll     % s X Y U V
        3 2 roll     % s X U V Y
        sub 2 mul    % s X U y:=2(V-Y)
        dup mul      % s X U y²
        3 1 roll     % s y² X U
        sub 2 mul    % s y² x:=2(X-U)
        dup mul      % s y² x²
        add          % s q:=y²+x²
        le {0 0 0} {1 1 1} ifelse % s ≤ q ? black : white

ImageMagick does not convert this pdf properly. (In general, it has issues with functional shading.) I ended up using online conversion tools.

This is inspired by http://www.peda.com/grafeq/gallery.html.

\pgfdeclarefunctionalshading is very useful.


Pixelated egg with light shading

% egg.tex
\def\ditherdivide{ %%%  y  x  dither_score  power_of_2
    2 mul 4 2 roll 2 mul dup cvi 1 and 2 mul
    3 2 roll 2 mul dup 4 1 roll cvi 1 and
    3 mul xor 5 4 roll add 4 div 4 3 roll
\pgfdeclarefunctionalshading{ordered dithering egg}
    %%%  X:[0,50]  Y:[0,50]
    50 div exch 50 div 0.5 1
    %%%  y:[0,1]  x:[0,1]  0.5:dither_score  1:power_of_2
    %%%   Y:[0,power2]  X:[0,power2]  score power2
    4 2 roll floor 2 index div  %  score  power2  Y  x
    3 1 roll floor exch div     %  score  x  y
    %%%  score  x:[0:1]  y:[0,1]
    .5 sub 2.2 mul exch .5 sub 2.2 mul  %  recenter and rescale
    1 index 5 div 1.3 add mul   %  score  x  y(1.3+x/5)
    %%%  score  u:=x  v:=y(1.3+x/5)
    dup dup mul                 %  score  u  v  v^2
    2 index dup mul add         %  score  u  v  r=radial
    1 ge {pop pop 0.5} {
    %   https://tex.stackexchange.com/a/75994
        dup dup mul 2 index dup mul add 1 sub   %  -w(u,v)^2
        .3 dup mul .4 dup mul add 1 sub         %  -w(.3,.4)^2
        mul sqrt                %  ww'
        exch .3 mul add         %  += vv'
        exch .4 mul add         %  += uu'
        dup abs add 2 div       %  keep if ≥ 0 otherwise 0
        .1 add                  %  affine transform light
    %%%  score  light
    le {1} {0} ifelse           %  sum<light
    \tikz\shade[shading=ordered dithering egg]

Using macOS Preview.app to convert pdf to png (with resolution 256 pixel/inch). But if you look closely, the “correct” resolution should be 128 pixel/inch. Turns out there are some rounding issues no matter I use floor or round in the code. The issues go away when the resolution is doubled.

This is a derivation of my answer to How can I add stippling to elements of my tikz diagrams? Drawing an egg here is motivated by Kurzgesagt’s video The Egg - A Short Story.


This is not TikZ. But this piece of TeX code generates my old, deprecated email hpwang2@illinois.edu.


This piece of TeX code generates my old, deprecated email hsw001@ucsd.edu.


Try it on Overleaf! But either you need to change the compiler to TeX manually (which is not quite easy), or you can wrap the code above using:


The trick is that TeX has this neat feature that ^^68 is interpret as the letter h because the ASCII code for the letter h is 0x68. TeX also interprets ^^( as the letter h because the ASCII code for the left parenthesis ( and the ASCII code for the letter h differ by sixty-four. With this trick, lowercase letters abcd... can be expanded into ^^61^^62^^63^^64... or, less obviously, ^^!^^"^#^^$....

However, it would be less mysterious if one sees two superscript symbols ^^ everywhere in a code. So TeX also has this neat feature that it doesn’t have to be two superscript symbols; any repeated symbols whose category code is seven will work. For instance, if we change the category code of the left parenthesis to seven by \catcode`(=7, then ((( equals ^^( equals h. Similarly, if you change the category code of the number six to seven by \catcode`6=7, then 6668 equals ^^68 equals h.

Now it becomes a matter of looking up every letter in your email address and choose either ^^68 or ^^(, whichever maximizes the obscurity.

These pieces of code are motivated by the (unnecessary) need to hide one’s email address from spammers. This is inspired by the famous file xii.tex.


A binary tree that is somewhat irregular

    \pgfparserdef{bonsai}{initial}{the character :}{
        % \PackageWarning{bonsai}{execute :}
        \pgfmathsetmacro\angle{\fanout*1.5 + 30}
    \pgfparserdef{bonsai}{initial}{the character <}{
        % \PackageWarning{bonsai}{execute <}
        \pgfmathsetmacro\angle{\angle - \fanout}
        \path (0, 0) coordinate (stem);
        \pgfmathsetmacro\angle{\angle * 0.75}
        \pgfmathsetmacro\factor{\factor * (0.8 + abs(\angle/1000))}
        \draw [root!\factor!leaf, line width=\factor/100*\bonsaiwidth]
            (stem) to[in=\angle+180+rand*20, out=\angle+rand*20] (0, 0);
        \pgfmathsetmacro\angle{\angle + \fanout*1.5}
    \pgfparserdef{bonsai}{initial}{the character >}{
        % \PackageWarning{bonsai}{execute >}
    \pgfparserdef{bonsai}{initial}{the character .}{
        % \PackageWarning{bonsai}{execute .}
        \fill [leaf] (0, 0) circle [radius=\factor/500];
    \pgfparserdef{bonsai}{initial}{the character ;}{
        % \PackageWarning{bonsai}{execute ;}
        \filldraw [red!50!black, line width=2pt]
            (-1, 0) -- (1, 0) -- (.7, -.5) -- (-.7, -.5) -- cycle;

Convert pdf to png with terminal command (with ImageMagick installed)

convert -density 600 bonsai.pdf bonsai.png

Pixel art

A pixelated Mona Lisa

% pixel.tex


\def\pixelartnextrow{ % next row = new line = enter
    \pgfmathsetmacro\pixelarty{\pixelarty - 1}
\def\pixelartput#1{ % #1 is a color or tikz options
    \pgfmathsetmacro\pixelartx{\pixelartx + 1}
    \fill [color={#1}] (\pixelartx, \pixelarty) rectangle +(1.05, 1.05);
    % 1.05 is overshoot; necessary to avoid white gaps

\pgfparserdef{pixel art}{initial}{the character <}{\pixelartinitial}
\pgfparserdef{pixel art}{initial}{the character >}{\pixelartfinal}
\pgfparserdef{pixel art}{initial}{the character ^^M}{\pixelartnextrow}%
\pgfparserdef{pixel art}{initial}{the character '}{\pixelartput{gray,7: black,0; white,7}}
\pgfparserdef{pixel art}{initial}{the character -}{\pixelartput{gray,7: black,1; white,6}}
\pgfparserdef{pixel art}{initial}{the character ;}{\pixelartput{gray,7: black,2; white,5}}
\pgfparserdef{pixel art}{initial}{the letter l}{\pixelartput{gray,7: black,3; white,4}}
\pgfparserdef{pixel art}{initial}{the letter C}{\pixelartput{gray,7: black,4; white,3}}
\pgfparserdef{pixel art}{initial}{the letter D}{\pixelartput{gray,7: black,5; white,2}}
\pgfparserdef{pixel art}{initial}{the letter H}{\pixelartput{gray,7: black,6; white,1}}
\pgfparserdef{pixel art}{initial}{the letter M}{\pixelartput{gray,7: black,7; white,0}}

\begin{tikzpicture}[x=1mm, y=2mm]
\pgfparserparse{pixel art}<


Would it be nice if TikZ is WYSIWYG?

Convert pdf to png with terminal command (with ImageMagick installed)

convert -density 300 pixel.pdf pixel.png

Another approach is to use \pdfliteral.