Figures were positioned by hand-written offsets. Every gap was a magic number tuned against the content that happened to be there, so a label that grew two characters landed on the next tensor, and two stages started from two different x shared no rail. Both failures compile cleanly. Replace it with a cursor. Objects placed with an empty coordinate argument reserve their own width -- including a stack's offset sheets and a bracket's overhang -- and gaps are declared once (\stgutter, \strowgap, \stblockgap). The gap belongs to the object that follows it and the first object in a band gets none, so every band starts flush on a shared rail and gap=0pt states that two shards tile exactly. \stlink makes a connector's label a flow object, which is what removes the label-wider-than-its-arrow failure entirely. \stcol is a vertical sub-flow for a split along the contracted axis. \strow declares its height, so an object that does not fit -- or a column that does not add up to what it declared, and is therefore drawn off-center -- becomes a package warning, which build.sh fails on. Absolute placement is unchanged: passing a coordinate takes the original code path, and \sttrack folds a hand-placed node back into the cursor. All three golden examples and the new tests/flow.tex are converted and build clean.
617 lines
26 KiB
TeX
617 lines
26 KiB
TeX
%% supertensor.sty -- shape-aware tensor figure toolkit
|
|
%% Geometry invariants are enforced by construction: every face is sized from a
|
|
%% declared axis length, so equal shapes get equal edges, a x a is a square, and
|
|
%% a transpose physically swaps width and height.
|
|
%%
|
|
%% Options: cjk load ctex with the portable fandol fontset (XeLaTeX)
|
|
%% en English rail labels in the meaning box (default: zh)
|
|
\NeedsTeXFormat{LaTeX2e}
|
|
\ProvidesPackage{supertensor}[2026/08/04 v0.1 shape-aware tensor figure toolkit]
|
|
|
|
\newif\ifst@cjk\st@cjkfalse
|
|
\newif\ifst@en\st@enfalse
|
|
\DeclareOption{cjk}{\st@cjktrue}
|
|
\DeclareOption{en}{\st@entrue}
|
|
\DeclareOption{zh}{\st@enfalse}
|
|
\ProcessOptions\relax
|
|
|
|
\RequirePackage{amsmath}
|
|
\RequirePackage{amssymb}
|
|
\RequirePackage{xcolor}
|
|
\RequirePackage{array}
|
|
\RequirePackage{etoolbox}
|
|
\RequirePackage{xstring}
|
|
\RequirePackage{tikz}
|
|
\usetikzlibrary{calc,positioning,arrows.meta,backgrounds,fit,shapes.geometric,%
|
|
decorations.pathreplacing,decorations.markings}
|
|
|
|
\ifst@cjk
|
|
\RequirePackage[UTF8,fontset=fandol]{ctex}
|
|
\fi
|
|
|
|
% ---------------------------------------------------------------- layers ----
|
|
% Connectors on the background, tensors on main, all text on the foreground.
|
|
\pgfdeclarelayer{stbg}
|
|
\pgfdeclarelayer{stfg}
|
|
\pgfsetlayers{stbg,main,stfg}
|
|
|
|
% ---------------------------------------------------------------- palette ---
|
|
% Muted, mid-chroma paper colors. Do not add saturated primaries.
|
|
\definecolor{stTeal}{HTML}{4F8FA5}
|
|
\definecolor{stOrange}{HTML}{EE995B}
|
|
\definecolor{stCoral}{HTML}{C95B5B}
|
|
\definecolor{stViolet}{HTML}{8A74B5}
|
|
\definecolor{stGray}{HTML}{85898F}
|
|
\definecolor{stInk}{HTML}{1A1A1A}
|
|
|
|
% Role registry: draw macros take a ROLE, never a color, so one tensor role
|
|
% keeps one hue across every stage of the figure.
|
|
% \stsetrole{X}{stTeal} -> role "X" is teal everywhere
|
|
\newcommand{\stsetrole}[2]{\expandafter\gdef\csname st@role@#1\endcsname{#2}}
|
|
% Expandable on purpose: usable inside \edef. Undeclared roles fall back to
|
|
% neutral gray and are reported at the end of the run.
|
|
\newcommand{\strole}[1]{%
|
|
\ifcsname st@role@#1\endcsname\csname st@role@#1\endcsname\else stGray\fi}
|
|
\newcommand{\stcheckrole}[1]{%
|
|
\ifcsname st@role@#1\endcsname\else
|
|
\PackageWarning{supertensor}{Undeclared role `#1' -- drawn in neutral gray.
|
|
Declare it with \string\stsetrole\space so the hue budget stays visible}%
|
|
\fi}
|
|
\stsetrole{neutral}{stGray}
|
|
|
|
% Three separated lightness levels. Never render data cells below level 1.
|
|
\newcommand{\stlevelpct}[1]{\ifcase#1 0\or30\or55\or80\else55\fi}
|
|
|
|
% ------------------------------------------------------------ geometry ------
|
|
% The geometry ledger, made executable. Declare each symbolic axis once:
|
|
% \stdim{d}{8} \stdim{dh}{4}
|
|
% then every face built from `d' has the same physical edge, everywhere.
|
|
\newlength{\stunit}\setlength{\stunit}{4.6mm}
|
|
\newlength{\sttilegap}\setlength{\sttilegap}{0.5mm}
|
|
\newcommand{\stdim}[2]{\expandafter\gdef\csname st@dim@#1\endcsname{#2}}
|
|
\newcommand{\stresolve}[1]{\ifcsname st@dim@#1\endcsname\csname st@dim@#1\endcsname\else#1\fi}
|
|
|
|
% --------------------------------------------------------- type hierarchy ---
|
|
\newcommand{\stformula}[1]{{\large #1}}
|
|
\newcommand{\ststagelabel}[1]{{\small\bfseries\color{black!55}#1}}
|
|
\newcommand{\stoperator}[1]{{\Large #1}}
|
|
\newcommand{\stsymfont}[1]{{\small #1}}
|
|
\newcommand{\stshapefont}[1]{{\scriptsize\color{black!55}#1}}
|
|
\newcommand{\stprose}[1]{{\small #1}}
|
|
|
|
\tikzset{
|
|
st sym/.style = {font=\small, text=stInk, inner sep=1pt},
|
|
st shape/.style = {font=\scriptsize, text=black!55, inner sep=1pt},
|
|
st stage/.style = {font=\small\bfseries, text=black!55, inner sep=2pt},
|
|
st op/.style = {font=\Large, text=stInk, inner sep=2pt},
|
|
st note/.style = {font=\scriptsize, text=black!55, inner sep=2pt},
|
|
st arrow/.style = {-{Stealth[length=2.2mm,width=1.6mm]}, draw=black!45, line width=0.5pt},
|
|
st comm/.style = {draw=black!45, fill=black!4, rounded corners=1.2pt,
|
|
font=\scriptsize, inner sep=3pt},
|
|
st brace/.style = {decorate, decoration={brace,amplitude=3pt}, draw=black!45,
|
|
line width=0.4pt},
|
|
}
|
|
|
|
% --------------------------------------------------------- flow layout ------
|
|
% Hand-placed absolute coordinates are the main source of layout bugs in these
|
|
% figures. Every gap ends up a tuned magic number, so a label that grows by two
|
|
% characters silently lands on the next tensor, and two stages started from two
|
|
% different x share no rail. The cursor below removes both by construction:
|
|
% gaps are declared once, positions are derived from the objects actually
|
|
% drawn. Absolute placement still works -- pass a coordinate instead of `{}'.
|
|
%
|
|
% \ststage{S1}{stage heading} % anchored on the left rail
|
|
% \strow{R1}{T} % band of height T units
|
|
% \stface[role=q]{Q}{}{T}{d} % empty coord = place at the cursor
|
|
% \stglyph{m}{$\times$}
|
|
% \stface[role=w]{W}{}{d}{d}
|
|
% \stcol{C}{2*d units} % a vertical pair in one band slot
|
|
% \stface[role=r1]{Ca}{}{d}{d}
|
|
% \stface[role=r2, gap=0pt]{Cb}{}{d}{d}
|
|
% \stcolend
|
|
% \strowend % fits the band, arms \stlane
|
|
\newlength{\stgutter}\setlength{\stgutter}{6mm} % between objects in a band
|
|
\newlength{\strowgap}\setlength{\strowgap}{3.5mm} % above a band
|
|
\newlength{\stblockgap}\setlength{\stblockgap}{9mm} % above a stage heading
|
|
\newlength{\stlinklen}\setlength{\stlinklen}{10mm} % bare connector reservation
|
|
\newlength{\st@railx}\newlength{\st@cx}\newlength{\st@cy}
|
|
\newlength{\st@ycur}\newlength{\st@bandh}\newlength{\st@tmpx}\newlength{\st@tmpy}
|
|
% Column sub-flow state. Named st@v* rather than st@col*: \st@cols and \st@col
|
|
% are already the column count and the resolved hue of a face.
|
|
\newlength{\st@vx}\newlength{\st@vy}\newlength{\st@vw}\newlength{\st@vtop}
|
|
\newlength{\st@vh}
|
|
\newif\ifst@inrow
|
|
\newif\ifst@incol
|
|
\newif\ifst@first
|
|
|
|
\newcommand{\stlayoutreset}{%
|
|
\global\st@railx=0pt \global\st@ycur=0pt
|
|
\global\st@cx=0pt \global\st@cy=0pt \global\st@bandh=0pt
|
|
\global\st@inrowfalse \global\st@incolfalse
|
|
\gdef\st@rowlist{}\gdef\st@alllist{}\gdef\st@rowname{}%
|
|
\gdef\st@collist{}\gdef\st@colname{}%
|
|
\gdef\st@lastnode{}\gdef\st@linklabel{}\gdef\st@linkprev{}}
|
|
\stlayoutreset
|
|
\newcommand{\stleftrail}[1]{\global\st@railx=\dimexpr#1\relax}
|
|
\newcommand{\stvgap}[1]{\global\advance\st@ycur by -\dimexpr#1\relax}
|
|
|
|
% The vertical cursor tracks the lowest ink so far, so the next band never has
|
|
% to be positioned by eye.
|
|
\newcommand{\st@lower}[1]{%
|
|
\pgfextracty{\st@tmpy}{\pgfpointanchor{#1}{south}}%
|
|
\ifdim\st@tmpy<\st@ycur \global\st@ycur=\st@tmpy \fi}
|
|
\newcommand{\st@regall}[1]{\xdef\st@alllist{\st@alllist(#1)}}
|
|
% Inside a column the items belong to the column, not to the band: they must not
|
|
% terminate a pending connector, or the arrow would land on the first sheet of
|
|
% the stack instead of on the column as a whole.
|
|
\newcommand{\st@regrow}[1]{%
|
|
\ifst@incol
|
|
\xdef\st@collist{\st@collist(#1)}\st@regall{#1}%
|
|
\else
|
|
\xdef\st@rowlist{\st@rowlist(#1)}\st@regall{#1}%
|
|
\st@drawpendinglink{#1}%
|
|
\gdef\st@lastnode{#1}%
|
|
\fi}
|
|
|
|
\newcommand{\st@needrow}[1]{%
|
|
\ifst@inrow\else
|
|
\PackageError{supertensor}{\string#1\space needs an open \string\strow}%
|
|
{Flow placement only works between \string\strow\space and
|
|
\string\strowend. Pass an explicit coordinate instead.}%
|
|
\fi}
|
|
|
|
% The gap belongs to the object that follows it, and the first object in a band
|
|
% gets none -- that is what puts every band's left edge on the same rail.
|
|
\newcommand{\st@leadgap}[1]{%
|
|
\ifst@first
|
|
\global\st@firstfalse
|
|
\else
|
|
\ifst@incol\global\advance\st@vy by -\dimexpr#1\relax
|
|
\else\global\advance\st@cx by \dimexpr#1\relax\fi
|
|
\fi}
|
|
|
|
% Reserve #1 of horizontal space, hand back the center coordinate for it.
|
|
\newcommand{\st@flow}[1]{%
|
|
\dimen0=\dimexpr#1\relax
|
|
\dimen2=\st@cx \advance\dimen2 by 0.5\dimen0
|
|
\edef\st@pos{(\the\dimen2,\the\st@cy)}%
|
|
\global\advance\st@cx by \dimen0}
|
|
|
|
% Reserve #2 of vertical space inside a column, hand back the center for it.
|
|
% Items are centered on the column's own axis, so a narrow shard sits under a
|
|
% wide one without an eyeballed offset.
|
|
\newcommand{\st@vflow}[2]{%
|
|
\dimen0=\dimexpr#1\relax \dimen4=\dimexpr#2\relax
|
|
\dimen2=\st@vx \advance\dimen2 by 0.5\dimen0
|
|
\dimen6=\st@vy \advance\dimen6 by -0.5\dimen4
|
|
\edef\st@pos{(\the\dimen2,\the\dimen6)}%
|
|
\global\advance\st@vy by -\dimen4
|
|
\ifdim\dimen0>\st@vw \global\st@vw=\dimen0 \fi}
|
|
|
|
% A band declares its height, so an object that does not fit is a build
|
|
% failure rather than something the reader discovers.
|
|
\newcommand{\st@checkh}[2]{%
|
|
\dimen0=\dimexpr#2\relax \advance\dimen0 by -\st@bandh
|
|
\ifdim\dimen0>4mm
|
|
\PackageWarning{supertensor}{Object `#1' overflows its band by
|
|
\the\dimen0. Raise the height declared in \string\strow\space or give it
|
|
its own band}%
|
|
\fi}
|
|
|
|
\newcommand{\ststage}[2]{%
|
|
\dimen0=\st@ycur \advance\dimen0 by -\stblockgap
|
|
\node[st stage, anchor=north west] (#1) at (\the\st@railx,\the\dimen0) {#2};
|
|
\st@regall{#1}\st@lower{#1}}
|
|
|
|
% \strow{name}{height in axis units or a declared axis}
|
|
\newcommand{\strow}[2]{%
|
|
\gdef\st@rowlist{}\gdef\st@rowname{#1}\gdef\st@lastnode{}%
|
|
\gdef\st@linklabel{}\gdef\st@linkprev{}%
|
|
\global\st@inrowtrue\global\st@firsttrue
|
|
% Resolve through the ledger first: pgfmath's parser cannot digest
|
|
% \stresolve's \ifcsname on its own.
|
|
\edef\st@bandu{\stresolve{#2}}%
|
|
\pgfmathsetlengthmacro{\st@bh}{\st@bandu*\stunit}%
|
|
\global\st@bandh=\st@bh
|
|
\global\st@cx=\st@railx
|
|
\dimen0=\st@ycur \advance\dimen0 by -\strowgap \advance\dimen0 by -0.5\st@bandh
|
|
\global\st@cy=\dimen0}
|
|
|
|
\newcommand{\strowend}{%
|
|
\ifdefempty{\st@rowlist}%
|
|
{\PackageWarning{supertensor}{Empty \string\strow\space `\st@rowname'}}%
|
|
{\edef\st@do{\noexpand\node[inner sep=0pt, outer sep=0pt,
|
|
fit={\st@rowlist}] (\st@rowname) {};}\st@do
|
|
\stlane{\st@rowname}\st@regall{\st@rowname}\st@lower{\st@rowname}}%
|
|
\global\st@inrowfalse}
|
|
|
|
% \stcol{name}{height} ... \stcolend -- a vertical sub-flow occupying one slot
|
|
% of the band. This is what a split along a shared axis looks like: two shards
|
|
% stacked with gap=0pt tile their parent exactly, which no pair of hand-tuned
|
|
% y offsets can guarantee.
|
|
\newcommand{\stcol}[2]{%
|
|
\st@needrow{\stcol}%
|
|
\ifst@incol
|
|
\PackageError{supertensor}{Nested \string\stcol}%
|
|
{Close the open column with \string\stcolend\space first.}%
|
|
\fi
|
|
\st@leadgap{\stgutter}%
|
|
\gdef\st@collist{}\gdef\st@colname{#1}%
|
|
\global\st@vx=\st@cx \global\st@vw=0pt
|
|
\edef\st@colu{\stresolve{#2}}%
|
|
\pgfmathsetlengthmacro{\st@ch}{\st@colu*\stunit}%
|
|
\global\st@vh=\st@ch
|
|
\dimen0=\st@cy \advance\dimen0 by 0.5\st@vh
|
|
\global\st@vtop=\dimen0 \global\st@vy=\dimen0
|
|
\global\st@incoltrue\global\st@firsttrue}
|
|
|
|
\newcommand{\stcolend}{%
|
|
\ifdefempty{\st@collist}%
|
|
{\PackageWarning{supertensor}{Empty \string\stcol\space `\st@colname'}%
|
|
\global\st@incolfalse}%
|
|
{\dimen0=\st@vtop \advance\dimen0 by -\st@vy % height actually consumed
|
|
\global\st@incolfalse
|
|
\edef\st@do{\noexpand\node[inner sep=0pt, outer sep=0pt,
|
|
fit={\st@collist}] (\st@colname) {};}\st@do
|
|
\global\advance\st@cx by \st@vw
|
|
\global\st@firstfalse
|
|
% The column is centered on the band using its DECLARED height, so a
|
|
% mismatch is not just an overflow -- it is a column drawn off-center.
|
|
\dimen2=\dimen0 \advance\dimen2 by -\st@vh
|
|
\ifdim\dimen2<0pt \dimen2=-\dimen2 \fi
|
|
\ifdim\dimen2>2mm
|
|
\PackageWarning{supertensor}{Column `\st@colname' consumes
|
|
\the\dimen0\space but declares \the\st@vh; it is drawn off-center.
|
|
Fix the height in \string\stcol}%
|
|
\fi
|
|
\st@checkh{\st@colname}{\the\dimen0}%
|
|
\st@regrow{\st@colname}}}
|
|
|
|
% A node in the flow: an operator glyph, a collective, a note. It reserves its
|
|
% own width, which is the whole point -- `right=6mm of X' reserves nothing, so
|
|
% the next face is free to land on top of it.
|
|
\newcommand{\stnode}[3][]{%
|
|
\st@needrow{\stnode}\st@leadgap{\stgutter}%
|
|
\ifst@incol
|
|
\node[#1, anchor=north west] (#2) at (\the\st@vx,\the\st@vy) {#3};
|
|
\pgfextractx{\st@tmpx}{\pgfpointanchor{#2}{east}}%
|
|
\advance\st@tmpx by -\st@vx
|
|
\ifdim\st@tmpx>\st@vw \global\st@vw=\st@tmpx \fi
|
|
\pgfextracty{\st@tmpy}{\pgfpointanchor{#2}{south}}%
|
|
\global\st@vy=\st@tmpy
|
|
\else
|
|
\node[#1, anchor=west] (#2) at (\the\st@cx,\the\st@cy) {#3};
|
|
\pgfextractx{\st@tmpx}{\pgfpointanchor{#2}{east}}%
|
|
\global\st@cx=\st@tmpx
|
|
\fi
|
|
\st@regrow{#2}}
|
|
% Not \stop: that name is already taken by plain TeX.
|
|
\newcommand{\stglyph}[2]{\stnode[st op]{#1}{#2}}
|
|
\newcommand{\stcomm}[2]{\stnode[st comm]{#1}{#2}}
|
|
\newcommand{\stgap}[1]{\global\advance\st@cx by \dimexpr#1\relax}
|
|
|
|
% \stlink{name}{label} -- a connector whose LABEL is a flow object. The label
|
|
% reserves its own width and the arrows are drawn to the neighbours once the
|
|
% right-hand one exists, so a label can never be wider than its connector.
|
|
\newcommand{\stlink}[2]{%
|
|
\st@needrow{\stlink}%
|
|
\xdef\st@linkprev{\st@lastnode}%
|
|
\ifblank{#2}%
|
|
{\gdef\st@linklabel{}\stgap{\stlinklen}}%
|
|
{\st@leadgap{\stgutter}%
|
|
\node[st note, anchor=west, inner sep=1pt] (#1) at (\the\st@cx,\the\st@cy) {#2};
|
|
\pgfextractx{\st@tmpx}{\pgfpointanchor{#1}{east}}%
|
|
\global\st@cx=\st@tmpx
|
|
\gdef\st@linklabel{#1}\st@regall{#1}}}
|
|
\newcommand{\st@drawpendinglink}[1]{%
|
|
\ifdefempty{\st@linkprev}{}{%
|
|
\begin{pgfonlayer}{stbg}
|
|
\ifdefempty{\st@linklabel}%
|
|
{\draw[st arrow] (\st@linkprev.east) -- (#1.west);}%
|
|
{\draw[st arrow] (\st@linkprev.east) -- (\st@linklabel.west);
|
|
\draw[st arrow] (\st@linklabel.east) -- (#1.west);}%
|
|
\end{pgfonlayer}
|
|
\gdef\st@linkprev{}\gdef\st@linklabel{}}}
|
|
|
|
% Everything drawn through the flow, as one node -- for the meaning box anchor.
|
|
\newcommand{\stbbox}[1]{%
|
|
\edef\st@do{\noexpand\node[inner sep=0pt, outer sep=0pt,
|
|
fit={\st@alllist}] (#1) {};}\st@do}
|
|
|
|
% Escape hatch: fold a hand-placed node into the bounding box and push the
|
|
% vertical cursor below it, so the next band still knows where the ink ends.
|
|
\newcommand{\sttrack}[1]{\st@regall{#1}\st@lower{#1}}
|
|
|
|
% \sttopformula{name}{math} -- the formula line, centered on everything drawn
|
|
% so far. Call it AFTER the bands: a formula placed first can only be centered
|
|
% on a figure whose width is not known yet, which is how the top line ends up
|
|
% visibly off-center.
|
|
\newcommand{\sttopformula}[2]{%
|
|
\stbbox{st@bbt}%
|
|
% For more than one line, wrap the math in amsmath's `gathered': TikZ's own
|
|
% \\ handling does not survive the group \stformula puts around the content.
|
|
\node[above=6mm of st@bbt, anchor=south] (#1) {\stformula{#2}};
|
|
\st@regall{#1}}
|
|
|
|
% ------------------------------------------------------------- faces --------
|
|
\newif\ifst@bracket
|
|
\newif\ifst@border
|
|
\newif\ifst@tiles
|
|
\pgfkeys{
|
|
/st/face/.cd,
|
|
role/.store in=\st@role,
|
|
pattern/.store in=\st@pattern,
|
|
data/.store in=\st@data,
|
|
level/.store in=\st@level,
|
|
gap/.store in=\st@gap,
|
|
bracket/.is if=st@bracket,
|
|
border/.is if=st@border,
|
|
tiles/.is if=st@tiles,
|
|
% gap= is the space BEFORE this object, replacing the standing gutter.
|
|
% gap=0pt is how shards are made to tile their parent exactly.
|
|
role=neutral, pattern=dense, data={}, level=2, gap=\stgutter,
|
|
bracket=false, border=true, tiles=true,
|
|
}
|
|
|
|
% \st@hash{i}{j}{n} -> \st@hv in 0..n-1.
|
|
% Nested mods on purpose. Any polynomial in (i,j) reduced mod 3 is periodic
|
|
% with period 3 in BOTH directions, so a polynomial hash makes rows 1,2,4,5 of
|
|
% a face literally identical -- the eye reads that stripe as structure in the
|
|
% data. Composing coprime moduli (17, 19) pushes the period past any face we
|
|
% would ever draw.
|
|
\newcommand{\st@hash}[3]{%
|
|
\pgfmathtruncatemacro{\st@hv}%
|
|
{mod(mod(11*#1+7*#2,17)*mod(5*#1+13*#2,19)+3*#1+#2,#3)}}
|
|
|
|
% \st@cellfill{i}{j} -> \st@lv in 0..3 (0 = known zero, left unfilled)
|
|
\newcommand{\st@cellfill}[2]{%
|
|
\edef\st@p{\st@pattern}%
|
|
\IfStrEq{\st@p}{solid}{\def\st@lv{\st@level}}{%
|
|
\IfStrEq{\st@p}{diag}{\pgfmathtruncatemacro{\st@lv}{ifthenelse(#1==#2,3,0)}}{%
|
|
\IfStrEq{\st@p}{band}{\pgfmathtruncatemacro{\st@lv}{ifthenelse(abs(#1-#2)<=1,3,0)}}{%
|
|
\IfStrEq{\st@p}{empty}{\def\st@lv{0}}{%
|
|
\IfStrEq{\st@p}{lower}{%
|
|
\ifnum#1<#2 \def\st@lv{0}\else\st@hash{#1}{#2}{2}%
|
|
\pgfmathtruncatemacro{\st@lv}{\st@hv+2}\fi}{%
|
|
\IfStrEq{\st@p}{upper}{%
|
|
\ifnum#1>#2 \def\st@lv{0}\else\st@hash{#1}{#2}{2}%
|
|
\pgfmathtruncatemacro{\st@lv}{\st@hv+2}\fi}{%
|
|
\IfStrEq{\st@p}{causal}{%
|
|
\ifnum#1<#2 \def\st@lv{0}\else\st@hash{#1}{#2}{3}%
|
|
\pgfmathtruncatemacro{\st@lv}{\st@hv+1}\fi}{%
|
|
% default, including `dense'
|
|
\st@hash{#1}{#2}{3}\pgfmathtruncatemacro{\st@lv}{\st@hv+1}}}}}}}}}
|
|
|
|
% Shared setup: read keys, resolve the axis names through the geometry ledger.
|
|
\newcommand{\st@setup}[3]{%
|
|
\pgfkeys{/st/face/.cd,#1}%
|
|
\edef\st@rows{\stresolve{#2}}%
|
|
\edef\st@cols{\stresolve{#3}}%
|
|
\stcheckrole{\st@role}%
|
|
\edef\st@col{\strole{\st@role}}%
|
|
\pgfmathsetlengthmacro{\st@w}{\st@cols*\stunit}%
|
|
\pgfmathsetlengthmacro{\st@h}{\st@rows*\stunit}}
|
|
|
|
% \st@facecore{name}{coord} -- draws one face from the already-resolved state.
|
|
% \ststack calls this directly rather than re-entering \stface: passing
|
|
% `role=\st@role' back through pgfkeys would define \st@role in terms of
|
|
% itself and hang the run.
|
|
\newcommand{\st@facecore}[2]{%
|
|
\st@basenode{#1}{#2}%
|
|
\st@facebody{#1}}
|
|
|
|
% Shared entry for cursor placement: reserve the width, check the band.
|
|
\newcommand{\st@flowbegin}[3]{%
|
|
\st@needrow{\stface}\st@leadgap{\st@gap}%
|
|
\ifst@incol \st@vflow{#2}{#3}\else \st@flow{#2}\st@checkh{#1}{#3}\fi}
|
|
|
|
% Neither the TikZ path parser nor calc expands a macro sitting where it
|
|
% expects `('. Everything the cursor computes therefore goes through \edef and
|
|
% reaches the parser as literal text.
|
|
\newcommand{\st@coordat}[2]{\coordinate (#1) at #2;}
|
|
\newcommand{\st@basenode}[2]{%
|
|
\node[inner sep=0pt, outer sep=0pt, minimum width=\st@w, minimum height=\st@h]
|
|
(#1) at #2 {};}
|
|
|
|
% \stface[keys]{name}{center coord}{rows}{cols}
|
|
% rows/cols accept a declared axis name or a raw integer. Height <- rows,
|
|
% width <- cols, always: a matrix face a x b never renders sideways.
|
|
% An empty coord places the face at the row cursor. The bracket decoration is
|
|
% drawn outside the node box, so its 3.4mm on each side is reserved too.
|
|
\newcommand{\stface}[5][]{%
|
|
\begingroup
|
|
\st@setup{#1}{#4}{#5}%
|
|
\ifblank{#3}{%
|
|
\ifst@bracket\pgfmathsetlengthmacro{\st@tw}{\st@w+6.8mm}\else\let\st@tw\st@w\fi
|
|
\st@flowbegin{#2}{\st@tw}{\st@h}%
|
|
\edef\st@do{\noexpand\st@facecore{#2}{\st@pos}}\st@do
|
|
\st@regrow{#2}%
|
|
}{\st@facecore{#2}{#3}}%
|
|
\endgroup}
|
|
|
|
% \stindexface[keys]{name}{center coord}{rows}{cols}{entries}
|
|
% An INDEX face: the cells carry discrete symbols, not magnitudes. Entries are
|
|
% comma-separated in row-major order (rows*cols of them); `.' leaves a cell
|
|
% blank. Deliberately a different grammar from \stface -- outlined cells, no
|
|
% lightness ramp -- because an index that is drawn like a score invites the
|
|
% reader to compare 7 > 2 as if the numbers meant size.
|
|
\newcommand{\stindexface}[6][]{%
|
|
\begingroup
|
|
\st@setup{#1}{#4}{#5}%
|
|
\ifblank{#3}%
|
|
{\ifst@bracket\pgfmathsetlengthmacro{\st@tw}{\st@w+6.8mm}\else\let\st@tw\st@w\fi
|
|
\st@flowbegin{#2}{\st@tw}{\st@h}%
|
|
\edef\st@do{\noexpand\st@basenode{#2}{\st@pos}}\st@do}%
|
|
{\st@basenode{#2}{#3}}%
|
|
\foreach \st@e [count=\st@z from 0] in {#6} {%
|
|
\pgfmathtruncatemacro{\st@ii}{div(\st@z,\st@cols)+1}%
|
|
\pgfmathtruncatemacro{\st@jj}{mod(\st@z,\st@cols)+1}%
|
|
\edef\st@ee{\st@e}%
|
|
\IfStrEq{\st@ee}{.}{}{%
|
|
\draw[draw=\st@col!70, line width=0.4pt, rounded corners=0.7pt,
|
|
fill=\st@col!12]
|
|
($(#2.north west)+(\st@jj*\stunit-\stunit+0.5\sttilegap,%
|
|
-\st@ii*\stunit+\stunit-0.5\sttilegap)$)
|
|
rectangle
|
|
($(#2.north west)+(\st@jj*\stunit-0.5\sttilegap,%
|
|
-\st@ii*\stunit+0.5\sttilegap)$);
|
|
\node[font=\tiny, text=\st@col!85!black, inner sep=0pt]
|
|
at ($(#2.north west)+(\st@jj*\stunit-0.5\stunit,%
|
|
-\st@ii*\stunit+0.5\stunit)$) {\st@ee};}}%
|
|
\ifst@border
|
|
\draw[draw=black!60, line width=0.5pt, rounded corners=1pt]
|
|
(#2.south west) rectangle (#2.north east);
|
|
\fi
|
|
\ifblank{#3}{\st@regrow{#2}}{}%
|
|
\endgroup}
|
|
|
|
\newcommand{\st@facebody}[1]{%
|
|
\def\st@n{#1}%
|
|
\ifst@tiles
|
|
\IfStrEq{\st@pattern}{data}{%
|
|
\foreach \st@row [count=\st@ii] in \st@data {%
|
|
\foreach \st@jj in {1,...,\st@cols} {%
|
|
\StrChar{\st@row}{\st@jj}[\st@c]%
|
|
\ifnum\st@c>0
|
|
\st@tile{#1}{\st@ii}{\st@jj}{\st@c}%
|
|
\fi}}%
|
|
}{%
|
|
\foreach \st@ii in {1,...,\st@rows} {%
|
|
\foreach \st@jj in {1,...,\st@cols} {%
|
|
\st@cellfill{\st@ii}{\st@jj}%
|
|
\ifnum\st@lv>0
|
|
\st@tile{#1}{\st@ii}{\st@jj}{\st@lv}%
|
|
\fi}}%
|
|
}%
|
|
\else
|
|
\fill[\st@col!\stlevelpct{\st@level}, rounded corners=1pt]
|
|
(#1.south west) rectangle (#1.north east);
|
|
\fi
|
|
\ifst@border
|
|
\draw[draw=black!60, line width=0.5pt, rounded corners=1pt]
|
|
(#1.south west) rectangle (#1.north east);
|
|
\fi
|
|
\ifst@bracket
|
|
\draw[black!55, line width=0.5pt]
|
|
($(#1.north west)+(-1.1mm,0.6mm)$) -- ++(-1.1mm,0)
|
|
-- ($(#1.south west)+(-2.2mm,-0.6mm)$) -- ++(1.1mm,0);
|
|
\draw[black!55, line width=0.5pt]
|
|
($(#1.north east)+(1.1mm,0.6mm)$) -- ++(1.1mm,0)
|
|
-- ($(#1.south east)+(2.2mm,-0.6mm)$) -- ++(-1.1mm,0);
|
|
\fi}
|
|
|
|
% \st@tile{face}{row}{col}{level} -- one rounded tile with a white gutter.
|
|
\newcommand{\st@tile}[4]{%
|
|
\fill[\st@col!\stlevelpct{#4}, rounded corners=0.7pt]
|
|
($(#1.north west)+(#3*\stunit-\stunit+0.5\sttilegap,%
|
|
-#2*\stunit+\stunit-0.5\sttilegap)$)
|
|
rectangle
|
|
($(#1.north west)+(#3*\stunit-0.5\sttilegap,%
|
|
-#2*\stunit+0.5\sttilegap)$);}
|
|
|
|
% \ststack[keys]{name}{center}{rows}{cols}{sheets}
|
|
% Leading axes become depth. Back sheets are outline-only and are included in
|
|
% the bounding box, so neighbours can be spaced against the real extent.
|
|
\newcommand{\ststack}[6][]{%
|
|
\begingroup
|
|
\st@setup{#1}{#4}{#5}%
|
|
\pgfmathtruncatemacro{\st@back}{#6-1}%
|
|
\pgfmathsetlengthmacro{\st@dx}{1.3mm}%
|
|
% The offset sheets are part of the object: reserve their extent too, or the
|
|
% neighbour is spaced against the front sheet and lands on the back ones.
|
|
\ifblank{#3}%
|
|
{\pgfmathsetlengthmacro{\st@tw}{\st@w+\st@back*\st@dx}%
|
|
\pgfmathsetlengthmacro{\st@th}{\st@h+\st@back*\st@dx}%
|
|
\ifst@bracket\pgfmathsetlengthmacro{\st@tw}{\st@tw+6.8mm}\fi
|
|
\st@flowbegin{#2}{\st@tw}{\st@th}%
|
|
\edef\st@do{\noexpand\st@coordat{#2-c}%
|
|
{($\st@pos+(-0.5*\st@back*\st@dx,-0.5*\st@back*\st@dx)$)}}\st@do}%
|
|
{\coordinate (#2-c) at ($#3+(-0.5*\st@back*\st@dx,-0.5*\st@back*\st@dx)$);}%
|
|
\st@basenode{#2-front}{(#2-c)}%
|
|
\ifnum\st@back>0
|
|
% Ascending loop, descending index: `{\macro,...,1}' cannot infer its
|
|
% direction from an unexpanded macro and runs away.
|
|
\foreach \st@kk in {1,...,\st@back} {%
|
|
\pgfmathtruncatemacro{\st@k}{\st@back+1-\st@kk}%
|
|
\draw[draw=black!35, line width=0.4pt, rounded corners=1pt, fill=white]
|
|
($(#2-front.south west)+(\st@k*\st@dx,\st@k*\st@dx)$)
|
|
rectangle
|
|
($(#2-front.north east)+(\st@k*\st@dx,\st@k*\st@dx)$);}
|
|
\fi
|
|
\st@facebody{#2-front}%
|
|
% Braces are load-bearing: a bare coordinate expression contains a comma and
|
|
% would be split into two pgfkeys keys.
|
|
\node[inner sep=0pt, outer sep=0pt,
|
|
fit={(#2-front) ($(#2-front.north east)+(\st@back*\st@dx,\st@back*\st@dx)$)}]
|
|
(#2) {};
|
|
\ifblank{#3}{\st@regrow{#2}}{}%
|
|
\endgroup}
|
|
|
|
% ------------------------------------------------- symbol / shape captions ---
|
|
% Symbol immediately under the block, shape on the next line. Both are reserved
|
|
% lanes: nothing else may be placed between a face and its caption.
|
|
% \stlane{node} declares a shared caption baseline: every later \stcaption
|
|
% hangs from the bottom of that node instead of from its own block, so symbols
|
|
% and shapes occupy two flat lanes even when the faces have different heights.
|
|
% Typical use: draw the row, \node[fit=(A)(B)(C)] (row) {};, \stlane{row}.
|
|
\def\st@lane{}
|
|
\newcommand{\stlane}[1]{\def\st@lane{#1}}
|
|
\newcommand{\stnolane}{\def\st@lane{}}
|
|
\newcommand{\stcaption}[3]{%
|
|
\ifdefempty{\st@lane}%
|
|
{\node[st sym, below=1.6mm of #1] (#1-sym) {#2};}%
|
|
{\node[st sym, anchor=north]
|
|
at ($(#1.center |- \st@lane.south)+(0,-1.6mm)$) (#1-sym) {#2};}%
|
|
\node[st shape, below=0.6mm of #1-sym] (#1-shape) {#3};%
|
|
\st@regall{#1-shape}\st@lower{#1-shape}}
|
|
\newcommand{\stcaptiontop}[2]{%
|
|
\node[st sym, above=1.6mm of #1] (#1-top) {#2};}
|
|
|
|
% ------------------------------------------------------------ connectors ----
|
|
% Routed on the background layer so a connector can never cover a face.
|
|
\newcommand{\starrow}[3][]{%
|
|
\begin{pgfonlayer}{stbg}
|
|
\draw[st arrow,#1] (#2) -- (#3);
|
|
\end{pgfonlayer}}
|
|
\newcommand{\starrowlabel}[4][]{%
|
|
\begin{pgfonlayer}{stbg}
|
|
\draw[st arrow,#1] (#2) -- node[st note, above, fill=white, inner sep=1pt] {#4} (#3);
|
|
\end{pgfonlayer}}
|
|
|
|
% ------------------------------------------------------------ meaning box ---
|
|
\ifst@en
|
|
\def\st@lblaxes{Axes}\def\st@lblobj{Objects}\def\st@lblmech{Mechanism}
|
|
\else
|
|
\def\st@lblaxes{轴}\def\st@lblobj{对象}\def\st@lblmech{机制}
|
|
\fi
|
|
\newcommand{\stsetlabels}[3]{\def\st@lblaxes{#1}\def\st@lblobj{#2}\def\st@lblmech{#3}}
|
|
|
|
% \stmeaningbox{node name}{total width}{below-of anchor}{axes}{objects}{mechanism}
|
|
% One full-width, low-contrast box, one reading column, at most three rows.
|
|
% Leave a row empty to drop it.
|
|
\newcommand{\stmeaningbox}[6]{%
|
|
\node[below=4mm of #3, anchor=north,
|
|
draw=black!18, fill=black!3, rounded corners=1.5pt,
|
|
inner xsep=9pt, inner ysep=8pt, text width=#2] (#1) {%
|
|
\small
|
|
\setlength{\tabcolsep}{0pt}%
|
|
\renewcommand{\arraystretch}{1.25}%
|
|
\begin{tabular}{@{}p{\st@raillen}@{\hspace{7pt}}p{\dimexpr#2-\st@raillen-7pt\relax}@{}}
|
|
% \ifblank, not \IfStrEq: the row content is math and CJK, and must not
|
|
% be expanded just to test for emptiness.
|
|
\ifblank{#4}{}{\textbf{\st@lblaxes} & \raggedright\arraybackslash #4 \\}
|
|
\ifblank{#5}{}{\textbf{\st@lblobj} & \raggedright\arraybackslash #5 \\}
|
|
\ifblank{#6}{}{\textbf{\st@lblmech} & \raggedright\arraybackslash #6 \\}
|
|
\end{tabular}};}
|
|
\newlength{\st@raillen}\setlength{\st@raillen}{3.2em}
|
|
\newcommand{\stsetrail}[1]{\setlength{\st@raillen}{#1}}
|
|
|
|
% ------------------------------------------------------------- signature ----
|
|
% One centered identification line, outside the meaning box, low contrast.
|
|
\def\st@author{五道口纳什}
|
|
\newcommand{\stsetauthor}[1]{\def\st@author{#1}}
|
|
\newcommand{\stsignature}[2]{%
|
|
\node[below=2.2mm of #2, font=\scriptsize, text=black!45] (st-signature) {#1@\st@author};}
|
|
|
|
\endinput
|