Advertisement
Guest User

Unintended Tabular Colouring MWE

a guest
Jul 6th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Latex 2.61 KB | None | 0 0
  1. \documentclass{article}
  2. \usepackage{etoolbox}
  3. \usepackage[table]{xcolor}
  4. \usepackage{booktabs} % or ctable
  5.  
  6. \colorlet{text}{black}
  7. \colorlet{page}{white}
  8. \colorlet{tableheadcolor}{orange!30!page}
  9.  
  10. % The use of \showrowcolors and \hiderowcolors is an idea from Ulrike
  11. % Fischer's answer here: <https://tex.stackexchange.com/a/494954/73317>
  12. \renewcommand{\toprule}{%
  13.     \showrowcolors
  14.     \arrayrulecolor{text}\specialrule{\heavyrulewidth}{\abovetopsep}{0pt}%
  15.     \arrayrulecolor{tableheadcolor}\specialrule{\belowrulesep}{0pt}{0pt}%
  16.     \arrayrulecolor{text}%
  17.     \rowcolor{tableheadcolor}%
  18. }
  19.  
  20. \apptocmd\midrule{\hiderowcolors}{}{\FAILED}
  21.  
  22. \makeatletter
  23.  
  24. \let\@BTrule@ORI=\@BTrule
  25. \let\my@BTrule=\@BTrule
  26.  
  27. % Modified version of \@BTrule that doesn't do \vskip\@aboverulesep, for use
  28. % when the corresponding vertical space should be coloured.
  29. \patchcmd{\my@BTrule}{%
  30.     \ifnum\@lastruleclass=\z@\vskip\@aboverulesep\else
  31.   }{%
  32.     \ifnum\@lastruleclass=\z@\else
  33.   }{}{\FAILED}
  34.  
  35. \let\@arraycrORI=\@arraycr
  36.  
  37. % The “master counter” hackery is explained in the TeXbook appendix D (Dirty
  38. % Tricks), pp. 385-386. It is also mentioned in the array.sty implementation
  39. % notes concerning \@arraycr.
  40. \renewcommand*{\@arraycr}{%
  41.   % Increase the master counter. This is needed to prevent TeX from
  42.   % prematurely finishing the alignment entry in case \\ was followed by '&'
  43.   % (when the \futurelet from \@ifnextchar causes TeX to read a '&', this
  44.   % finishes the entry unless the master counter has a different value than it
  45.   % had when the entry was started).
  46.   \relax\iffalse{\fi\ifnum 0=`}\fi
  47.   % Each of the two branches takes care of decreasing the master counter.
  48.   \@ifnextchar\midrule
  49.     {\@firstoftwo{\my@endtablehead}}% gobble the following \midrule
  50.     {\my@closebrace@and@arraycrORI}%
  51. }
  52.  
  53. \newcommand*{\my@endtablehead}{%
  54.  \ifnum 0=`{}\fi % the second brace decreases the master counter
  55.  \@arraycrORI[\aboverulesep]% this colours the additional space with the
  56.                             % current row color
  57.  \noalign{\global\let\@BTrule\my@BTrule}% temporarily modify \@BTrule
  58.  \midrule
  59.  \noalign{\global\let\@BTrule\@BTrule@ORI}% restore it
  60. }
  61.  
  62. \newcommand*{\my@closebrace@and@arraycrORI}{%
  63.   \ifnum 0=`{}\fi % the second brace decreases the master counter
  64.   \@arraycrORI
  65. }
  66.  
  67. \makeatother
  68.  
  69. \rowcolors{1}{tableheadcolor}{tableheadcolor}
  70.  
  71. \begin{document}
  72. \pagecolor{blue!10}
  73.  
  74. \begin{tabular}{l}
  75.    a \\
  76.    b \\
  77.    c
  78. \end{tabular}
  79.  
  80. \bigskip
  81.  
  82. \begin{tabular}{l}
  83.    \midrule
  84. \end{tabular}
  85.  
  86. \bigskip
  87.  
  88. \begin{tabular}{l}
  89.    a \\
  90.    b \\
  91.    c
  92. \end{tabular}
  93.  
  94. \end{document}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement