summaryrefslogtreecommitdiff
path: root/macros/luatex/generic/lutabulartools/lutabulartools.tex
blob: 8a2b5f856911e6e7d66d32fcad365bb7bc003b4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
% Kale Ewasiuk (kalekje@gmail.com)
% 2022-04-16
% Copyright (C) 2021-2022 Kale Ewasiuk
%
% Permission is hereby granted, free of charge, to any person obtaining a copy
% of this software and associated documentation files (the "Software"), to deal
% in the Software without restriction, including without limitation the rights
% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
% copies of the Software, and to permit persons to whom the Software is
% furnished to do so, subject to the following conditions:
%
% The above copyright notice and this permission notice shall be included in
% all copies or substantial portions of the Software.
%
% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
% ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
% TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
% PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT
% SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
% ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
% ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
% OR OTHER DEALINGS IN THE SOFTWARE.


\documentclass[11pt,parskip=half]{scrartcl}
\setlength{\parindent}{0ex}
\newcommand{\llcmd}[1]{\leavevmode\llap{\texttt{\detokenize{#1}\ }}}
\newcommand{\cmd}[1]{\texttt{\detokenize{#1}}}
\newcommand{\qcmd}[1]{``\cmd{#1}''}
\usepackage{url}
\usepackage{showexpl}
\lstset{explpreset={justification=\raggedright,pos=r,wide=true}}
\setlength\ResultBoxRule{0mm}
\lstset{
	language=[LaTeX]TeX,
	basicstyle=\ttfamily\small,
	commentstyle=\ttfamily\small\color{gray},
	frame=none,
	numbers=left,
	numberstyle=\ttfamily\small\color{gray},
	prebreak=\raisebox{0ex}[0ex][0ex]{\color{gray}\ensuremath{\hookleftarrow}},
	extendedchars=true,
	breaklines=true,
	tabsize=4,
}
\addtokomafont{title}{\raggedright}
\addtokomafont{author}{\raggedright}
\addtokomafont{date}{\raggedright}
\author{Kale Ewasiuk (\url{kalekje@gmail.com})}
\usepackage[yyyymmdd]{datetime}\renewcommand{\dateseparator}{--}
\date{\today}





\newcommand{\tMC}{\cmd{\\MC}}

\usepackage[pl,extrasnoglobals]{penlight}


\RequirePackage{lutabulartools}
\title{lutabulartools}
\subtitle{some useful tabular tools (LuaLaTeX-based)}

\usepackage{tabularx}
\usepackage{siunitx}

\lttdebugON

\begin{document}

%  todo: can improve naming... set col spec to a table instead of a string


\maketitle

lutabulartools is a package that contains a few useful macros to help with tables.
Most functions require LuaLaTeX.
This package redefines the {\ttfamily tabular} and {\ttfamily tabular*} environments.
The following packages are loaded:
\texttt{
{booktabs},
{multirow},
{makecell},
{xparse},
{array},
{xcolor},
{colortbl},
{luacode},
{penlight},
}

\section{Options}
The author likes tables with left and right-most columns flush to the end.
Since the \cmd{tabular} env is redefined in this, the author took the liberty to
automatically pad the \cmd{tabular} cell spec with \cmd{@{}} on each end.
If you don't want this, you can pass \cmd{notrim} to the package. Or,
you can manually fix it in a \cmd{tabular} with \cmd{!{}} like so:

\begin{LTXexample}
\begin{tabular}{!{}c!{}}\toprule
Xyz\\\bottomrule    \end{tabular}
\end{LTXexample}

\subsection{Debugging}
You can toggle log output debugging with \cmd{\lttdebugON} and \cmd{\lttdebugOFF}.
The messages will be printed in a format like so:
\begin{verbatim}
vvvvv msg1 (lutabulartools)
msg2
^^^^^
\end{verbatim}

\section{\texttt{\textbackslash MC} -- Magic Cell}
\texttt{\textbackslash MC} (magic cell) combines the facilities of
\cmd{\multirow} and \cmd{\multicolumn} from the \texttt{multirow} package, and \cmd{\makcell} from the titular package.
With the help of LuaLaTeX, it takes an easy-to-use cell specification and employs said commands as required.
\cmd{\MC} will not work properly if your table is only 1 column wide (you probably don't need \cmd{MC} in that case anyway...).
Here is the usage:

\texttt{\textbackslash MC * [cell spec] <cell format> (override multicolumn col) \{contents\} }

\llcmd{*}This will wrap the entire command in \{\}. This is necessary for \texttt{siunitx} single-column width columns.
However, the \cmd{\MC} command attempts to detect this automatically.

\llcmd{[cell spec]}%
Any letters placed in this argument are used for cell alignment.
You can use one of three: \qcmd{t}, \qcmd{m}, \qcmd{b} for top, middle, bottom (vertical alignment),
or \qcmd{l}, \qcmd{c}, \qcmd{r} for horizontal alignment.
By default, \cmd{\MC}   will try to autodetect the horizontal alignment based on the current column.
If it can't, it will be left-aligned.
The default vertical alginment is top. More on this in section \ref{s.def}.

This argument can also contain two integers, separated by a comma (if two are used).
\qcmd{C,R}, \qcmd{C}, or \qcmd{,R} are a valid inputs,
where \cmd{R}=rows (int), and \cmd{C}=columns, (int).
If you want a 1 column wide, multirow cell,
you can pass \qcmd{,R}. These numbers can be negative (positive numbers occupy columns to the right
and rows below, and negative numbers occupy columns to the left and rows above).
If no spec is passed, (argument empty), \cmd{\MC}
acts like a \texttt{makecell}.
Additionally, you can pass \qcmd{+} in place of C (number of columns wide),
and it will make the cell width fill until the end of the current row.

Examples:\\
\qcmd{\MC[2,2]} means two columns wide, two rows tall.\\
\qcmd{\MC[2,1]} or \qcmd{\MC[2]}  means two columns wide, one row tall.\\
\qcmd{\MC[1,2]} or \qcmd{\MC[,2]} means one column wide, two rows tall.\\
\qcmd{\MC[+,2]}, if placed in the first column, occupies the entire row and is two rows tall.\\
\qcmd{\MC[+,2]}, if placed in the second column, occupies the second column to the end of the table and is two rows tall.\\
In any of these examples, you can place the alignment letters anywhere.
So, \qcmd{\MC[l1,2b]} and \qcmd{\MC[1,2 lb]} are both left-bottom aligned (spaces are ignored).


\llcmd{(override}%
You may want to adjust the column specification of a multicolumn  cell,\\
\llcmd{multicolumn)}for example, using
\texttt{(@\{\}c@\{\})} to remove padding between the cell.


\llcmd{<cell format>}%
You can place formatting like \cmd{\bfseries} here.


\subsection{Defaults}\label{s.def}
The \cmd{tabular[*]} environment is re-defined to use Lua pattern matching
to parse the column specification of the table. This is done to determine how many columns there are,
and what the current column type is, even if specifications like \cmd{r@{.}l*{3}{r}} are used.
If you have defined a column that expands many, you should register it with
\cmd{\setMCrepl{?}{??}} where \cmd{?} is your column and \cmd{??} is what it expands to.
You can also specify default horizontal and vertical alignments (ie if alignment not passed to \cmd{\MC})
for an arbitrary column by \cmd{\setMChordef{?}{l|r|c}} and\\
\cmd{\setMChordef{?}{t|m|b}}, where \cmd{?} is the column.
To add a column that should be surrounded by brackets for \cmd{siunitx} purposes, do so with \cmd{\addMCsicol{?}}.
S is included by default.


\newpage

\subsection{Examples}

\let\nl\\


\subsubsection{A good use for headers}
\begin{LTXexample}
\begin{tabular}{ l l l }\toprule
\MC[+m]<\itshape>{A Decent
                Example}\\\midrule
    & \MC[2m]{Heading} \\\cmidrule{2-}
\MC[b,-2]{Multi\\Line}  & A & B   \\\midrule
\end{tabular}
\end{LTXexample}

\subsubsection{A small example}


\begin{LTXexample}
\begin{tabular}{ l l l }\midrule
 A & \MC[mc2,2]{Lttrs}    \\
 B &                       \\
 1 & A & B   \\
\end{tabular}
\end{LTXexample}


\subsubsection{A small bad example}
Notice that the top-aligned p-column
doesn't play particularly well with the middle aligned \cmd{\MC}
\begin{LTXexample}
\begin{tabular}{ p{1cm} l }\toprule
   hello\newline world
        & \MC[mr]{11\\2} \\
\end{tabular}
\end{LTXexample}


\subsubsection{If you insist on vertical lines}
\begin{LTXexample}
\begin{tabular}{|c|c|c|} \hline
1 & 2 & 3\\\hline
4 & \MC[2,2cm](@{}c@{}|)%
    <\ttfamily>{5}\\\cline{1-1}
  & \MC[2](r|){} \\\hline%hacky fix
6 & 7 & 8\\\hline
\end{tabular}
\end{LTXexample}


\subsubsection{A perhaps useful example}

\begin{LTXexample}
\begin{tabularx}{\linewidth}{S[table-format=2.1,table-alignment=left]X}
% err & ... \\% ERROR, not wrap
  \MC{Error,\%} & Comment \\% MC helps
 3.1 & \MC[,2]{multi-line\\comment}\\
 10.1& \\
 \MC[2c]{... ...} \\
\end{tabularx}
\end{LTXexample}

\subsubsection{A messy example}

\begin{LTXexample}
\begin{tabular}{| c | c | c | c | c | c |}\toprule
 \MC[2,2cm]<\ttfamily>{2,2cm}   & \MC[2r]<\ttfamily>{2r} & 5 & \MC[,2b]<\ttfamily>{,2b}\\
   &   & 3 & 4 & 5 & \\\midrule
 1 & 2 & \MC[2l](@{}l)<\ttfamily>{2l (\@\{\}l)} & 5 & 6666\\\cmidrule{3-4}
 1 & \MC[+r]<\ttfamily>{+r}  \\
  \\
 1 & 2 & 3 & 4 & 5 & \MC[,-2]<\ttfamily>{,\\-2}\\
\end{tabular}
\end{LTXexample}





%%% https://tex.stackexchange.com/questions/287346/width-of-column-after-multicolumn-header

%\textbackslash MC[<spec>]{<contents}


%\subsubsection{And another one}
%
%\begin{LTXexample}
%\setMChordef{l}{r}
%\setMCverdef{l}{b}
%% change deflt alignment ^
%\newcolumntype{e}{ll}
%\setMCrepl{e}{ll}
%% e -> l l  ^
%\begin{tabular}{l l l e}\toprule
%aaaaa & bbb & ccc & eee & fff\\
%\MC{aaaaa}\\
%\MC{a}\\\midrule
%\MC{a\\bbbbb}  & x\\\midrule
%\makecell[br]{a\\b} & x\\\midrule
%\MC[+]{--- ---}\\
%\end{tabular}
%\end{LTXexample}
%% WHY ISNT THIS RIGHT?



\newpage


\section{Some additional rules}
This package also redefines the \texttt{booktabs} midrules.\\
\llcmd{\gmidrule}is a full gray midrule.\\
By taking advantage of knowing how many columns there are (if you chose to redefine \texttt{tabular}),
you can specify individual column numbers (for a one column wide rule),
or reference with respect to the last column (blank, \texttt{+1}, \texttt{+0}, or \texttt{+} means last column,
\texttt{+2} means second last column, for example)
or omit the last number.\\
\llcmd{\cmidrule}is a single partial rule, with the above features\\
\llcmd{\gcmidrule}is a single partial gray rule, with the above features\\
You can add multiple \qcmd{cmidrule}'s with the \texttt{(g)cmidrules} command. Separate with a comma.
You can apply global trimming of the rules with the \qcmd{()} optional argument, and then
override it for a specific rule by placing \qcmd{r} or \qcmd{l} with the span specification.\\
\llcmd{\gcmidrules}Can produce multiple, light gray partial rules\\
\llcmd{\cmidrules}Can produce multiple black partial rules.\\
Here's an example:

\begin{LTXexample}
\begin{tabular}{c c c c c c}
 1 & 2 & 3 & 4 & 5 & 6\\
    \cmidrule{+1}  % rule on last column
 1 & 2 & 3 & 4 & 5 & 6\\
    \cmidrules{1,3-+3,+} % rule on first col, third to third last col, and last col
 1 & 2 & 3 & 4 & 5 & 6\\
    \cmidrules{1,3-+3rl,+} % same as above, but trim middle
 1 & 2 & 3 & 4 & 5 & 6\\
    \cmidrules(l){1,r3-+3,+1}% trim left for all, but only trim right for middle rule
 1 & 2 & 3 & 4 & 5 & 6\\
    \gcmidrule{+1}  % rule on last column
 1 & 2 & 3 & 4 & 5 & 6\\
    \gcmidrules{1,3-+3,+} % rule on first col, third to third last col, and last col
 1 & 2 & 3 & 4 & 5 & 6\\
    \gcmidrules{1,3-+3rl,+} % same as above, but trim middle
 1 & 2 & 3 & 4 & 5 & 6\\
    \gcmidrules(l){1,r3-+3,+1}% trim left for all, but only trim right for middle rule
\end{tabular}
\end{LTXexample}


\subsection{Midrule every X\textsuperscript{th} row}

\llcmd{\midruleX}%
With this command, you can place a rule
every X rows.
You can change the step size and what kind of midrule you prefer.
\begin{verbatim}
\def\midruleXstep{5}
\def\midruleXrule{\gmidrule}
\end{verbatim}


To use, insert \cmd{@{\midrulex}} at the end of each row using the tabular column spec.
Before you want counting to begin, you should
apply \cmd{\resetmidruleX}  to avoid header rows being counted.
Use \cmd{\noalign{\resetmiduleX}} if you need place a rule on the same line or in a cell.


\begin{LTXexample}
\def\midruleXstep{3}
\def\midruleXrule{\cmidrules{1,3-4r}}
\begin{tabular}{rclc@{\midruleX}}
                % ^^^ inject midrule
\toprule
Num  & . & . & .  \\
\midrule\resetmidruleX  % reset
1    & & &  \\
2    & & &  \\
3    & & &  \\
4    & & &  \\
5    & & &  \\
6    & & &  \\
7    & & &  \\
8    & & &  \\
9    & & &  \\
10   & & &  \\
11   & & &  \\
\resetmidruleX % no bottom rule
12   & & &  \\
\bottomrule
\end{tabular}
\end{LTXexample}

\end{document}