TransWikia.com

Mixed table borders

TeX - LaTeX Asked by Younis Bensalah on July 4, 2021

I am trying to draw a table where some lines have (vertical) borders and some lines don’t. I couldn’t find a satisfying way to do this.

Horizontal borders are not the problem obviously, since I can decide where I need a hline, but the vertical ones seem to be fixed in the parameter of the whole tabular environment.

The best I could find was using multicolumn, but I guess that’s not the proper way to do it, since its real purpose is to merge cells.

Here is what I’ve done so far:

begin{tabular}{ cccccccc }
    hline
    multicolumn{1}{|c|}{7} &
    multicolumn{1}{|c|}{16} &
    multicolumn{1}{|c|}{3} &
    multicolumn{1}{|c|}{-1} &
    multicolumn{1}{|c|}{9} &
    multicolumn{1}{|c|}{32} &
    multicolumn{1}{|c|}{4} &
    multicolumn{1}{|c|}{2}
    hline
    0 & 1 & 2 & 3 & 4 & 5 & 6 & 7
end{tabular}

what I want to do

This example code does produce the layout I need, but I’m sure that’s not how it should be done.

tl;dr: I want to produce a table where the first line has horizontal and vertical borders, while the second line doesn’t have any borders at all.

3 Answers

You've done it almost the right way. If you want to have a shorter code, you can define a command that will replace all these multicolumns. Here is a way to do it, with some minor improvements to your table:

documentclass{book}
usepackage[utf8]{inputenc}
usepackage[T1]{fontenc}
usepackage{array}
usepackage{xparse}
DeclareExpandableDocumentCommandfcell{O{c}m}{multicolumn{1}{>{$}#1<{$}|}{#2}}

begin{document}

renewcommandarraystretch{1.25}
begin{tabular}{*{8}{>{$}c<{$}}}
    hline
    multicolumn{1}{|c|}{7} & fcell{16} & fcell{3} & fcell{-1} & fcell{9} & fcell{32} & fcell{4} & fcell{2}
    hline
    0 & 1 & 2 & 3 & 4 & 5 & 6 & 7
end{tabular}

end{document} 

enter image description here

Answered by Bernard on July 4, 2021

You wrote

... The best I could find was using multicolumn, but I guess that's not the proper way to do it, since its real purpose is to merge cells."

Not quite. I'd say that multicolumn has two real purposes: (a) to merge cells -- the purpose you mention -- and (b) to change the column type of its argument, be that a single cell or a range of cells.

To simplify switching between column types, it's frequently handy to set up a shortcut macro. In the example below, the macro mr -- short for "multicolumn-right", I suppose -- is set up for just that purpose.

enter image description here

documentclass{article}
usepackage{array}
newcommand{mr}[1]{multicolumn{1}{r|}{#1}}
begin{document}
$begin{array}{ *{8}{r} }
    hline
    multicolumn{1}{|r|}{7} 
      & mr{16} & mr{3} & mr{-1} & mr{9} & mr{32} & mr{4} & mr{2}
    hline
    0 & 1 & 2 & 3 & 4 & 5 & 6 & 7
    hline
end{array}$
end{document}

Answered by Mico on July 4, 2021

With nicematrix.

documentclass{article}
usepackage{nicematrix}
begin{document}
$begin{NiceMatrix}[hvlines,last-row=2]
    7 & 16 & 3 & -1 & 9 & 32 & 4 & 2 
    0 & 1  & 2 & 3  & 4 & 5  & 6 & 7 
end{NiceMatrix}$
end{document}

Output of the above code

Answered by F. Pantigny on July 4, 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