Debian Codegraph @

texmf-dist/source/latex/wargame/hex/labels.dtx

raw
1 % \iffalse
2 % --------------------------------------------------------------------
3 %<*hex>
4 % \fi
5 %
6 % \subsubsection{Labels}
7 % \label{sec:impl:hex:label}
8 %
9 % Like terrains, we will set up some macros for dealing with labels.
10 %
11 % To process coordinates and turn them into labels, we set up two
12 % counters.
13 %
14 % \begin{macrocode}
15 \newcounter{hex@l@c}
16 \newcounter{hex@l@r}
17 % \end{macrocode}
18 %
19 % In case we want to invert the row axis, we set-up a key to set the
20 % maximum row number.
21 %
22 % \begin{macrocode}
23 \def\hex@max@row{-1}
24 \tikzset{
25 max hex row/.store in=\hex@max@row,
26 }
27 % \end{macrocode}
28 %
29 % Again, we will make a separate namespace/family for the handling
30 % labels. We also define a counter which we will use to typeset
31 % alphabetic column numbers.
32 %
33 % First a graphics style.
34 %
35 % \begin{macrocode}
36 \tikzset{%
37 hex/label/.style={%
38 draw=none,%
39 shape=rectangle,%
40 anchor=north,%
41 color=gray,%
42 font=\sffamily\bfseries\scriptsize,%
43 inner sep=0},
44 }
45 % \end{macrocode}
46 %
47 % Next, the choices of how to make a label. These are put in the
48 % \spec{/hex/label} family to make it easy to parse out only these
49 % keys. This uses some macros defined below. Note, this uses the
50 % macros \cs{hex@col} and \cs{hex@row} defined by the hex coordinate
51 % system. \emph{Important:} If you do not want to set a label but
52 % they are otherwise automatically added, then set \texttt{label=none}
53 % rather than \texttt{label=} (empty), which will not work.
54 %
55 % \begin{macrocode}
56 \tikzset{%
57 /hex/label/.search also={/tikz},
58 /hex/label/.cd,
59 none/.code={\global\let\hex@l@text\@empty},
60 auto/.is choice,
61 auto/none/.code={\global\let\hex@l@text\@empty},
62 auto/numbers/.code={%
63 \hex@l@abs%
64 \xdef\hex@l@text{%
65 \hex@l@n@pad{\the\c@hex@l@c}%
66 \hex@l@n@pad{\the\c@hex@l@r}}},
67 auto/alpha column/.code={%
68 \xdef\hex@l@text{%
69 \ifnum0>\hex@col\AlphAlph{-\hex@col}\else\AlphAlph{\hex@col}\fi%
70 \hex@row}},
71 auto/alpha 2 column/.code={%
72 \hex@l@abs%
73 \advance\c@hex@l@c27\relax%
74 \xdef\hex@l@text{%
75 \AlphAlph{\value{hex@l@c}}%
76 \hex@l@n@pad{\hex@row}}},
77 auto/inv y x plus 1/.code={%
78 \hex@dbg{3}{Inverse row, add one to column with arg `#1'}
79 \let\hex@l@text\@empty%
80 \ifnum\hex@max@row>0%
81 \pgfmathtruncatemacro{\hex@l@row}{\hex@max@row-\hex@row}
82 \pgfmathtruncatemacro{\hex@l@col}{1+\hex@col}
83 \xdef\hex@l@text{%
84 \hex@l@n@pad{\hex@l@col}%
85 \hex@l@n@pad{\hex@l@row}}
86 \else\message{Max row number not set}\fi},
87 auto/x and y plus 1/.code={%
88 \hex@dbg{3}{Inverse row, add one to column with arg `#1'}
89 \pgfmathtruncatemacro{\hex@l@row}{1+\hex@row}
90 \pgfmathtruncatemacro{\hex@l@col}{1+\hex@col}
91 \xdef\hex@l@text{%
92 \hex@l@n@pad{\hex@l@col}%
93 \hex@l@n@pad{\hex@l@row}}},
94 auto/.default=numbers,
95 %text/.store in=\hex@l@text,
96 text/.code={\gdef\hex@l@text{#1}},
97 place/.store in=\hex@l@pos,
98 location/.forward to=/hex/label/place,
99 rotate/.store in=\hex@l@rot
100 }
101 % \end{macrocode}
102 % \begin{Macro}{\hex@l@abs}
103 %
104 % This takes the absolute value of row and column numbers.
105 %
106 % \begin{macrocode}
107 \def\hex@l@abs{
108 \setcounter{hex@l@c}{\hex@col}
109 \setcounter{hex@l@r}{\hex@row}
110 \expandafter\ifnum\value{hex@l@c}<0\multiply\c@hex@l@c by-1\fi%
111 \expandafter\ifnum\value{hex@l@r}<0\multiply\c@hex@l@r by-1\fi%
112 % \hex@dbg{0}{\hex@col->\the\c@hex@l@c\space\hex@row->\the\c@hex@l@r}
113 }
114 % \end{macrocode}
115 % \end{Macro}
116 %
117 % \begin{Macro}{\hex@l@n@pad}
118 % This will pad a number with a 0 if the number is smaller than 10.
119 %
120 % \begin{macrocode}
121 \long\def\hex@l@n@pad#1{%
122 \ifnum#1<10 0\fi%
123 #1}
124 % \end{macrocode}
125 % \end{Macro}
126 %
127 %
128 % \begin{Macro}{\hex@do@label}
129 %
130 % This macro puts in the label. First, we reset label keys, then we
131 % read in the keys from the argument. If this results in the macro
132 % \cs{hex@l@text} to be non-empty, then we set the label via a
133 % \TikZ{} \spec{node}.
134 %
135 % \begin{macrocode}
136 \def\hex@do@label{%
137 \hex@dbg{3}{Hex label: `\meaning\hex@label'}%
138 \edef\hex@l@tmp{[%
139 /hex/label/.cd,%
140 rotate=0,%
141 place={(90:.8)},%
142 /tikz/hex/label/.try,%
143 /tikz/every hex label/.try,%
144 \hex@label]}%
145 \expandafter\scope\hex@l@tmp%
146 \hex@dbg{1}{Label:
147 ^^J Text: `\meaning\hex@l@text'
148 ^^J Location: `\meaning\hex@l@pos'
149 ^^J Rotation: `\meaning\hex@l@rot'
150 }%
151 \@ifundefined{hex@l@text}{\let\hex@l@text\empty}{}%
152 \ifx\hex@l@text\empty\else%
153 \node[rotate=\hex@l@rot] at \hex@l@pos {\hex@l@text};%
154 \fi%
155 \endscope%
156 }
157 % \end{macrocode}
158 % \end{Macro}
159 % \iffalse
160 %</hex>
161 % --------------------------------------------------------------------
162 % \fi