| 1 |
% Kale Ewasiuk ([email protected]) |
| 2 |
% 2025-01-06 |
| 3 |
% Copyright (C) 2021-2025 Kale Ewasiuk |
| 4 |
% |
| 5 |
% Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 |
% of this software and associated documentation files (the "Software"), to deal |
| 7 |
% in the Software without restriction, including without limitation the rights |
| 8 |
% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 |
% copies of the Software, and to permit persons to whom the Software is |
| 10 |
% furnished to do so, subject to the following conditions: |
| 11 |
% |
| 12 |
% The above copyright notice and this permission notice shall be included in |
| 13 |
% all copies or substantial portions of the Software. |
| 14 |
% |
| 15 |
% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF |
| 16 |
% ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED |
| 17 |
% TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A |
| 18 |
% PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT |
| 19 |
% SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR |
| 20 |
% ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 21 |
% ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 |
% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE |
| 23 |
% OR OTHER DEALINGS IN THE SOFTWARE. |
| 24 |
|
| 25 |
\NeedsTeXFormat{LaTeX2e} |
| 26 |
\ProvidesPackage{lutabulartools}[2025-01-06] |
| 27 |
|
| 28 |
\RequirePackage{booktabs} |
| 29 |
\RequirePackage{multirow} |
| 30 |
\RequirePackage{longtable} |
| 31 |
\RequirePackage{makecell} |
| 32 |
\RequirePackage{xparse} |
| 33 |
\RequirePackage{array} |
| 34 |
\RequirePackage{xcolor} |
| 35 |
\RequirePackage{colortbl} |
| 36 |
\RequirePackage{luacode} |
| 37 |
\RequirePackage{penlightplus} % NEEDED, extras option must be used so penlight should be loaded before this... |
| 38 |
|
| 39 |
\luadirect{lutabt = require('lutabulartools')} |
| 40 |
|
| 41 |
\newcommand{\lttdebugON}{\luadirect{lutabt.debug = true}} |
| 42 |
\newcommand{\lttdebugOFF}{\luadirect{lutabt.debug = true}} |
| 43 |
\newcommand{\lttdebugprt}{\luadirect{__lutabt__debugprtall()}} |
| 44 |
|
| 45 |
\newcolumntype{\lttltrim}{} |
| 46 |
\newcolumntype{\lttrtrim}{} |
| 47 |
|
| 48 |
|
| 49 |
%%% midrule every X rows |
| 50 |
|
| 51 |
% NO LONGER NEEDED %%% this was used for @{\midruleX} in colspec, but I made a better way |
| 52 |
%\def\midruleX{\aftergroup\aftergroup\aftergroup\midruleX@aux} |
| 53 |
%\def\midruleX@aux{\luadirect{lutabt.mrX.midruleX('')}} % |
| 54 |
%%% |
| 55 |
|
| 56 |
\NewDocumentCommand{\midruleX}{s O{N} m}{% |
| 57 |
\MakeluastringCommands[N]{#2}% |
| 58 |
\luadirect{lutabt.mrX.set_midruleX(\plluastringA{#3}, \luastring{#1})}% |
| 59 |
} |
| 60 |
\NewDocumentCommand{\resetmidruleX}{O{0}}{\luadirect{lutabt.mrX.reset_midruleX(\luastring{#1})}} |
| 61 |
|
| 62 |
\NewCommandCopy{\ltt@RealPackageWarning}{\PackageWarning} |
| 63 |
\newcommand*{\ltt@DeactivatePackageWarning}{\renewcommand*{\PackageWarning}[2]{}} |
| 64 |
\newcommand*{\ltt@ActivatePackageWarning}{\RenewCommandCopy{\PackageWarning}{\ltt@RealPackageWarning}} |
| 65 |
|
| 66 |
|
| 67 |
\NewDocumentCommand{\settabular}{s m}{\ltt@DeactivatePackageWarning% |
| 68 |
\luadirect{lutabt.set_tabular(\luastring{#2})}% |
| 69 |
\ltt@ActivatePackageWarning% |
| 70 |
}% |
| 71 |
|
| 72 |
\def\ltt@resetrownum{\luadirect{lutabt.reset_rows()}} |
| 73 |
\def\ltt@resetrownumhard{\luadirect{lutabt.row_num = 0}} |
| 74 |
\def\ltt@autotoprule{\luadirect{lutabt.process_auto_topbot_rule('top')}} |
| 75 |
\def\ltt@autobotrule{\luadirect{lutabt.process_auto_topbot_rule('bottom')}} |
| 76 |
\def\ltt@storeautotbandfalse{\luadirect{ |
| 77 |
lutabt.auto_topbot_old = lutabt.auto_topbot |
| 78 |
lutabt.auto_topbot = false}} |
| 79 |
\def\ltt@restoreautotb{\luadirect{lutabt.auto_topbot = lutabt.auto_topbot_old}} |
| 80 |
\def\ltt@inctablelevel{\luadirect{lutabt.tablelevel = lutabt.tablelevel + 1}} |
| 81 |
\def\ltt@dectablelevel{\luadirect{lutabt.tablelevel = lutabt.tablelevel - 1}} |
| 82 |
\def\ltt@midruleXoff{\luadirect{lutabt.mrX.off()}} |
| 83 |
\def\ltt@actlevelreset{\luadirect{lutabt.actlvl = 1}} |
| 84 |
|
| 85 |
\apptocmd{\@arraycr}{\luadirect{lutabt.process_auto_rules()}}{}{} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
\NewDocumentCommand{\midrulesat}{m}{\luadirect{lutabt.add_auto_midrules(\luastring{#1})}} |
| 90 |
|
| 91 |
|
| 92 |
%%%% |
| 93 |
|
| 94 |
%%% gray midrule and gray cmidrule |
| 95 |
\aboverulesep=0.0ex |
| 96 |
\belowrulesep=0.5ex |
| 97 |
\providecommand{\gmidrule}{\arrayrulecolor{lightgray}\specialrule{\lightrulewidth}{0.5\aboverulesep}{0.5\belowrulesep}\arrayrulecolor{black}} |
| 98 |
\cmidrulewidth=\lightrulewidth |
| 99 |
|
| 100 |
%%% use gcmidrule for a gray "c" midrule |
| 101 |
% Copy \cmidrule from |
| 102 |
% http://mirrors.ctan.org/macros/latex/contrib/booktabs/booktabs.dtx |
| 103 |
% and modify it for insertion of \arrayrulecolor{lightgray} at the start, |
| 104 |
% and \arrayrulecolor{black} at the end |
| 105 |
|
| 106 |
\def\oldgcmidrule{\arrayrulecolor{lightgray}% Switch colour to lightgray |
| 107 |
\noalign{\ifnum0=`}\fi |
| 108 |
\@ifnextchar[{\@gcmidrule}{\@gcmidrule[\cmidrulewidth]}} |
| 109 |
\def\@gcmidrule[#1]{\@ifnextchar({\@@gcmidrule[#1]}{\@@gcmidrule[#1]()}} |
| 110 |
\def\@@gcmidrule[#1](#2)#3{\@@@gcmidrule[#3]{#1}{#2}} |
| 111 |
\def\@@@gcmidrule[#1-#2]#3#4{\global\@cmidla#1\relax |
| 112 |
\global\advance\@cmidla\m@ne |
| 113 |
\ifnum\@cmidla>0\global\let\@gtempa\@cmidrulea\else |
| 114 |
\global\let\@gtempa\@cmidruleb\fi |
| 115 |
\global\@cmidlb#2\relax |
| 116 |
\global\advance\@cmidlb-\@cmidla |
| 117 |
\global\@thisrulewidth=#3 |
| 118 |
\@setrulekerning{#4} |
| 119 |
\ifnum\@lastruleclass=\z@\vskip 0.5\aboverulesep\fi |
| 120 |
\ifnum0=`{\fi}\@gtempa |
| 121 |
\noalign{\ifnum0=`}\fi\futurenonspacelet\@tempa\@xgcmidrule} |
| 122 |
\def\@xgcmidrule{% |
| 123 |
\ifx\@tempa\gcmidrule |
| 124 |
\vskip-\@thisrulewidth |
| 125 |
\global\@lastruleclass=\@ne |
| 126 |
\else \ifx\@tempa\morecmidrules |
| 127 |
\vskip \cmidrulesep |
| 128 |
\global\@lastruleclass=\@ne\else |
| 129 |
\vskip 0.5\belowrulesep |
| 130 |
\global\@lastruleclass=\z@ |
| 131 |
\fi\fi |
| 132 |
\ifnum0=`{\fi} |
| 133 |
\arrayrulecolor{black}}% Switch colour back to black |
| 134 |
|
| 135 |
|
| 136 |
%%% |
| 137 |
|
| 138 |
\let\oldcmidrule\cmidrule % improve cmidrule. Can reference column number from back with a +, eg, {3+-2+} means third to second last |
| 139 |
\RenewExpandableDocumentCommand{\cmidrule}{ O{} D(){} m }{% |
| 140 |
\luadirect{lutabt.make1cmidrule( \luastringN{#1}, |
| 141 |
\luastring{#2}, |
| 142 |
\luastring{#3}, |
| 143 |
'oldcmidrule') |
| 144 |
}% |
| 145 |
} |
| 146 |
|
| 147 |
\NewExpandableDocumentCommand{\cmidrules}{ O{} D(){} m }{% supports comma in {} |
| 148 |
\luadirect{lutabt.makecmidrules( \luastringN{#1}, |
| 149 |
\luastring{#2}, |
| 150 |
\luastring{#3}, |
| 151 |
'cmidrule') |
| 152 |
}% |
| 153 |
} |
| 154 |
|
| 155 |
\NewExpandableDocumentCommand{\gcmidrule}{ O{} D(){} m }{% |
| 156 |
\luadirect{lutabt.make1cmidrule( \luastringN{#1}, |
| 157 |
\luastring{#2}, |
| 158 |
\luastring{#3}, |
| 159 |
'oldgcmidrule') |
| 160 |
}% |
| 161 |
} |
| 162 |
|
| 163 |
\NewExpandableDocumentCommand{\gcmidrules}{ O{} D(){} m }{% supports comma in {}, rl can be passed as well |
| 164 |
\luadirect{lutabt.makecmidrules( \luastringN{#1}, |
| 165 |
\luastring{#2}, |
| 166 |
\luastring{#3}, |
| 167 |
'gcmidrule') |
| 168 |
}% |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
\apptocmd{\LTXtable}{\ltt@actlevelreset\ltt@resetrownumhard\luadirect{lutabt.tablelevel=0}}{}{} |
| 177 |
|
| 178 |
|
| 179 |
\let\oldtabular\tabular |
| 180 |
\let\oldendtabular\endtabular |
| 181 |
\RenewExpandableDocumentCommand{\tabular}{ O{l} m }{% |
| 182 |
\luadirect{lutabt.set_col_spec(\luastringN{#2})}% |
| 183 |
\oldtabular[#1]{\lttltrim#2\lttrtrim}\ltt@inctablelevel\ltt@resetrownum\ltt@autotoprule% |
| 184 |
} |
| 185 |
\def\endtabular{\ltt@autobotrule\ltt@midruleXoff\ltt@dectablelevel\oldendtabular} |
| 186 |
|
| 187 |
|
| 188 |
\expandafter\let\expandafter\oldtabulars\csname tabular*\endcsname |
| 189 |
\expandafter\let\expandafter\endoldtabulars\csname endtabular*\endcsname |
| 190 |
\RenewDocumentEnvironment{tabular*}{ m O{l} m }{% |
| 191 |
\luadirect{lutabt.set_col_spec(\luastringN{#3})}% |
| 192 |
\begin{oldtabulars}{#1}[#2]{\lttltrim#3\lttrtrim}\ltt@inctablelevel\ltt@resetrownum\ltt@autotoprule% |
| 193 |
}{% |
| 194 |
\ltt@autobotrule\ltt@midruleXoff\ltt@dectablelevel\end{oldtabulars}% |
| 195 |
} |
| 196 |
%%% NOTE tabularx uses tabular*, NO NEED TO CHANGE TABULARX! |
| 197 |
|
| 198 |
|
| 199 |
\let\oldlongtable\longtable |
| 200 |
\RenewExpandableDocumentCommand{\longtable}{ O{l} m }{% |
| 201 |
\ltt@storeautotbandfalse% |
| 202 |
\luadirect{lutabt.set_col_spec(\luastringN{#2})}% |
| 203 |
\oldlongtable[#1]{\lttltrim#2\lttrtrim}\ltt@inctablelevel\ltt@resetrownum% |
| 204 |
} |
| 205 |
\let\oldendlongtable\endlongtable |
| 206 |
\def\endlongtable{\ltt@restoreautotb\ltt@midruleXoff\ltt@dectablelevel\oldendlongtable}% restore status of auto topbot rule |
| 207 |
|
| 208 |
|
| 209 |
%s0,spec,mcspec,pre,content |
| 210 |
% Magic Cell |
| 211 |
\NewExpandableDocumentCommand{\MC}{ s O{} O{} D<>{} D(){x} m }{% |
| 212 |
% Magic cell *wraps with {} but automatically checks for SI column, |
| 213 |
% [column spec] [mult-column spec override] <pre-cell stuff> (add rule) {cell content} |
| 214 |
\luadirect{lutabt.MagicCell( |
| 215 |
\luastring{#1}, |
| 216 |
\luastring{#2}, |
| 217 |
\luastringN{#3}, |
| 218 |
\luastringN{#4}, |
| 219 |
\luastringN{#6}, |
| 220 |
\luastring{#5} |
| 221 |
)}} |
| 222 |
|
| 223 |
|
| 224 |
\NewDocumentCommand{\setMCrepl}{m m}{\luadirect{ |
| 225 |
lutabt.col_replaces.#1 = '#2' |
| 226 |
}} |
| 227 |
\NewDocumentCommand{\setMChordef}{ m m}{\luadirect{ |
| 228 |
lutabt.col_hor_repl.#1 = '#2' |
| 229 |
}} |
| 230 |
\NewDocumentCommand{\setMCverdef}{ m m}{\luadirect{ |
| 231 |
lutabt.col_ver_repl.#1 = '#2' |
| 232 |
}} |
| 233 |
\NewDocumentCommand{\addMCsicol}{ m }{\luadirect{ |
| 234 |
lutabt.SI_cols[\string##lutabt.SI_cols+1] = '#1' |
| 235 |
}} |
| 236 |
|
| 237 |
\NewDocumentCommand{\forcecolspec}{ m }{\luadirect{ |
| 238 |
lutabt.col_spec = (\luastring{#1}):totable() |
| 239 |
}} |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
\ProcessOptions |
| 244 |
|
| 245 |
|