% Kale Ewasiuk (kalekje@gmail.com)
% 2022-11-29
% 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}\ }}}
\def\cmd{\bgroup\catcode`\\=12 \catcode`\ =12 \cmdA}
\def\cmdA#1{\egroup{\texttt{\detokenize{#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{caption}

\usepackage{ltxtable}
\usepackage{longtable}

\usepackage[pl,extras]{penlight}


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

\usepackage{tabularx}
\usepackage{siunitx}

\lttdebugON

\usepackage{hyperref}


\begin{document}

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

\maketitle

\cmd{lutabulartools} is a package that contains a few useful Lua\LaTeX-based macros to help with tables.
A global \cmd{lua} variable \cmd{lutabt} is created.
This package redefines the {\ttfamily tabular} and {\ttfamily tabular*} environments as well as \cmd{\@arraycr} to add functionarlity.
The following packages are loaded by this one,
so if you have specific settings for these packages, load the \cmd{lutabulartools} package after:
\texttt{
{booktabs},
{multirow},
{makecell},
{xparse},
{array},
{xcolor},
{colortbl},
{luacode},
{penlight}}.

\section{\texttt{\textbackslash settabular}}

A key-val interface in the \cmd{\settabular{}} command is used to set some tabular settings.
\cmd{nopad} automatically adds \cmd{@{}} on each end of the column spec.\\
\cmd{tbrule} automatically adds \cmd{\toprule} as the first thing in the \cmd{tabular(*|x)} environment, and \cmd{\bottomrule} as the last.
Note that this automatic top/bottom rule adding is disabled in \cmd{longtable}.
\cmd{row/colsep} tweaks the row spacing with \cmd{arraystretch}
or adjusts the \cmd{tabcolsep} length (an integer must be used, the result is multiplied by 6pt). For example:
\begin{verbatim}
\settabular{nopad,tbrule,rowsep=2,colsep=2} % or
\settabular{nopad=false,tbrule=false} % to set the switches to off
\end{verbatim}


\section{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}

\cmd{\lttdebugprt} can be used pretty-print the \cmd{lutabt} module and its attributes---useful for checking the
``state'' of the package.

\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] [override multicol] <cell format>   \{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: \cmd{t}, \cmd{m}, \cmd{b} for top, middle, bottom (vertical alignment),
and/or \cmd{l}, \cmd{c}, \cmd{r} for horizontal alignment, in no particular order.
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).
\cmd{C,R}, \cmd{C}, or \cmd{,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 \cmd{,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 \cmd{+} in place of C (number of columns wide),
and it will make the cell width fill until the end of the current row.

Examples:\\
\cmd{\MC[2,2]} means two columns wide, two rows tall.\\
\cmd{\MC[2,1]} or \cmd{\MC[2]}  means two columns wide, one row tall.\\
\cmd{\MC[1,2]} or \cmd{\MC[,2]} means one column wide, two rows tall.\\
\cmd{\MC[+,2]}, if placed in the first column, occupies the entire row and is two rows tall.\\
\cmd{\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, \cmd{\MC[l1,2b]} and \cmd{\MC[1,2 lb]} are both left-bottom aligned (spaces are ignored).\\
Put an \cmd{_} as the last thing to add a \cmd{cmidrule} under, followed by a trim spec (the thing that goes in \cmd{()})
\cmd{\MC[+c_l]} will add a \cmd{cmidrule} underneath the cell (which fills until the end of the table), with the left side trimmed.

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

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


\subsection{\texttt{\textbackslash MC} 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{?}}.
\cmd{S} is included by default.


\section{\texttt{\textbackslash midrulesat}}
If you'd rather specify the location of midrules outside the table, use \cmd{\midrulesat{1,2,3}}: a midrule will be
placed on rows 1, 2, and 3, for the next table only.


\section{Examples}

\let\nl\\

Change the settings for the rest of the doc.

\begin{LTXexample}
\settabular{nopad,tbrule}
\end{LTXexample}

\settabular{nopad,tbrule}


\subsubsection{A good use for headers}

\begin{LTXexample}
\begin{tabular}{ l l l }
\MC[+m]<\itshape>{A Decent
                Example}\\\midrule
    & \MC[2m_]{Heading} \\
\MC[b,-2]{Multi\\Line}  & A & B \\\midrule
   end & & \\
\end{tabular}
\end{LTXexample}

\subsubsection{A small example}
\begin{LTXexample}
\midrulesat{1,2,3}
\begin{tabular}{ l l l }
a \\
b \\
c \\
d \\
\end{tabular}
\end{LTXexample}


\subsubsection{A small example}

\begin{LTXexample}
\begin{tabular}{ l l l }\midrule
 \MC[_]{A} & \MC[mc2,2]{Lttrs}    \\
 \MC[_r]{B} &        \\ \cmidrule{2-3}
 \MC[_r]{1} & \MC[_r]{A} & \MC[_r]{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 }
   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}



\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 \cmd{cmidrule}'s with the \texttt{(g)cmidrules} command. Separate with a comma.
You can apply global trimming of the rules with the \cmd{()} optional argument, and then
override it for a specific rule by placing \cmd{r} or \cmd{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}
 a & 2 & 3 & 4 & 5 & 6\\
    \cmidrule{+1}  % rule on last column
 b & 2 & 3 & 4 & 5 & 6\\
    \cmidrules{1,3-+3,+} % rule on first col, third to third last col, and last col
 c & 2 & 3 & 4 & 5 & 6\\
    \cmidrules{1,3-+3rl,+} % same as above, but trim middle
 d & 2 & 3 & 4 & 5 & 6\\
    \cmidrules(l){1,r3-+3,+1}% trim left for all, but only trim right for middle rule
 e & 2 & 3 & 4 & 5 & 6\\
    \gcmidrule{+1}  % rule on last column
 f & 2 & 3 & 4 & 5 & 6\\
    \gcmidrules{1,3-+3,+} % rule on first col, third to third last col, and last col
 g & 2 & 3 & 4 & 5 & 6\\
    \gcmidrules{1,3-+3rl,+} % same as above, but trim middle
 h & 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}


\section{\texttt{\textbackslash midruleX} - Midrule every X\textsuperscript{th} row}

With this command, you can place a rule
every X rows for the next table made (place command outside of table).
You can configure the step size and what kind of midrule you prefer with the following key-val syntax, with default values below:\\
\cmd{\midruleX{step=5,rule=midrule,cntr=0,head=0,long=false,longhead=0}}\\
\cmd{step} is the number of rows (X) before applying the rule set by \cmd{rule}. \\
Concering \cmd{longtable}s: If \cmd{long} is set to \cmd{true} (or the key is present),
\cmd{\midruleX} will also add a unique \cmd{\label{}} on each row, and if the row starts on a newpage,
resets the row counter to -1*\cmd{longhead}, to ensure you have the same X number of rows after the header of each page of a \cmd{longtable}.\\
Use optional parameter  \cmd{\midruleX*[o|n|f]{}}
to control expansion of the key-val settings (\cmd{[n]} for not expanded is default).
Before you want counting to begin, or anywhere in the table, you could
apply \cmd{\resetmidruleX[cntr]} (\cmd{cnter} is incremented each row) to avoid header
rows being counted and set the row \cmd{cntr} to an arbitrary value.
If you want to skip the first \cmd{x} rows with \cmd{\midruleX}, set \cmd{head=x} (which is equivalent to \cmd{cntr=-x}).
If you want to add a \cmd{midrule} under row \cmd{x}, use \cmd{head*=x} instead.

Note: Use \cmd{\noalign{\resetmiduleX}} if you need place a rule on the same line the reset takes place (ie. in a cell before \cmd{\\}).


\subsection{\texttt{\textbackslash midruleX} examples}

\begin{LTXexample}
\midruleX{step=3,rule=gmidrule,head*=1}
\begin{tabular}{rclc}
                % ^^^ inject midruleX
Num  & . & . & .  \\
1    & & &  \\
2    & & &  \\
3    & & &  \\
4    & \MC[2,2]{Hi\\world}  \\
5    & & &  \\
6    & & &  \\
7    & & &  \\
8    & & &  \\
9    & & &  \\
10   & & &  \\
11   & & &  \\
\resetmidruleX % resest
   % so no bottom gray rule
12   & & &  \\
\end{tabular}
\end{LTXexample}

\pagebreak

Here's an example with long table. Notice the gray rules reliably appear 3 rows after each header.
\begin{verbatim}
\midruleX{long=true,step=3,rule="\gcmidrules{1r,2-}"}
\def\tblhead{\toprule No & Name & Place & other\\\midrule}
\def\tblcontinued{\MC[+l]{Continued...}\\}
\def\tblcontinues{\MC[+r]{...Continues}\\}
\begin{longtable}{rclc}
  \tblhead\endfirsthead
  \tblcontinued\tblhead\endhead % all the lines above this will be repeated on every page
  \tblcontinues\endfoot
  \bottomrule End.\endlastfoot
  \resetmidruleX
1       &           &       &  \\
2       &           &       &    \\
3       &           &       &    \\
4       &           &       &    \\
5       &           &       &    \\
6       &           &       &    \\
7       &           &       &    \\
8       &           &       &    \\
9       &           &       &    \\
10      &           &       &    \\
11      &           &       &    \\
12      & \MC[2,-2](){Hi\\world}     \\
13      &           &       &    \\
14      &           &       &    \\
15      &           &       &    \\\newpage
16      &   \MC[2,2]{Hi\\world}     \\
17      &           &       &    \\
18      &           &       &    \\\resetmidruleX
19      &           &       &    \\
\end{longtable}
\end{verbatim}

\midruleX{long=true,rule="\gcmidrules{1r,2-}"}
\def\tblhead{\toprule No & Name & Place & other\\\midrule}
\def\tblcontinued{\MC[+l]{Continued...}\\}
\def\tblcontinues{\MC[+r]{...Continues}\\}
\begin{longtable}{rclc}
  \tblhead\endfirsthead
  \tblcontinued\tblhead\endhead % all the lines above this will be repeated on every page
  \tblcontinues\endfoot
  \bottomrule End.\endlastfoot
  \resetmidruleX
1       &           &       &  \\
2       &  \MC[_]{hi}         &       &    \\
3       &           &       &    \\
4       &           &       &    \\
5       &           &       &    \\
6       &           &       &    \\
7       &           &       &    \\
8       &           &       &    \\
9       &           &       &    \\
10      &           &       &    \\
11      &           &       &    \\
12      & \MC[2,-2_]{Hi\\world}     \\
13      &           &       &    \\
14      &           &       &    \\
15      &           &       &    \\\newpage
16      &   \MC[2,-2_]{Hi\\world}     \\
17      &           &       &    \\
18      &           &       &    \\\resetmidruleX
19      &           &       &    \\
\end{longtable}

%\begin{tabular}{ll}
%  a \\
%  b \\
%  v \\
%  d \\
%  r \\
%\end{tabular}

Long table X example

\LTXtable{\linewidth}{ltxexample}

%\luadirect{pl.help_wrt(lutabt.actlvl, 'actlevel')}
%\luadirect{pl.help_wrt(lutabt.row_num, 'row')}
%\lttdebugprt

%\settabular{tbrule} % glitch with LTX
%\midrulesat{1,2,3}
%\begin{tabular}{ll}
%  a \\
%  b \\
%  c \\
%  d \\
%  e \\
%\end{tabular}


\end{document}
