% Manual for kolam-tikz.sty.  Compile from this directory with
%   TEXINPUTS=../tex: latexmk -pdf kolam-tikz-doc.tex
% (or with kolam-tikz.sty installed).  The images come from ../docs/assets and
% ../examples in the repository, or from this folder in the CTAN package.
\documentclass[11pt]{article}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[margin=2.5cm]{geometry}
\usepackage{amsmath,amssymb}
\usepackage{booktabs}
\usepackage{array}
\usepackage{graphicx}
\graphicspath{{./}{../docs/assets/}{../examples/}}
\usepackage{kolam-tikz}
\usepackage[hidelinks]{hyperref}
\newcommand\pkg[1]{\textsf{#1}}
\newcommand\cmd[1]{\texttt{\textbackslash #1}}
\newcommand\meta[1]{\ensuremath{\langle}\textit{#1}\ensuremath{\rangle}}
\newcommand\marg[1]{\texttt{\{}\meta{#1}\texttt{\}}}
\setlength{\parindent}{0pt}
\setlength{\parskip}{0.6em}
\setlength{\emergencystretch}{2em}

% Four pulli in a square, all four gates open: two closed strokes.
\newcommand\SeedKolam{%
  \begin{kolam}
    \KolamSetup{turn radius=exact,line width=1.2pt,dot size=4.2pt}
    \KolamPulli{0}{0}\KolamPulli{1}{0}\KolamPulli{0}{1}\KolamPulli{1}{1}
    \KolamOpenGate{0}{0}{1}{0}\KolamOpenGate{0}{1}{1}{1}
    \KolamOpenGate{0}{0}{0}{1}\KolamOpenGate{1}{0}{1}{1}
    \KolamRender
  \end{kolam}}

\title{The \pkg{kolam-tikz} package\\[0.4em]
  \large A coordinate-first TikZ grammar for pulli kolam}
\author{Aalok Thakkar}
\date{Version 0.2.0, 2026/11/22}
\begin{document}
\maketitle

\begin{center}
\includegraphics[width=0.9\textwidth]{gallery.png}
\end{center}

\pkg{kolam-tikz} draws pulli kolam: a strand loops around a grid of dots
(pulli). A drawing is declared as integer pulli coordinates and a state,
open or closed, for each gate between two neighbouring pulli. The package
constructs the strands from these declarations, with circular turns and
straight tracks that meet tangentially. Nothing else is drawn by hand, so a
source file states exactly which drawing it makes.

Pulli kolam is a living family of practices with regional vocabularies and
many ways of learning and making. This package supplies one narrow formal
drawing language inspired by pulli kolam. It does not define, exhaust or
replace the wider practice.

The package needs \LaTeXe\ with TikZ and works with pdf\LaTeX, Lua\LaTeX\ and
Xe\LaTeX. The source and an independent auditor are at
\url{https://github.com/aalok-thakkar/kolam-tikz}.

\section{A first kolam}

\begin{minipage}{0.62\textwidth}
\begin{verbatim}
\documentclass{article}
\usepackage{kolam-tikz}
\begin{document}
\begin{tikzpicture}[kolam grid]
  \begin{kolam}
    \KolamPulli{0}{0}  \KolamPulli{1}{0}
    \KolamPulli{0}{1}  \KolamPulli{1}{1}
    \KolamOpenGate{0}{0}{1}{0}
    \KolamOpenGate{0}{1}{1}{1}
    \KolamOpenGate{0}{0}{0}{1}
    \KolamOpenGate{1}{0}{1}{1}
    \KolamRender
  \end{kolam}
\end{tikzpicture}
\end{document}
\end{verbatim}
\end{minipage}\hfill
\begin{minipage}{0.3\textwidth}
\centering
\begin{tikzpicture}[kolam grid]
  \path[use as bounding box] (-.62,-.62) rectangle (1.62,1.62);
  \SeedKolam
\end{tikzpicture}
\end{minipage}

Every drawing sits in a \texttt{kolam} environment inside a
\texttt{tikzpicture} with the \texttt{kolam grid} style. The declarations
come first and \cmd{KolamRender} draws the result.

\section{The grammar}

A kolam program is a pair $K=(D,\sigma)$ of a finite set of pulli
$D\subset\mathbb Z^2$ and a gate assignment
$\sigma\colon E_D\to\{\mathsf{open},\mathsf{closed}\}$, where
\[
  E_D=\bigl\{\{p,q\}: p,q\in D,\ \lVert p-q\rVert_1=1\bigr\}
\]
is the set of gates: pairs of pulli that are immediate north, east, south or
west neighbours.

\subsection{Pulli}

\cmd{KolamPulli}\marg{x}\marg{y} declares a pulli at the integer point
$(x,y)$. Coordinates must be canonical decimal integer literals such as
\texttt{-2}, \texttt{0} or \texttt{3} (not \texttt{+3}, \texttt{03} or
\texttt{-0}). Declarations may be generated by TikZ \cmd{foreach} loops:

\begin{verbatim}
\foreach \x in {0,...,4} { \foreach \y in {0,...,2} { \KolamPulli{\x}{\y} } }
\end{verbatim}

\cmd{KolamThreeFiveFiveFiveThree} declares the centred 3--5--5--5--3 field
(row lengths from top to bottom), from $(-1,2)$ to $(1,-2)$.

\subsection{Gates}

\begin{verbatim}
\KolamGate{x}{y}{u}{v}{open}      % or {closed}
\KolamOpenGate{x}{y}{u}{v}
\KolamClosedGate{x}{y}{u}{v}
\end{verbatim}

All three forms name the undirected gate between $(x,y)$ and $(u,v)$. Both
endpoints must be declared pulli at Manhattan distance one. Every gate that
is not declared is closed, so a program lists only its open gates; an
explicit \cmd{KolamClosedGate} documents a deliberate choice.

\begin{center}
\begin{tikzpicture}[kolam grid,scale=.8]
  \begin{scope}
    \begin{kolam}
      \KolamPulli{0}{0}\KolamPulli{1}{0}
      \KolamRender
    \end{kolam}
    \node at (.5,-.9) {\small closed};
  \end{scope}
  \begin{scope}[shift={(3.5,0)}]
    \begin{kolam}
      \KolamPulli{0}{0}\KolamPulli{1}{0}
      \KolamOpenGate{0}{0}{1}{0}
      \KolamRender
    \end{kolam}
    \node at (.5,-.9) {\small open};
  \end{scope}
\end{tikzpicture}
\end{center}

\subsection{Strokes}

Each pulli has eight ports, two on each side. Four permanent corner arcs pair
adjacent side ports around the pulli. A closed gate caps the two ports on
each of its endpoints; an open gate pairs the two lanes across the gap, so
the strand crosses between the pulli. Every port then meets exactly one arc
and one gate or cap, so the port graph is 2-regular and its connected
components are the closed strokes of the drawing. A crossing that looks like
an X is two tracks passing over each other, not a junction: connectivity is
decided by the port pairing alone. The first kolam above has two strokes.

\section{Setup}

\cmd{KolamSetup}\marg{keys} sets the appearance for the current group.

\begin{center}
\begin{tabular}{l l p{0.45\textwidth}}
\toprule
Key & Default & Meaning \\
\midrule
\texttt{turn radius=exact} & exact & radius $r=1/(2\sqrt2)$ with an exact
45-degree port angle \\
\texttt{radius=}\meta{number} & 0.353553 & any radius with $0<r<1/2$, in grid
units; leaves exact mode \\
\texttt{line width=}\meta{dimension} & 1.15pt & strand width \\
\texttt{dot size=}\meta{dimension} & 4.10pt & pulli diameter \\
\texttt{dot fill=}\meta{colour} & black & pulli fill \\
\texttt{background=}\meta{colour} & white & pulli outline \\
\bottomrule
\end{tabular}
\end{center}

The strand colour is the colour \texttt{kolamTikZInk}. Change it with
\begin{verbatim}
\colorlet{kolamTikZInk}{blue!60!black}
\end{verbatim}
A radius outside the
permitted range raises an error and falls back to the exact preset. The
constant \cmd{KolamExactRadius} expands to the exact radius.

\begin{center}
\begin{tikzpicture}[kolam grid,scale=.8]
  \colorlet{kolamTikZInk}{blue!60!black}
  \begin{scope}
    \begin{kolam}
      \KolamSetup{line width=2pt,dot size=5pt,dot fill=orange}
      \KolamPulli{0}{0}\KolamPulli{1}{0}\KolamPulli{0}{1}\KolamPulli{1}{1}
      \KolamOpenGate{0}{0}{1}{0}\KolamOpenGate{0}{1}{1}{1}
      \KolamOpenGate{0}{0}{0}{1}\KolamOpenGate{1}{0}{1}{1}
      \KolamRender
    \end{kolam}
  \end{scope}
  \begin{scope}[shift={(3.5,0)}]
    \begin{kolam}
      \KolamSetup{radius=.25}
      \KolamPulli{0}{0}\KolamPulli{1}{0}\KolamPulli{0}{1}\KolamPulli{1}{1}
      \KolamOpenGate{0}{0}{1}{0}\KolamOpenGate{0}{1}{1}{1}
      \KolamOpenGate{0}{0}{0}{1}\KolamOpenGate{1}{0}{1}{1}
      \KolamRender
    \end{kolam}
  \end{scope}
\end{tikzpicture}
\end{center}

\section{Geometry and scaling}

With turn radius $r$ in grid units, $0<r<1/2$, the tracks leave each pulli
at the angle $\alpha=\arcsin(2r)$. The exact preset $r_*=1/(2\sqrt2)$ gives
$\alpha=45^\circ$, which the package assigns exactly instead of computing an
inverse sine; adjacent tracks are then collinear.

The tangency holds on the \texttt{kolam grid}, which is \texttt{x=1cm,y=1cm}.
Resize a drawing with a uniform \texttt{scale=}\meta{factor} on the picture
or a scope. Do not change \texttt{x} or \texttt{y} separately: TikZ stores arc
radii as physical lengths, so the arcs and the grid would no longer match,
and non-uniform scaling turns the turns into ellipses.

\section{Checking a drawing}

\cmd{KolamAssertDropFan}, placed before \cmd{KolamRender}, checks that every
pulli has one or three open gates (the Drop-Fan family) and raises an error
naming the first pulli that does not. It does not check that the drawing is
one stroke. The repository's auditor rebuilds the port graph from the source
file, independently of TikZ, and can require one stroke:

\begin{verbatim}
python3 tools/kolam-audit.py examples/03-braided-ladder.tex \
    --drop-fan --require-one
\end{verbatim}

The auditor reads only explicit \cmd{KolamPulli}, \cmd{KolamGate},
\cmd{KolamOpenGate}, \cmd{KolamClosedGate} and
\cmd{KolamThreeFiveFiveFiveThree} declarations.

\section{Partial-instance diagrams}

A figure in a paper sometimes shows a partly specified instance rather than a
finished kolam. A separate layer marks each gate as free, prescribed open or
prescribed closed, and draws the gates themselves instead of strands. It uses
the same pulli and validation as the drawing grammar but never changes the
gate assignment used by \cmd{KolamRender}.

\begin{minipage}{0.62\textwidth}
\begin{verbatim}
\begin{kolam}
  \KolamPulli{0}{0} \KolamPulli{1}{0}
  \KolamPulli{0}{1} \KolamPulli{1}{1}
  \KolamDiagramFreeGate{0}{0}{1}{0}
  \KolamDiagramOpenGate{0}{0}{0}{1}
  \KolamDiagramClosedGate{1}{0}{1}{1}
  \KolamDiagramTerminal{0}{1}{W}{$T$}
  \KolamRenderGateDiagram
\end{kolam}
\end{verbatim}
\end{minipage}\hfill
\begin{minipage}{0.3\textwidth}
\centering
\begin{tikzpicture}[kolam grid]
  \begin{kolam}
    \KolamPulli{0}{0}\KolamPulli{1}{0}\KolamPulli{0}{1}\KolamPulli{1}{1}
    \KolamDiagramFreeGate{0}{0}{1}{0}
    \KolamDiagramOpenGate{0}{0}{0}{1}
    \KolamDiagramClosedGate{1}{0}{1}{1}
    \KolamDiagramTerminal{0}{1}{W}{$T$}
    \KolamRenderGateDiagram
  \end{kolam}
\end{tikzpicture}
\end{minipage}

\cmd{KolamDiagramGate}\marg{x}\marg{y}\marg{u}\marg{v}\marg{state} takes the
states \texttt{free}, \texttt{open} and \texttt{closed}. The shorthands are
\begin{verbatim}
\KolamDiagramFreeGate  \KolamDiagramOpenGate  \KolamDiagramClosedGate
\end{verbatim} \cmd{KolamRenderGateDiagram} draws every gate
once: free gates thick and solid, prescribed open gates thin and solid, and
prescribed closed gates (including undeclared ones) thin and dashed.
\cmd{KolamDiagramTerminal}\marg{x}\marg{y}\marg{side}\marg{label} adds a dotted
boundary stub on side \texttt{N}, \texttt{E}, \texttt{S} or \texttt{W} with an
optional label; \verb|\KolamDiagramSetup{terminal length=.43}| sets its
length in grid units.

\section{Styles and extension}

\begin{center}
\begin{tabular}{l l}
\toprule
TikZ style & Use \\
\midrule
\texttt{kolam grid} & the \texttt{x=1cm,y=1cm} grid \\
\texttt{kolam strand} & strands \\
\texttt{kolam dot} & pulli \\
\texttt{kolam diagram dot} & pulli in a partial-instance diagram \\
\texttt{kolam diagram free gate} & a free gate \\
\texttt{kolam diagram prescribed open gate} & a prescribed open gate \\
\texttt{kolam diagram prescribed closed gate} & a prescribed closed gate \\
\texttt{kolam diagram terminal} & a boundary stub \\
\texttt{kolam diagram terminal label} & its label \\
\bottomrule
\end{tabular}
\end{center}

\cmd{KolamPort}\marg{x}\marg{y}\marg{angle} is the coordinate of a tangent
port, for extensions that draw extra geometry. Drawings made only from pulli
and gate declarations stay auditable.

\section{Several drawings in one picture}

Use one \texttt{kolam} environment per drawing; environments do not nest.
Several can share a picture through scopes:

\begin{verbatim}
\begin{tikzpicture}[kolam grid]
  \begin{scope}[shift={(-3,0)}]
    \begin{kolam} ... \KolamRender \end{kolam}
  \end{scope}
  \begin{scope}[shift={(3,0)}]
    \begin{kolam} ... \KolamRender \end{kolam}
  \end{scope}
\end{tikzpicture}
\end{verbatim}

\section{Errors}

Malformed declarations raise a package error and are not stored:
\begin{itemize}
\item a coordinate that is not an integer literal;
\item a pulli or gate declared twice;
\item a gate whose endpoints are not declared, or are not cardinal
  neighbours;
\item a gate state other than \texttt{open} or \texttt{closed} (or
  \texttt{free} in diagrams);
\item a declaration or \cmd{KolamRender} outside a \texttt{kolam}
  environment, a nested environment, or rendering with no pulli;
\item a turn radius outside $0<r<1/2$.
\end{itemize}

\section{Examples}

The repository's \texttt{examples} folder has seven sources, from the
four-pulli seed to the 41-pulli diamond below (1--3--5--7--9--7--5--3--1
rows, three closed strokes).

\begin{center}
\includegraphics[width=0.42\textwidth]{05-one-stroke-21.pdf}\hfill
\includegraphics[width=0.42\textwidth]{07-nine-row-diamond.pdf}
\end{center}

The Margazhi typeface (\url{https://github.com/aalok-thakkar/margazhi}) is
drawn with this package: each letter is a pulli bitmap with its gates, and
the \pkg{margazhi} package draws the letters with \pkg{kolam-tikz}.

\section{License}

\pkg{kolam-tikz} is distributed under the MIT License. Copyright 2026 the
\pkg{kolam-tikz} contributors.

\end{document}
