| 1 |
%% \iffalse |
| 2 |
%% Copyright (C) 2024 Jerome Plut |
| 3 |
%% |
| 4 |
%% This work may be distributed and/or modified under the |
| 5 |
%% conditions of the LaTeX Project Public License, either version 1.3 |
| 6 |
%% of this license or (at your option) any later version. |
| 7 |
%% The latest version of this license is in |
| 8 |
%% https://www.latex-project.org/lppl.txt |
| 9 |
%% and version 1.3c or later is part of all distributions of LaTeX |
| 10 |
%% version 2008-05-04 or later. |
| 11 |
%% |
| 12 |
%% This work has the LPPL maintenance status "maintained". |
| 13 |
%% |
| 14 |
%% The Current Maintainer of this work is Jerome Plut. |
| 15 |
%% |
| 16 |
%% This work consists of the files `euclidean-lattice.ins` |
| 17 |
%% and `euclidean-lattice.dtx`, |
| 18 |
%% as well as the derived file `euclidean-lattice.sty`. |
| 19 |
%% \fi |
| 20 |
|
| 21 |
% \iffalse |
| 22 |
%<*driver> |
| 23 |
\ProvidesFile{euclidean-lattice.dtx} |
| 24 |
%</driver> |
| 25 |
% |
| 26 |
%<*driver> |
| 27 |
\documentclass{ltxdoc} |
| 28 |
\usepackage{color} |
| 29 |
\usepackage{euclidean-lattice} |
| 30 |
\usepackage[listings]{tcolorbox} |
| 31 |
\tcbuselibrary{skins} |
| 32 |
\usepackage[margin=25mm]{geometry} |
| 33 |
\usepackage{amsmath,amsfonts,amssymb} |
| 34 |
\usepackage{titling} |
| 35 |
\setlength{\droptitle}{-30mm} |
| 36 |
|
| 37 |
\sloppy |
| 38 |
\definecolor{blue1}{RGB}{0,53,130} |
| 39 |
\definecolor{red1}{RGB}{218,41,28} |
| 40 |
\definecolor{green1}{RGB}{0,150,59} |
| 41 |
\definecolor{purple1}{RGB}{114,36,108} |
| 42 |
\definecolor{cyan1}{RGB}{242,249,245} |
| 43 |
\definecolor{cyan2}{RGB}{28,133,115} |
| 44 |
\parindent 0pt |
| 45 |
\parskip 1ex |
| 46 |
\EnableCrossrefs |
| 47 |
\CodelineIndex |
| 48 |
\RecordChanges |
| 49 |
\newtcblisting{example}{% |
| 50 |
listing side text,tikz lower={scale=.5}, |
| 51 |
listing options={basicstyle=\ttfamily\fontsize{8pt}{9pt}\selectfont, |
| 52 |
breaklines=true,language=tex,commentstyle=\color{cyan2}\itshape}, |
| 53 |
sharp corners,boxrule=.4pt,righthand width=55mm,boxsep=0mm,left=2mm, |
| 54 |
sidebyside gap=2mm, |
| 55 |
skin=bicolor,colback=cyan1,colframe=black,colbacklower=white, |
| 56 |
} |
| 57 |
\makeatletter |
| 58 |
\def\makecompact#1{\g@addto@macro#1{% |
| 59 |
\setlength{\itemsep}{\z@}\setlength{\parsep}{\z@}% |
| 60 |
\setlength{\topsep}{\z@}\setlength{\partopsep}{\z@}% |
| 61 |
}}\makecompact\itemize \makecompact\enumerate |
| 62 |
\makeatother |
| 63 |
\let\labelitemi\textendash |
| 64 |
|
| 65 |
\begin{document} |
| 66 |
\DocInput{euclidean-lattice.dtx} |
| 67 |
\PrintChanges |
| 68 |
\PrintIndex |
| 69 |
\end{document} |
| 70 |
%</driver> |
| 71 |
%<*doc> |
| 72 |
\fi |
| 73 |
|
| 74 |
\GetFileInfo{euclidean-lattice.sty} |
| 75 |
|
| 76 |
\title{The \textsf{euclidean-lattice} package} |
| 77 |
\author{J\'er\^ome Pl\^ut |
| 78 |
\textlangle\texttt{[email protected]}\textrangle} |
| 79 |
\date{\textsf{euclidean-lattice}~\fileversion, dated~\filedate} |
| 80 |
|
| 81 |
|
| 82 |
\maketitle |
| 83 |
|
| 84 |
This package provides a simple and efficient way |
| 85 |
of drawing \textsf{TikZ} pictures of two-dimensional Euclidean lattices. |
| 86 |
|
| 87 |
\section{Usage} |
| 88 |
|
| 89 |
\subsection{The \texttt{\textbackslash lattice} command} |
| 90 |
|
| 91 |
\DescribeMacro{\lattice} |
| 92 |
{\color{red1}|\lattice|}% |
| 93 |
{\color{green1}|<|\textit{overlay specification}|>|}% |
| 94 |
{\color{blue1}|[|\textit{options...}|]|}% |
| 95 |
|(|$a$|,|$b$|)(|$c$|,|$d$|);| |
| 96 |
This commands draws a part of the two-dimensional lattice |
| 97 |
generated by the vectors $(a,b)$ and $(c,d)$. |
| 98 |
|
| 99 |
The {\color{green1}\textit{overlay specification}} is optional |
| 100 |
and follows~\textsf{beamer} syntax. |
| 101 |
It will only work if the \textsf{beamer} class is loaded. |
| 102 |
|
| 103 |
The {\color{blue1}\textit{options}} enable customizing how the lattice |
| 104 |
is displayed. They follow the standard \textsf{TikZ/PGF} key-value |
| 105 |
interface. The following options are available. |
| 106 |
|
| 107 |
\DescribeMacro{x} |
| 108 |
\DescribeMacro{y} |
| 109 |
{{\color{blue1}\texttt{x=}$x_1$|:|$x_2$} |
| 110 |
and {\color{blue1}\texttt{y=}$y_1$|:|$y_2$}} |
| 111 |
(default value |x=-2:2|,|y=-2:2|). |
| 112 |
|
| 113 |
These two options specify a bounding box which determines |
| 114 |
which part of the lattice is drawn. |
| 115 |
As a shortcut, passing the value |x=|$x_1$ |
| 116 |
is equivalent to |x=|$-x_1$|:|$x_1$, and likewise for~|y|. |
| 117 |
\begin{example} |
| 118 |
\draw[help lines,gray!25] (-5,0) grid (5,2); |
| 119 |
\lattice[x=5,y=0:2](3,0)(1,1); |
| 120 |
\node[inner sep=1.5pt,circle,fill=red] at (0,0){}; |
| 121 |
\end{example} |
| 122 |
|
| 123 |
\DescribeMacro{grid} |
| 124 |
{{\color{blue1}\texttt{grid=}\textit{options...}}} |
| 125 |
|
| 126 |
This option specifies how to draw |
| 127 |
the grid generated by the two given lattice vectors. |
| 128 |
It may take either a single value (generally a color name), |
| 129 |
or several values grouped in braces. |
| 130 |
Note that this grid depends on the basis vectors and not only on the |
| 131 |
lattice; see the example below. |
| 132 |
\begin{example} |
| 133 |
\lattice[y=0:2,x=0:5,grid=gray](2,-1)(1,1); |
| 134 |
% Same lattice, different basis: |
| 135 |
\lattice[y=0:2,x=6:11,grid={yellow,very thick}] |
| 136 |
(3,0)(4,1); |
| 137 |
\end{example} |
| 138 |
|
| 139 |
\DescribeMacro{bounding box} |
| 140 |
{{\color{blue1}\texttt{bounding box=}\textit{options...}}} |
| 141 |
|
| 142 |
This option specifies how to draw |
| 143 |
the bounding box given by the |x| and |y| options. |
| 144 |
It may take either a single value (generally a color name), |
| 145 |
or several values grouped in braces. |
| 146 |
\begin{example} |
| 147 |
\lattice[bounding box=green,x=-5:5,y=0:2](3,0)(1,1); |
| 148 |
\end{example} |
| 149 |
|
| 150 |
\DescribeMacro{each point} |
| 151 |
{{\color{blue1}\texttt{each point/.code n args=\{5\}\{}% |
| 152 |
\textit{code...}\texttt{\}}}} |
| 153 |
|
| 154 |
This option contains contains the code to execute for each |
| 155 |
lattice point. This code is called with the five following parameters: |
| 156 |
\begin{itemize} |
| 157 |
\item (|#1|) the parsed lattice node options, |
| 158 |
\item (|#2|,|#3|) the $(x,y)$ coordinates of the current lattice point |
| 159 |
(as \textsf{TikZ} canvas coordinates); |
| 160 |
\item (|#4|,|#5|) the coordinates of the same point |
| 161 |
relative to the provided lattice basis (as integers). |
| 162 |
\end{itemize} |
| 163 |
Note that, due to this parameter being expanded inside a macro, all the |
| 164 |
|#| signs must be doubled in its definition, as in the following example. |
| 165 |
\begin{example} |
| 166 |
\lattice[x=-5:5,y=0:2,each point/.code n args= |
| 167 |
{5}{\node[##1,fill=cyan!10] at (##2,##3){(##4,##5)};}] |
| 168 |
(3,0)(1,1); |
| 169 |
\end{example} |
| 170 |
|
| 171 |
|
| 172 |
\textbf{Other options} |
| 173 |
|
| 174 |
Any remaining options are directly passed to |
| 175 |
the |\node| calls for each lattice point. |
| 176 |
\begin{example} |
| 177 |
\lattice[red,rectangle,inner sep=2pt,x=-5:5,y=0:2] |
| 178 |
(3,0)(1,1); |
| 179 |
\end{example} |
| 180 |
|
| 181 |
|
| 182 |
\subsection{Configuring default behaviour} |
| 183 |
|
| 184 |
\DescribeMacro{x} |
| 185 |
\DescribeMacro{y} |
| 186 |
\DescribeMacro{grid} |
| 187 |
\DescribeMacro{bounding box} |
| 188 |
\DescribeMacro{each point} |
| 189 |
The |/lattice/x|, |/lattice/y|, |/lattice/grid|, |/lattice/bounding box|, |
| 190 |
|/lattice/each point| PGF keys contain |
| 191 |
the default parameters for the correspondingly-named options. |
| 192 |
Note that the |#| signs must \emph{not} be doubled in |/lattice/each point|. |
| 193 |
\begin{example} |
| 194 |
\pgfqkeys{/lattice}{x=-5:5,y=-.1:2.1, |
| 195 |
grid={blue!10,very thick}, |
| 196 |
bounding box={red,fill=gray!5}, |
| 197 |
each point/.code n args={5}{% |
| 198 |
\node[#1,fill=cyan!10,rectangle] at (#2,#3) |
| 199 |
{\tiny (#4,#5)};}} |
| 200 |
\lattice[grid,bounding box](3,0)(1,1); |
| 201 |
\end{example} |
| 202 |
|
| 203 |
\DescribeMacro{node} |
| 204 |
The |/lattice/node| key contains the default parameters for the |\node| |
| 205 |
call for each point. |
| 206 |
Equivalently, these are the default options passed to each |\lattice| call. |
| 207 |
\begin{example} |
| 208 |
\pgfkeys{/lattice/node={fill=yellow!25,draw=red}} |
| 209 |
\lattice[x=-5:5,y=0:2](3,0)(1,1); |
| 210 |
\end{example} |
| 211 |
|
| 212 |
\subsection{Exactness and limitations} |
| 213 |
|
| 214 |
This command only outputs lattices containing the origin. |
| 215 |
An offset lattice may still be obtained by using |
| 216 |
the PGF transformation mechanism |
| 217 |
and correspondingly adjusting the |x|,|y| bounding box parameters: |
| 218 |
\begin{example} |
| 219 |
\begin{scope}\pgftransformshift{\pgfpoint{1cm}{0}} |
| 220 |
\lattice[x=-3:1,red,rectangle](2,0)(1,1); |
| 221 |
\end{scope} |
| 222 |
\lattice(2,0)(1,1); |
| 223 |
\end{example} |
| 224 |
|
| 225 |
|
| 226 |
This command might be executed a large number of times in a single |
| 227 |
document, for example in the case of a \textsf{beamer} presentation |
| 228 |
with a large number of overlays. |
| 229 |
This means that we need to be careful to make it efficient. |
| 230 |
|
| 231 |
Therefore, all computations are performed using \TeX\ registers. |
| 232 |
Since these registers only implement 32-bit integer arithmetic, |
| 233 |
we use fixed-point arithmetic with 16-bit offset for numeric computations |
| 234 |
(which include matrix inversion). |
| 235 |
This implies some precision loss and enforces some numeric limits. |
| 236 |
The use of fixed-point arithmetic means that |
| 237 |
the products appearing in the determinant of the lattice |
| 238 |
must not exceed $2^{15}$. |
| 239 |
In particular, this package should work |
| 240 |
as long as all the coordinates (in absolute value) do not exceed~$128$, |
| 241 |
which should cover most typical scales of \textsf{TikZ} pictures. |
| 242 |
|
| 243 |
We compensate for the precision loss by sampling a bit too many |
| 244 |
lattice points (relative to the required bounding box) |
| 245 |
and then filtering \emph{a posteriori} to ensure that all points |
| 246 |
are in the bounding box. |
| 247 |
This last step should remain exact, at least as soon as |
| 248 |
the coordinates of the lattice vectors are integers |
| 249 |
(or integer multiples of $2^{-8}$). |
| 250 |
|
| 251 |
Therefore, we expect that in most cases, the command will exactly output |
| 252 |
the lattice points inside the bounding box. |
| 253 |
If this were to fail, however, as a last-resort option, |
| 254 |
enlarging the bounding box by a small number in all directions |
| 255 |
should ensure that all lattice points appear. |
| 256 |
|
| 257 |
%\iffalse |
| 258 |
%</doc> |
| 259 |
%\fi |
| 260 |
% \section{Implementation} |
| 261 |
% |
| 262 |
% \paragraph{Initialization.} |
| 263 |
% We obviously depend on~\textsf{tikz} |
| 264 |
% (and more precisely on~\textsf{pgfkeys}). |
| 265 |
% \begin{macrocode} |
| 266 |
\NeedsTeXFormat{LaTeX2e} |
| 267 |
\ProvidesPackage{euclidean-lattice}[2024-07-26 Euclidean~lattices 1.0] |
| 268 |
\RequirePackage{tikz} |
| 269 |
% \end{macrocode} |
| 270 |
% We use fixed-point arithmetic in the 31-bit registers supported by |
| 271 |
% \TeX, using 16 bits for offset. |
| 272 |
% |
| 273 |
% TODO: we could slightly enhance the precision of computations by |
| 274 |
% correctly rounding the last bit (instead of truncating). |
| 275 |
% Depending on the value's sign, |
| 276 |
% |\advance XXX by .5\la@unit| should do the trick. |
| 277 |
% |
| 278 |
% This means that we represent a real number~$x$ |
| 279 |
% using the integer $f(x) = B x$, where $B = 2^{16}$. |
| 280 |
% To multiply two such integers, we note that |
| 281 |
% $f(xy) = B x y = f(x) f(y)/B$, |
| 282 |
% which we compute as $(f(x)/\beta) \cdot (f(y)/\beta)$, |
| 283 |
% where $\beta = \sqrt{B} = 2^{8}$. |
| 284 |
% We store this value in the register |\la@unit|. |
| 285 |
% \begin{macrocode} |
| 286 |
\newdimen\la@unit \la@unit=256sp |
| 287 |
\def\lattice@mul#1#2{% writes #1*#2 in #1 |
| 288 |
\@tempdimc=#2 \divide\@tempdimc\la@unit |
| 289 |
\divide #1\la@unit |
| 290 |
\multiply #1\@tempdimc}% |
| 291 |
% \end{macrocode} |
| 292 |
% We perform division using |
| 293 |
% $f(x/y) = B x/y = B f(x)/f(y)$, |
| 294 |
% which we compute as $\frac{\beta f(x)}{f(y)/\beta}$. |
| 295 |
% \begin{macrocode} |
| 296 |
\def\lattice@div#1#2{% |
| 297 |
\@tempdimc=#2\divide\@tempdimc\la@unit |
| 298 |
\multiply #1\la@unit \divide#1\@tempdimc}% |
| 299 |
\def\lattice@invert#1{% writes 1/#1 in #1 |
| 300 |
\@tempdimc=#1 \divide\@tempdimc\la@unit |
| 301 |
#1=256\p@ \divide #1\@tempdimc}% |
| 302 |
% \end{macrocode} |
| 303 |
% The next macro is our main enumeration routine. |
| 304 |
% It enumerates all the point in the lattice~$\Lambda$ generated |
| 305 |
% by the vectors $(a=|#1|,b=|#2|)$ and $(c=|#3|,d=|#4|)$ |
| 306 |
% inside the box~$R$ delimited by the registers |
| 307 |
% $[|\la@Ax|,|\la@Bx|] \times [|\la@Ay|,|\la@By|]$. |
| 308 |
% On each such point, it invokes the macro |
| 309 |
% |\lattice@donode|. |
| 310 |
% |
| 311 |
% Since $\Lambda = B \cdot \mathbb{Z}^2$, |
| 312 |
% instead of enumerating $S = \Lambda \cap R$ |
| 313 |
% we enumerate $B^{-1} S = \mathbb{Z}^2 \cap (B^{-1} R)$. |
| 314 |
% We use $(x,y)$ coordinates to describe elements of~$S$ |
| 315 |
% and $(w,z)$ coordinates to describes elements of~$B^{-1} S$. |
| 316 |
% |
| 317 |
% We declare several numeric registers, all prefixed with |\la@|: |
| 318 |
% \begin{itemize} |
| 319 |
% \item $(a,b)$ and $(c,d)$ are the vectors of the lattice basis~$B$; |
| 320 |
% \item $R := [Ax, Bx] \times [Ay, By]$ is the bounding box for the drawing; |
| 321 |
% \item $U := (Ux, Uy)$ and $V := (Vx, Vy)$ are the side vectors |
| 322 |
% of the parallelogram $P := B^{-1} \cdot R$, |
| 323 |
% and~$C := (Cx, Cy)$~is its center; |
| 324 |
% \item $sM < sL$ are the slopes of the vectors $U$ and~$V$; |
| 325 |
% \item $wA, wB, wM$ are the horizontal parameters for enumerating |
| 326 |
% the parallelogram; |
| 327 |
% \item $zA, zB, zC$ are the vertical parameters for enumerating the |
| 328 |
% parallelogram; |
| 329 |
% \item $D$ plays a double role, first as the (inverse) determinant of |
| 330 |
% the lattice, and then as a vertical enumeration parameter. |
| 331 |
% \end{itemize} |
| 332 |
% \begin{macrocode} |
| 333 |
\newdimen\la@a \newdimen\la@b \newdimen\la@c \newdimen\la@d |
| 334 |
\newdimen\la@Ax\newdimen\la@Bx\newdimen\la@Ay\newdimen\la@By |
| 335 |
\newdimen\la@D |
| 336 |
\newdimen\la@Ux\newdimen\la@Uy \newdimen\la@Vx\newdimen\la@Vy |
| 337 |
\newdimen\la@Cx\newdimen\la@Cy |
| 338 |
|
| 339 |
\newdimen\la@sL \newdimen\la@sM |
| 340 |
\newdimen\la@zA \newdimen\la@zB \newdimen\la@zC |
| 341 |
\newdimen\la@wA \newdimen\la@wB |
| 342 |
\newcount\la@wM |
| 343 |
% \end{macrocode} |
| 344 |
% \begin{macrocode} |
| 345 |
\def\lattice@enumerate#1#2#3#4{% |
| 346 |
\la@a=#1\p@ \la@b=#2\p@ \la@c=#3\p@ \la@d=#4\p@ |
| 347 |
% \end{macrocode} |
| 348 |
% First we store the \textrm{inverse} of the determinant in |\la@D|. |
| 349 |
% \begin{macrocode} |
| 350 |
\la@D=\la@a \lattice@mul\la@D\la@d |
| 351 |
\@tempdima=\la@b \lattice@mul\@tempdima\la@c |
| 352 |
\advance\la@D -\@tempdima |
| 353 |
\lattice@invert\la@D |
| 354 |
% \end{macrocode} |
| 355 |
% Compute the inverse image $B^{-1} R = P$. |
| 356 |
% We first compute the sides $U$, $V$ of the parallelogram, |
| 357 |
% multiplying by $-1$ if needed so that both $w$-coordinates are |
| 358 |
% $\geqslant 0$. |
| 359 |
% \begin{macrocode} |
| 360 |
\la@Ux=\la@Ax \advance\la@Ux-\la@Bx |
| 361 |
\lattice@mul\la@Ux \la@D |
| 362 |
\la@Uy=-\la@Ux |
| 363 |
\lattice@mul\la@Ux \la@d \ifdim\la@Ux <\z@ |
| 364 |
\multiply\la@Ux -1 \multiply\la@Uy -1 \fi |
| 365 |
\lattice@mul\la@Uy\la@b |
| 366 |
\la@Vx=\la@By \advance\la@Vx-\la@Ay \lattice@mul\la@Vx\la@D |
| 367 |
\la@Vy=-\la@Vx |
| 368 |
\lattice@mul\la@Vx\la@c \ifdim\la@Vx<\z@ |
| 369 |
\multiply\la@Vx -1 \multiply\la@Vy -1\fi |
| 370 |
\lattice@mul\la@Vy \la@a |
| 371 |
% \end{macrocode} |
| 372 |
% We ensure that $\det(U,V) > 0$, swapping both vectors if needed. |
| 373 |
% Since we only need the sign of this determinant, we scale down the |
| 374 |
% values to help prevent a numeric overflow. |
| 375 |
% \begin{macrocode} |
| 376 |
\@tempdima \la@Ux |
| 377 |
\@tempdimb \la@Vx |
| 378 |
\ifdim\@tempdima<100\p@\else |
| 379 |
\ifdim\@tempdimb<100\p@\else |
| 380 |
\divide\@tempdima 1024 |
| 381 |
\divide\@tempdimb 1024\fi\fi |
| 382 |
\lattice@mul\@tempdima\la@Vy |
| 383 |
\lattice@mul\@tempdimb\la@Uy |
| 384 |
\ifdim \@tempdimb>\@tempdima |
| 385 |
\@tempdimc=\la@Ux \la@Ux=\la@Vx \la@Vx=\@tempdimc |
| 386 |
\@tempdimc=\la@Uy \la@Uy=\la@Vy \la@Vy=\@tempdimc |
| 387 |
\fi |
| 388 |
% \end{macrocode} |
| 389 |
% Compute the center of the parallelogram. |
| 390 |
% Note that we also use |\la@sL|, |\la@sM| (not yet needed) |
| 391 |
% as temporaries here. |
| 392 |
% \begin{macrocode} |
| 393 |
\la@sL=\la@Ax \advance\la@sL\la@Bx \divide\la@sL 2 |
| 394 |
\la@sM=\la@Ay \advance\la@sM\la@By \divide\la@sM 2 |
| 395 |
\la@Cx=\la@sL \lattice@mul\la@Cx\la@d |
| 396 |
\@tempdima=\la@sM \lattice@mul\@tempdima\la@c |
| 397 |
\advance\la@Cx -\@tempdima |
| 398 |
\la@Cy=\la@sM \lattice@mul\la@Cy\la@a |
| 399 |
\@tempdima=\la@sL \lattice@mul\@tempdima\la@b |
| 400 |
\advance\la@Cy -\@tempdima |
| 401 |
\lattice@mul \la@Cx\la@D |
| 402 |
\lattice@mul \la@Cy\la@D |
| 403 |
% \end{macrocode} |
| 404 |
% Compute the bounding-box in $(w,z)$ space. |
| 405 |
% Note that the |\la@wA|,|\la@wB| values will be re-used below |
| 406 |
% for enumerating all lattice points; |
| 407 |
% on the other hand, |\la@zA| and |\la@zB| are used here |
| 408 |
% as temporary values. |
| 409 |
% \begin{macrocode} |
| 410 |
\@tempdima\la@Ux \advance\@tempdima\la@Vx |
| 411 |
\la@wA=\la@Cx \advance\la@wA -.5\@tempdima |
| 412 |
\la@wB=\la@wA \advance\la@wB \@tempdima |
| 413 |
\@tempdima\la@Uy \ifdim\@tempdima<\z@ \multiply\@tempdima -1\fi |
| 414 |
\ifdim\la@Vy<\z@ \advance\@tempdima -\la@Vy |
| 415 |
\else\advance\@tempdima\la@Vy\fi |
| 416 |
\la@zA=\la@Cy \advance\la@zA -.5\@tempdima |
| 417 |
\la@zB=\la@zA \advance\la@zB \@tempdima |
| 418 |
% \end{macrocode} |
| 419 |
% Draw the grid if the options require it. |
| 420 |
% \begin{macrocode} |
| 421 |
\ifx\lattice@grid\@empty\else \begin{scope}% |
| 422 |
\clip (\strip@pt\la@Ax,\strip@pt\la@Ay) |
| 423 |
rectangle (\strip@pt\la@Bx,\strip@pt\la@By); |
| 424 |
\pgftransformcm{\strip@pt\la@a}{\strip@pt\la@b}% |
| 425 |
{\strip@pt\la@c}{\strip@pt\la@d}{\pgfpointorigin}% |
| 426 |
\expandafter\draw\expandafter[\lattice@grid] |
| 427 |
(\strip@pt\la@wA,\strip@pt\la@zA) grid (\strip@pt\la@wB,\strip@pt\la@zB); |
| 428 |
\end{scope}\fi |
| 429 |
% \end{macrocode} |
| 430 |
% Compute the last values required to enumerate over $\mathbb{Z}^2 \cap P$. |
| 431 |
% We represent the parallelogram~$P$ as the intersection of four half-planes. |
| 432 |
% Let $A_1=(w_1,z_1)$ and $A_2=(w_2,z_2)$ be |
| 433 |
% the left-most and right-most vertices of~$P$, |
| 434 |
% and $\lambda, \mu$ be the slopes of the sides |
| 435 |
% (the condition $\det(U,V) > 0$ guarantees that $\lambda < \mu$). |
| 436 |
% |
| 437 |
% The two sides originating from~$A_i (i=1,2)$ have equations |
| 438 |
% \[ z=z_i+\lambda(w-w_i), \qquad z=z_i+\mu(w-w_i); \] |
| 439 |
% By defining $z_3 = z_2 - \lambda (w_2 - w_1)$ and $z_4=z_2-\mu(w_2-w_1)$, |
| 440 |
% we may rewrite both equations from~$A_2$ as: |
| 441 |
% \[ z= z_3 + \mu(w-w_1), |
| 442 |
% \qquad z = z_4 + \lambda(w-w_1) \] |
| 443 |
% The parallelogram~$P$ is then defined by the following inequations, |
| 444 |
% where we write $t = w - w_1$: |
| 445 |
% \[ \max(z_3+\mu t, z_1+\lambda t) \;\leqslant\; z \;\leqslant\; |
| 446 |
% \min(z_1+\mu t, z_4+\lambda t). \] |
| 447 |
% For any given value of~$w$ we store the value of these four affine |
| 448 |
% functions in the variables |\la@zB|, |\la@zA|, |\la@D| (not a typo), |
| 449 |
% |\la@zC|, in this order. |
| 450 |
% When increasing the value of~$w$ we only need to increase the values |
| 451 |
% of these four registers respectively by $\mu$, $\lambda$, $\mu$, |
| 452 |
% $\lambda$. |
| 453 |
% |
| 454 |
% First we compute the values $z_2 = z_C + \frac 12(z_U+z_V)$ |
| 455 |
% and~$w_2 - w_1 = w_U + w_V$. |
| 456 |
% \begin{macrocode} |
| 457 |
\@tempdima=\la@Uy \advance\@tempdima\la@Vy |
| 458 |
\@tempdimb=\la@Cy \advance\@tempdimb .5\@tempdima % z2 |
| 459 |
\@tempdima=\la@Ux \advance\@tempdima\la@Vx % w_2-w_1 = w(U+V) |
| 460 |
% \end{macrocode} |
| 461 |
% Then we compute the slopes $\lambda = |\la@sL|$ and $\mu = |\la@sM|$. |
| 462 |
% A Special case happens when either $w_U$ or $w_V$ is zero. |
| 463 |
% In the first case, $\lambda = -\infty$. |
| 464 |
% This means that the comparisons with |\la@zA| and |\la@zC| must be |
| 465 |
% ignored. |
| 466 |
% \begin{macrocode} |
| 467 |
\ifdim\la@Ux=\z@ |
| 468 |
\la@sL=\z@ \la@zC=\maxdimen \la@zA=-\maxdimen |
| 469 |
\else |
| 470 |
\la@sL=\la@Uy \lattice@div\la@sL\la@Ux |
| 471 |
\la@zC=-\@tempdima \lattice@mul\la@zC\la@sL \advance\la@zC \@tempdimb |
| 472 |
\la@zA=\@tempdimb \advance\la@zA -\la@Uy \advance\la@zA -\la@Vy |
| 473 |
\fi |
| 474 |
% \end{macrocode} |
| 475 |
% Likewise, if $w_V = 0$ then $\mu = +\infty$ |
| 476 |
% and the comparisons with |\la@zB| and |\la@D| are ignored. |
| 477 |
% \begin{macrocode} |
| 478 |
\ifdim\la@Vx=\z@ |
| 479 |
\la@sM=\z@ \la@zB=-\maxdimen \la@D=\maxdimen |
| 480 |
\else |
| 481 |
\la@sM=\la@Vy \lattice@div\la@sM\la@Vx |
| 482 |
\la@zB=-\@tempdima \lattice@mul\la@zB\la@sM \advance\la@zB \@tempdimb |
| 483 |
\la@D=\@tempdimb \advance\la@D -\la@Uy \advance\la@D -\la@Vy |
| 484 |
\fi |
| 485 |
% \end{macrocode} |
| 486 |
% |
| 487 |
% At this point the enumerator for $B^{-1} S$ is fully computed; |
| 488 |
% it uses the registers |\la@sL|, |\la@sM| for the slopes, |
| 489 |
% |\la@wA|, |\la@wB| for the boundaries, |
| 490 |
% |\la@zA|, |\la@zB|, |\la@zC|, |\la@D| for the four affine functions, |
| 491 |
% as well as the original matrix~$B$ in |\la@a|, |\la@b|, |\la@c|, |\la@d| |
| 492 |
% (to recompute the points of~$\Lambda$). |
| 493 |
% |
| 494 |
% We re-use the freed registers in the following way: |
| 495 |
% \begin{itemize} |
| 496 |
% \item $(|\la@Vx|,|\la@Vy|)$ hold the $(w,z)$ coordinates |
| 497 |
% of the current lattice point (stored as integers); |
| 498 |
% \item $(|\la@Ux|,|\la@Uy|)$ hold the corresponding point |
| 499 |
% in $(x,y)$ space (stored as \textsf{dimen}s); |
| 500 |
% \item $(|\la@Cx|,|\la@Cy|)$ hold the $(x,y)$ coordinates |
| 501 |
% of the $(w,0)$ point, which is used for each column. |
| 502 |
% \end{itemize} |
| 503 |
% |
| 504 |
% Because the fixed-point arithmetic is imprecise, we might miss some |
| 505 |
% lattice points on the boundaries. We compensate for this by enlarging |
| 506 |
% the bounds ($-1$ for lower bounds, $+1$ for upper bounds) |
| 507 |
% and \emph{a posteriori} checking that all the computed points |
| 508 |
% are in the bounding box. |
| 509 |
% |
| 510 |
% Store in |\la@Vx| our starting $(w,z)$ coordinate, |
| 511 |
% which is the integer $\lceil|\la@wA|\rceil -1$. |
| 512 |
% Since \TeX\ knows only \emph{signed} division (groumpf), |
| 513 |
% computing the ceiling is sign-dependent. |
| 514 |
% \begin{macrocode} |
| 515 |
\la@Vx=\la@wA \advance\la@Vx\expandafter\ifdim\la@Vx>\z@ -1sp\else-\p@\fi |
| 516 |
\divide\la@Vx \p@ |
| 517 |
% \end{macrocode} |
| 518 |
% Compute the upper boundary $|\la@wB|=\lfloor|\la@wB|\rfloor+1$. |
| 519 |
% \begin{macrocode} |
| 520 |
\ifdim\la@wB<\z@ \advance\la@wB 1sp\else\advance\la@wB\p@\fi |
| 521 |
\divide\la@wB\p@ |
| 522 |
% \end{macrocode} |
| 523 |
% Compute the difference $t = \lceil w_1\rceil-w_1$ |
| 524 |
% and correspondingly advance the four affine functions. |
| 525 |
% \begin{macrocode} |
| 526 |
\@tempdima=\la@Vx \multiply\@tempdima\p@ |
| 527 |
\advance\@tempdima -\la@wA |
| 528 |
\@tempdimb=\@tempdima \lattice@mul\@tempdimb\la@sL |
| 529 |
\advance\la@zA\@tempdimb \advance\la@zC\@tempdimb |
| 530 |
\@tempdimb=\@tempdima \lattice@mul\@tempdimb\la@sM |
| 531 |
\advance\la@zB\@tempdimb \advance\la@D\@tempdimb |
| 532 |
% \end{macrocode} |
| 533 |
% Store in |\la@Cx|, |\la@Cy| the $(x,y)$ coordinates of the |
| 534 |
% lattice vector $w \cdot (a,b)$: |
| 535 |
% \begin{macrocode} |
| 536 |
\la@Cx=\la@a \multiply\la@Cx\la@Vx |
| 537 |
\la@Cy=\la@b \multiply\la@Cy\la@Vx |
| 538 |
% \end{macrocode} |
| 539 |
% The main $w$-loop starts here. |
| 540 |
% \begin{macrocode} |
| 541 |
\loop |
| 542 |
% \end{macrocode} |
| 543 |
% Compute the range of $z=|\la@Vy|$ for this value of $w=|\la@Vx|$. |
| 544 |
% The minimum value is $\max(|\la@zA|,|\la@zB|)-1$: |
| 545 |
% \begin{macrocode} |
| 546 |
\la@Vy=\la@zA \ifdim\la@Vy<\la@zB \la@Vy=\la@zB\fi |
| 547 |
\advance\la@Vy\expandafter\ifdim\la@Vy>\z@ -1sp\else-\p@\fi |
| 548 |
\divide\la@Vy\p@ |
| 549 |
% \end{macrocode} |
| 550 |
% The maximum value is $|\la@wM|=\min(|\la@zC|,|\la@D|)$: |
| 551 |
% \begin{macrocode} |
| 552 |
\la@wM=\la@zC \ifnum\la@wM>\la@D \la@wM=\la@D\fi |
| 553 |
\advance\la@wM\expandafter\ifnum\la@wM<\z@\@ne\else\p@\fi |
| 554 |
\divide\la@wM\p@ |
| 555 |
% \end{macrocode} |
| 556 |
% Compute the $(|\la@Ux|,|\la@Uy|)$ point in $(x,y)$ space: |
| 557 |
% \begin{macrocode} |
| 558 |
\la@Ux=\la@c \multiply\la@Ux\la@Vy \advance\la@Ux\la@Cx |
| 559 |
\la@Uy=\la@d \multiply\la@Uy\la@Vy \advance\la@Uy\la@Cy |
| 560 |
% \end{macrocode} |
| 561 |
% Inner ($z$) loop. Nested loops require grouping. |
| 562 |
% \begin{macrocode} |
| 563 |
{\loop |
| 564 |
% \end{macrocode} |
| 565 |
% If the point is inside the bounding box, we invoke |\lattice@donode|. |
| 566 |
% Note the use of |\expandafter| to ensure that the |\node| call inside |
| 567 |
% the PGF key |/lattice/each node| gets the expanded options list. |
| 568 |
% \begin{macrocode} |
| 569 |
\ifdim\la@Ux<\la@Ax\else\ifdim\la@Ux>\la@Bx\else |
| 570 |
\ifdim\la@Uy<\la@Ay\else\ifdim\la@Uy>\la@By\else |
| 571 |
\expandafter\lattice@donode\expandafter{\lattice@node}% |
| 572 |
\fi\fi\fi\fi |
| 573 |
% \end{macrocode} |
| 574 |
% End of the $z$ loop. We increase $z$ and correspondingly advance the vector. |
| 575 |
% \begin{macrocode} |
| 576 |
\ifnum\la@Vy<\la@wM |
| 577 |
\advance\la@Vy 1sp |
| 578 |
\advance\la@Ux\la@c \advance\la@Uy\la@d |
| 579 |
\repeat} |
| 580 |
% \end{macrocode} |
| 581 |
% End of the $w$ loop. We increase $w$ and correspondingly advance |
| 582 |
% the $w\cdot (a,b)$ vector as well as the four affine functions. |
| 583 |
% \begin{macrocode} |
| 584 |
\ifnum\la@Vx<\la@wB |
| 585 |
\advance\la@Vx 1sp |
| 586 |
\advance\la@Cx\la@a \advance\la@Cy\la@b |
| 587 |
\advance\la@zA \la@sL \advance\la@zC \la@sL |
| 588 |
\advance\la@zB \la@sM \advance\la@D \la@sM |
| 589 |
\repeat |
| 590 |
} |
| 591 |
% \end{macrocode} |
| 592 |
% This macro gets called for each found lattice point. |
| 593 |
% Its main job is to ensure that the node options (here |#1|) |
| 594 |
% are correctly expanded. |
| 595 |
% \begin{macrocode} |
| 596 |
\def\lattice@donode#1{% |
| 597 |
\pgfkeys{/lattice/arg/each point={#1}{\strip@pt\la@Ux}{\strip@pt\la@Uy}% |
| 598 |
{\number\la@Vx}{\number\la@Vy}}} |
| 599 |
|
| 600 |
% \end{macrocode} |
| 601 |
% The PGF keys used for parsing the arguments of the |\lattice| command; |
| 602 |
% they correspond to the options passed in square brackets. |
| 603 |
% \begin{macrocode} |
| 604 |
\pgfqkeys{/lattice/arg}{ |
| 605 |
x/.store in=\lattice@x, |
| 606 |
y/.store in=\lattice@y, |
| 607 |
grid/.store in=\lattice@grid, |
| 608 |
each point/.code n args={5}{\pgfkeys{/lattice/each point={#1}{#2}{#3}{#4}{#5}}}, |
| 609 |
bounding box/.store in=\lattice@bbox, |
| 610 |
.unknown/.code={% |
| 611 |
\expandafter\lattice@setnode\pgfkeyscurrentkey=#1\lattice@eov |
| 612 |
}} |
| 613 |
% \end{macrocode} |
| 614 |
% The PGF keys used for user configuration of the default values. |
| 615 |
% \begin{macrocode} |
| 616 |
\pgfqkeys{/lattice}{ |
| 617 |
x/.initial=-2:2,y/.initial=-2:2, |
| 618 |
node/.initial={circle,inner sep=1pt,draw=none,fill=black}, |
| 619 |
grid/.style={/lattice/arg/grid/.default={#1}}, |
| 620 |
grid={gray,very thin}, |
| 621 |
bounding box/.style={/lattice/arg/bounding box/.default={#1}}, |
| 622 |
bounding box={cyan,thin}, |
| 623 |
each point/.code n args={5}{\node[#1] at (#2,#3){};}, |
| 624 |
} |
| 625 |
% \end{macrocode} |
| 626 |
% This handles the passing of any unknown |\lattice| keys |
| 627 |
% down to the |\node| calls. |
| 628 |
% \begin{macrocode} |
| 629 |
\def\lattice@setnode/lattice/arg/#1\lattice@eov{% |
| 630 |
\edef\lattice@node{\expandafter\noexpand\lattice@node,#1}} |
| 631 |
% \end{macrocode} |
| 632 |
% This macro parses the bounding-box |x| and |y| coordinates, |
| 633 |
% replacing |x=10| by |x=-10:10|. |
| 634 |
% The |detokenize| trick was nicely provided by David Carlisle: |
| 635 |
% |https://tex.stackexchange.com/questions/724989/|. |
| 636 |
% \begin{macrocode} |
| 637 |
\def\lattice@getxy#1#2#3{% |
| 638 |
\def\m@gic##1:##2:##3\end{\ifx ##3: #2=##1\p@ #3=##2\p@\else |
| 639 |
#2=-##1\p@ #3=##1\p@\fi}% |
| 640 |
\expandafter\m@gic\detokenize{#1:}:\end |
| 641 |
} |
| 642 |
% \end{macrocode} |
| 643 |
% The following macros parse the possible combinations of overlays |
| 644 |
% and square-bracket options. |
| 645 |
% They are similar to the way |\tikz@command@path| is defined |
| 646 |
% in |tikz.code.tex|. |
| 647 |
% Just as in that case, the overlay is handled by a |\alt| call, |
| 648 |
% which will fail if \textsf{beamer} is not loaded. |
| 649 |
% \begin{macrocode} |
| 650 |
\def\lattice{\@ifnextchar<\lattice@I{\@ifnextchar[\lattice@II{\lattice@@[]}}} |
| 651 |
\def\lattice@I{\ifnum\the\catcode`\;=\active\relax |
| 652 |
\let\lattice@next\lattice@Iactive\else |
| 653 |
\let\lattice@next\lattice@Inormal\fi |
| 654 |
\lattice@next} |
| 655 |
\long\def\lattice@Inormal<#1>#2;{\alt<#1>{\lattice@I@#2;}{}} |
| 656 |
{\catcode`\;=\active |
| 657 |
\long\gdef\lattice@Iactive<#1>#2;{\alt<#1>{\lattice@I@#2;}{}} |
| 658 |
} |
| 659 |
\def\lattice@I@{\@ifnextchar[\lattice@@{\lattice@@[]}} |
| 660 |
\def\lattice@II[#1]{\@ifnextchar<{\lattice@IIi[#1]}{\lattice@@[#1]}} |
| 661 |
\def\lattice@IIi[#1]<#2>{\lattice@I<#2>[#1]} |
| 662 |
% \end{macrocode} |
| 663 |
% This macros parses the basis vectors and calls the main loop. |
| 664 |
% |#4| and |#7| are dummy parameters swallowing a possible space between |
| 665 |
% the parentheses and the final semicolon. |
| 666 |
% \begin{macrocode} |
| 667 |
\def\lattice@@[#1](#2,#3)#4(#5,#6)#7{ |
| 668 |
\let\lattice@grid\@empty% |
| 669 |
\let\lattice@bbox\@empty% |
| 670 |
\edef\lattice@x{\pgfkeysvalueof{/lattice/x}}% |
| 671 |
\edef\lattice@y{\pgfkeysvalueof{/lattice/y}}% |
| 672 |
\edef\lattice@node{\pgfkeysvalueof{/lattice/node}}% |
| 673 |
\pgfqkeys{/lattice/arg}{#1}% |
| 674 |
% \end{macrocode} |
| 675 |
% Read the bounding-box coordinates from the |x| and |y| keys: |
| 676 |
% \begin{macrocode} |
| 677 |
\expandafter\lattice@getxy\expandafter{\lattice@x}\la@Ax\la@Bx |
| 678 |
\expandafter\lattice@getxy\expandafter{\lattice@y}\la@Ay\la@By |
| 679 |
% \end{macrocode} |
| 680 |
% Draw the bounding-box if it was required by the options. |
| 681 |
% \begin{macrocode} |
| 682 |
\ifx\lattice@bbox\@empty\else |
| 683 |
\expandafter\draw\expandafter[\lattice@bbox] |
| 684 |
(\strip@pt\la@Ax,\strip@pt\la@Ay) |
| 685 |
rectangle (\strip@pt\la@Bx,\strip@pt\la@By); |
| 686 |
\fi |
| 687 |
% \end{macrocode} |
| 688 |
% Call the main loop. |
| 689 |
% \begin{macrocode} |
| 690 |
\lattice@enumerate{#2}{#3}{#5}{#6}% |
| 691 |
} |
| 692 |
% \end{macrocode} |
| 693 |
% |
| 694 |
% \Finale |
| 695 |
\endinput |