0

Here I append code to \\ that looks ahead to see whether the \end in \end{tabular} is up next to bat. This code is hidden within a \noalign. The \\ has code that looks for a star and then a [ before finally calling the \cr. I suspect that herein lies the problem.

I am not sure whether this applies:

Code

See Log Output for \typeout{\noexpand\reserved@a value: \meaning\reserved@a}

\documentclass{article}
\usepackage{fontspec}
\usepackage{booktabs}

\catcode`@=11 % or \catcode"0040=11 or \makeatletter to change category code of @ to 11 and temporarily to access kernel macro \@tabularcr
\let\clone@tabularcr\@tabularcr%
\def\@tabularcr{\clone@tabularcr \mymidline}%

% Using \hline and \midrule as a model, I made my own hybrid with some extra end of tabular logic
\def\mymidline{%
  \noalign{\ifnum0=`}\fi\@aboverulesep=\aboverulesep\global\@belowrulesep=\belowrulesep \futurelet
    \reserved@a\@mymidline}%
\def\@mymidline{\typeout{\noexpand\reserved@a value: \meaning\reserved@a}\ifx\reserved@a\end\else% <-- Here is the value test. I need \reserved@a to eventually equal \end
               \vskip\@aboverulesep%
               \hrule \@height \lightrulewidth%
               \vskip\@belowrulesep%
             \fi
      \ifnum0=`{\fi}}
\catcode`@=12 % or \catcode"0040=12 or \makeatother to restore category code of @ to 12

\begin{document}
\makeatletter
\begin{tabular}{ccc}
col1 & col2 & col3 \\
\meaning\@tabularcr & col2 & col3 \\
\meaning\\ & col2 & col3 \\
col1 & col2 & col3 \\
\meaning\clone@tabularcr & col2 & col3 \\
\end{tabular}
\makeatother
\end{document}

Current Output

enter image description here

Desired Output

enter image description here

  • only difference between your "current" and "desired" images appears to be the final line, how does that relate to your question which asks about white space? – David Carlisle Nov 16 '16 at 15:34
  • @DavidCarlisle Final line in final row ;) – Jonathan Komar Nov 16 '16 at 15:35
  • @DavidCarlisle I will clarify that. I did not mean "white space", rather a technical space in TeX munching terms. See the log output. I would like the final typeout to yield the definition of \end, not "blank space". – Jonathan Komar Nov 16 '16 at 15:37
  • also note that \@tabularcr is the wrong macro to patch if array package is used (or colortbl, or tabularx or...) You could use \@ifnextchar instead of \futurelet as the main difference between them is that \@ifnextchar skips space but better would be to add your line after \\ has tested and skipped the space, as adding it where you do you stops the [2pt] from working. – David Carlisle Nov 16 '16 at 15:47
  • @DavidCarlisle Thx I am aware of the array package changes. You hit the nail on the head with that last part. I'd like to change my code to start testing after the [2pt]. I was imitating \hline code while trying to hide \futurelet in the \noalign – Jonathan Komar Nov 16 '16 at 15:56

1 Answers1

1

as \\ is anyway skipping over spaces looking for * you can let it do the work and ask it if it found a \end

this only works for the latex kernel tabular although with some name changes could be made for array package as well.

\documentclass{article}

\usepackage{booktabs}

\catcode`@=11 % or \catcode"0040=11 or \makeatletter to change category code of @ to 11 and temporarily to access kernel macro \@tabularcr
\def\addmyrule{\noalign{%
           \vskip\@aboverulesep%
           \hrule \@height \lightrulewidth%
           \vskip\@belowrulesep}}

\let\old@xtabularcr\@xtabularcr

\def\@xtabularcr{%
\ifx\@let@token\end\else
\aftergroup\aftergroup\aftergroup
\aftergroup\aftergroup\aftergroup
\aftergroup
\addmyrule
\fi
\old@xtabularcr}

\catcode`@=12 % or \catcode"0040=12 or \makeatother to restore category code of @ to 12

\begin{document}
\makeatletter
\begin{tabular}{ccc}
col1 & col2 & col3 \\
\meaning\@tabularcr & col2 & col3 \\
\meaning\\ & col2 & col3 \\
col1 & col2 & col3 \\
\meaning\clone@tabularcr & col2 & col3 \\
\end{tabular}
\makeatother
\end{document}
David Carlisle
  • 757,742
  • Whoa whoa, right when I think i am getting the hang of this you throw another unknown in there. Why is \@let@token in there? I assume it is part of the original \, but why introduce new mechanisms? I am sure you would know why. – Jonathan Komar Nov 16 '16 at 17:55
  • @macmadness86 \@let@token is the token used in the \futurelet that's inside \@ifnextchar so \\ has just tested it to see if it is * or [ but after that you can look to see what it was. – David Carlisle Nov 16 '16 at 18:21
  • Could you show which }s you are skipping over? I can count 2 in \@ifnextchar [\@argtabularcr {\ifnum 0=‘{\fi }\cr } and 1 in the expanded \@argtabularcr: \ifnum 0=‘{\fi }\ifdim #1>\z@ \unskip \@xargarraycr {#1}\else \@yargarraycr {#1}\fi. Am I right that you are skip those? This would mean f(3) n^2-1= x where x is the number of \aftergroups. So for the array package, I'd have to count them just like you did here, except in a redefinition of \@xarraycr. – Jonathan Komar Nov 17 '16 at 11:38
  • @macmadness86 probably. (I just guessed, to be honest:-) initially I wrote 3 but it appeared too early so I did 7:-) I could analyse it exactly but not just now... – David Carlisle Nov 17 '16 at 11:51
  • Haha ok! Well, with array loaded, it is 3. But then with arydshln loaded on top of that, it jumps back up to 7, because the \cr is replaced with \adl@cr, which expands to another }: \noalign {\adl@@cr \z@ }. This leads me to suspect that using the \ mechanism may be more trouble than it is worth (or maybe this annoyance is commonplace in the development of LaTeX) – Jonathan Komar Nov 17 '16 at 13:43