3

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.

1
  • Seven of the eight \multicolumn{1}{|c|}{...} statements should have only one, not two, vertical bars. E.g, the first statement could be \multicolumn{1}{|c|}{7}, and the remaining seven should be of the form \multicolumn{1}{c|}{...}. This issue becomes very evident if you load the array package -- or if you load a package (such as tabularx) that, in turn, loads the array package.
    – Mico
    Commented Nov 19, 2015 at 20:49

3 Answers 3

2

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}
\DeclareExpandableDocumentCommand\fcell{O{c}m}{\multicolumn{1}{>{$}#1<{$}|}{#2}}

\begin{document}

\renewcommand\arraystretch{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

1

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}
1

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .