TransWikia.com

Globally styling vertical and horizontal rules in both tabular and longtable

TeX - LaTeX Asked on March 3, 2021

I would like to change all the horizontal rules in all tables to be blue and all the vertical ones to be red by applying a .sty file. I cannot change the actual LaTeX markup of the tables themselves as they are automatically generated by Sphinx.

Currently, I have this:

% my .sty file
% red vertical lines
arrayrulecolor{red}
% table hline formatting
letmyhlinehline
renewcommand{hline}{arrayrulecolor{blue}myhline}

However, this only works for tabular but not for longtable.

Here is an example document that illustrates this problem:

documentclass{article}
usepackage{colortbl}
usepackage{longtable}

% red vertical lines
arrayrulecolor{red}
% table hline formatting
letmyhlinehline
renewcommand{hline}{arrayrulecolor{blue}myhline}

% I cannot change anything after this line!!!
begin{document}
begin{longtable}{|c|c|}
hline
a & b 
hline
endfirsthead
c & d 
hline
end{longtable}

begin{tabular}{|c|c|}
hline
e & f 
hline
g & h 
hline
end{tabular}
end{document}

Below is the output:

pic

As you can see, the tabular table is styled correctly with blue horizontal rules and red vertical rules. However, the longtable one is not styled correctly.

How do I make the longtable‘s hlines behave just like the tabular ones?

I am using pdfTeX, Version 3.14159265-2.6-1.40.20 (TeX Live 2019).

2 Answers

Okay, I figured it out. After digging through longtable.sty I found out that it was using LT@hline so I overwrite these as well.

In a wtf.sty file I now have these:

% red vertical lines
arrayrulecolor{red}
% table hline formatting
letmy@hlinehline
renewcommand{hline}{arrayrulecolor{blue}my@hline}
letmyLT@hlineLT@hline
renewcommand{LT@hline}{arrayrulecolor{blue}myLT@hlinearrayrulecolor{red}}

(You can inline this in your .tex document with makeatletter and makeatother)

It is an ugly hack but seems to work.

documentclass{article}
usepackage{colortbl}
usepackage{longtable}
usepackage{wtf}

begin{document}

begin{tabular}{|c|c|}
hline
e & f 
hline
g & h 
hline
end{tabular}

begin{longtable}{|c|c|}
hline
a & b 
hline
endfirsthead
c & d 
hline
end{longtable}

begin{tabular}{|c|c|}
hline
e & f 
hline
g & h 
hline
end{tabular}
end{document}

pic

NOTE: in general, overwriting built-in macros like hline is not recommended. However, I have no choice here since I am trying to change the behaviour of generated output from Sphinx.

Answered by Daniel on March 3, 2021

hhline is a way to do such thing:

documentclass{article}
usepackage{xcolor}
usepackage{colortbl}
usepackage{longtable}
usepackage{ehhline}
usepackage{xparse}

makeatletter
defhline@color{black}
newcommand{hlinecolor}[1]{defhline@color{#1}}
newcommand{hsl}{leadershbox{textcolor{hline@color}{rule{0.1pt}{0.4pt}}}hfil}
makeatother
newcommand{myhline}{hhline{!{hsl}!{hsl}}}

% I cannot change anything after this line!!!
begin{document}
arrayrulecolor{blue}
hlinecolor{red}
centering
begin{longtable}{|c|c|}
myhline
a & b 
myhline
endfirsthead
c & d 
myhline
end{longtable}

hlinecolor{teal}
begin{tabular}{|c|c|}
myhline
e & f 
myhline
g & h 
myhline
end{tabular}
end{document}

enter image description here

Answered by ZhiyuanLck on March 3, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP