summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/luatex/lutabulartools/lutabulartools.sty
blob: 1d2522b099d579c5cfca2056d76cd836a993fe55 (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
% Kale Ewasiuk (kalekje@gmail.com)
% 2022-11-27
% 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.

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{lutabulartools}[2022-11-27]

\RequirePackage{booktabs}
\RequirePackage{multirow}
\RequirePackage{makecell}
\RequirePackage{xparse}
\RequirePackage{array}
\RequirePackage{xcolor}
\RequirePackage{colortbl}
\RequirePackage{luacode}
\RequirePackage{penlight} % NEEDED, extras option must be used so penlight should be loaded before this...

\luadirect{lutabt = require('lutabulartools')}

\newcommand{\lttdebugON}{\luadirect{lutabt.debug = true}}
\newcommand{\lttdebugOFF}{\luadirect{lutabt.debug = true}}

\newcolumntype{\lttltrim}{}
\newcolumntype{\lttrtrim}{}


%%% midrule every X rows

\def\midruleX{\aftergroup\aftergroup\aftergroup\midruleX@aux}
\def\midruleX@aux{\luadirect{lutabt.mrX.midruleX('')}}  % todo argument for vadjust or noalign??
\NewDocumentCommand{\setmidruleX}{s O{N} m}{%
  \MakeluastringCommands[N]{#2}%
  \luadirect{lutabt.mrX.set_midruleX(\plluastringA{#3}, \luastring{#1})}%
}
\NewDocumentCommand{\midruleXreset}{O{0}}{\luadirect{lutabt.mrX.cntr = tonumber(\luastring{#1})}}
\NewCommandCopy\resetmidruleX\midruleXreset



%%%%

%%% gray midrule and gray cmidrule
\aboverulesep=0.0ex
\belowrulesep=0.5ex
\providecommand{\gmidrule}{\arrayrulecolor{lightgray}\specialrule{\lightrulewidth}{0.5\aboverulesep}{0.5\belowrulesep}\arrayrulecolor{black}}
\cmidrulewidth=\lightrulewidth

%%% use gcmidrule for a gray "c" midrule
% Copy \cmidrule from
%  http://mirrors.ctan.org/macros/latex/contrib/booktabs/booktabs.dtx
% and modify it for insertion of \arrayrulecolor{lightgray} at the start,
% and \arrayrulecolor{black} at the end

\def\oldgcmidrule{\arrayrulecolor{lightgray}% Switch colour to lightgray
    \noalign{\ifnum0=`}\fi
    \@ifnextchar[{\@gcmidrule}{\@gcmidrule[\cmidrulewidth]}}
\def\@gcmidrule[#1]{\@ifnextchar({\@@gcmidrule[#1]}{\@@gcmidrule[#1]()}}
\def\@@gcmidrule[#1](#2)#3{\@@@gcmidrule[#3]{#1}{#2}}
\def\@@@gcmidrule[#1-#2]#3#4{\global\@cmidla#1\relax
    \global\advance\@cmidla\m@ne
    \ifnum\@cmidla>0\global\let\@gtempa\@cmidrulea\else
    \global\let\@gtempa\@cmidruleb\fi
    \global\@cmidlb#2\relax
    \global\advance\@cmidlb-\@cmidla
    \global\@thisrulewidth=#3
    \@setrulekerning{#4}
    \ifnum\@lastruleclass=\z@\vskip 0.5\aboverulesep\fi
    \ifnum0=`{\fi}\@gtempa
    \noalign{\ifnum0=`}\fi\futurenonspacelet\@tempa\@xgcmidrule}
\def\@xgcmidrule{%
   \ifx\@tempa\gcmidrule
       \vskip-\@thisrulewidth
       \global\@lastruleclass=\@ne
   \else \ifx\@tempa\morecmidrules
       \vskip \cmidrulesep
       \global\@lastruleclass=\@ne\else
       \vskip 0.5\belowrulesep
       \global\@lastruleclass=\z@
   \fi\fi
   \ifnum0=`{\fi}
  \arrayrulecolor{black}}% Switch colour back to black


%%%

\let\oldcmidrule\cmidrule % improve cmidrule. Can reference column number from back with a +, eg, {3+-2+} means third to second last
\RenewExpandableDocumentCommand{\cmidrule}{ O{} D(){} m }{%
    \luadirect{lutabt.make1cmidrule( \luastringN{#1},
                            \luastring{#2},
                            \luastring{#3},
                            'oldcmidrule')
    }%
}

\NewExpandableDocumentCommand{\cmidrules}{ O{} D(){} m }{%  supports comma in {}
    \luadirect{lutabt.makecmidrules( \luastringN{#1},
                            \luastring{#2},
                            \luastring{#3},
                            'cmidrule')
    }%
}

\NewExpandableDocumentCommand{\gcmidrule}{ O{} D(){} m }{%
    \luadirect{lutabt.make1cmidrule( \luastringN{#1},
                                \luastring{#2},
                                \luastring{#3},
                                'oldgcmidrule')
    }%
}

\NewExpandableDocumentCommand{\gcmidrules}{ O{} D(){} m }{%  supports comma in {}, rl can be passed as well
    \luadirect{lutabt.makecmidrules( \luastringN{#1},
                                    \luastring{#2},
                                    \luastring{#3},
                                    'gcmidrule')
    }%
}



\NewDocumentCommand{\midrulesat}{m}{\luadirect{lutabt.add_auto_midrules(\luastring{#1})}}




\NewDocumentCommand{\settabular}{s m}{\luadirect{lutabt.set_tabular(\luastring{#2})}} % make kv interface for auto

\def\ltt@resetrownum{\luadirect{lutabt.reset_rows()}}
\def\ltt@autotoprule{\luadirect{lutabt.process_auto_topbot_rule('top')}}
\def\ltt@autobotrule{\luadirect{lutabt.process_auto_topbot_rule('bottom')}}

\let\oldtabular\tabular
\let\oldendtabular\endtabular
\RenewExpandableDocumentCommand{\tabular}{ O{l} m }{%
  \luadirect{lutabt.set_col_spec(\luastringN{#2})}%
  \oldtabular[#1]{\lttltrim#2\lttrtrim}\ltt@inctablelevel\ltt@resetrownum\ltt@autotoprule%
}
\def\endtabular{\ltt@autobotrule\ltt@dectablelevel\oldendtabular}




\expandafter\let\expandafter\oldtabulars\csname tabular*\endcsname
\expandafter\let\expandafter\endoldtabulars\csname endtabular*\endcsname
\RenewDocumentEnvironment{tabular*}{ m O{l} m }{%
  \luadirect{lutabt.set_col_spec(\luastringN{#3})}%
  \begin{oldtabulars}{#1}[#2]{\lttltrim#3\lttrtrim}\ltt@inctablelevel\ltt@resetrownum\ltt@autotoprule%
}{%
  \ltt@autobotrule\ltt@dectablelevel\end{oldtabulars}%
}
%%% NOTE tabularx uses tabular*, NO NEED TO CHANGE TABULARX!

\let\oldlongtable\longtable
\RenewExpandableDocumentCommand{\longtable}{ O{l} m }{%
  \luadirect{lutabt.auto_topbot_old = lutabt.auto_topbot
  lutabt.auto_topbot = false}%
  \luadirect{lutabt.set_col_spec(\luastringN{#2})}%
  \oldlongtable[#1]{\lttltrim#2\lttrtrim}\ltt@inctablelevel\ltt@resetrownum%
}
\let\oldendlongtable\endlongtable
\def\endlongtable{\luadirect{lutabt.auto_topbot = lutabt.auto_topbot_old}
  \ltt@dectablelevel\oldendlongtable}% restore status of auto topbot rule

\apptocmd{\@arraycr}{\luadirect{lutabt.process_auto_rules()}}{}{}


\def\ltt@inctablelevel{\luadirect{lutabt.tablelevel = lutabt.tablelevel + 1}}
\def\ltt@dectablelevel{\luadirect{lutabt.tablelevel = lutabt.tablelevel - 1}}

%s0,spec,mcspec,pre,content
% Magic Cell
\NewExpandableDocumentCommand{\MC}{ s O{} O{} D<>{} D(){x} m }{%
% Magic cell *wraps with {} but automatically checks for SI column,
                    % [column spec] [mult-column spec override]  <pre-cell stuff> (add rule)  {cell content}
\luadirect{lutabt.MagicCell(
    \luastring{#1},
    \luastring{#2},
    \luastringN{#3},
    \luastringN{#4},
    \luastringN{#6},
    \luastring{#5}
)}}


\NewDocumentCommand{\setMCrepl}{m m}{\luadirect{
  lutabt.col_replaces.#1 = '#2'
}}
\NewDocumentCommand{\setMChordef}{ m m}{\luadirect{
  lutabt.col_hor_repl.#1 = '#2'
}}
\NewDocumentCommand{\setMCverdef}{ m m}{\luadirect{
  lutabt.col_ver_repl.#1 = '#2'
}}
\NewDocumentCommand{\addMCsicol}{ m }{\luadirect{
  lutabt.SI_cols[\string##lutabt.SI_cols+1] = '#1'
}}

\NewDocumentCommand{\forcecolspec}{ m }{\luadirect{
  lutabt.col_spec = (\luastring{#1}):totable()
}}



\ProcessOptions







%%%  OLD STUFF,
%% before I had a fall-back for counting columns (just calculate current column on last column once)
%%   and kept track of row numbers. I felt that this wasn't needed, so I've removed.
%\newcommand{\TabColNum}{\luadirect{tex.print(lutabt.get_TabColNum())}} % prints the current column number of tabular
%\newcommand{\NumTabCols}{\luadirect{tex.print(lutabt.NumTabCols)}} % prints the total num columns of tabular
%\newcounter{RowNumCnt}
%\setcounter{RowNumCnt}{1}
%  \setcounter{RowNumCnt}{1}%
%  \luadirect{lutabt.NumTabColsMX = 0}% fall back if calc doesnt work for some reason
%  \setcounter{RowNumCnt}{1}%
%  \luadirect{lutabt.NumTabColsMX = 0}% fall back if calc doesnt work for some reason
%\DeclareOption{notrimx}{
%  \global\newcolumntype{\lttltrim}{!{\stepcounter{RowNumCnt}}}
%%  \global\newcolumntype{\lttrtrim}{!{\luadirect{lutabt.get_TabColNum()}}}
%}
%\DeclareOption{trimx}{
%  \newcolumntype{\lttltrim}{@{\stepcounter{RowNumCnt}}}
%%  \newcolumntype{\lttrtrim}{@{\luadirect{lutabt.get_TabColNum()}}}
%}


%% adapted from
% https://tex.stackexchange.com/questions/182953/add-midrule-every-5-lines-automatically
%% https://tex.stackexchange.com/questions/145084/what-is-the-function-of-noalign
%
%\def\midruleXstep{5}
%\def\midruleXrule{\gmidrule}
%
%\newcounter{midruleX}
%\newcommand*{\midruleXa}{%
%  \aftergroup\aftergroup\aftergroup\midruleXaux
%}
%
%\newif\ifmidruleX
%
%\newcommand*{\midruleXaux}{%
%  \noalign{%
%    \stepcounter{midruleX}%
%    \ifnum\value{midruleX}=\midruleXstep %
%      \global\midruleXtrue
%      \resetmidruleX%
%    \else
%      \global\midruleXfalse
%    \fi
%  }
%  \ifmidruleX\midruleXrule\fi
%}

%\newcommand*{\resetmidruleX}{\setcounter{midruleX}{0}}  %
%\resetmidruleX

%\NewDocumentCommand{\resetmidruleXonNP}{O{0}}{\luadirect{lutabt.reset_midruleX_on_newpage(\luastring{#1})}}

%
%\NewDocumentCommand{\IfmidruleX}{m m}{%  if a midruleX was placed, do #1, if not, do #2
%  \ifnum\the\value{midruleX}=0
%      #1
%    \else
%      #2
%  \fi%
%}