5

Consider the following MWE:

\documentclass{scrbook}

\usepackage{array}

\begin{document}

\renewcommand{\tabcolsep}{2pt}
\begin{tabular}{|c||c|c|c||c|}
    \firsthline
    1&2&3&4&5\\\hline
    \multicolumn{1}{c||}{}&6&7&8&\multicolumn{1}{c}{}\\
    \cline{2-4}
\end{tabular}

\end{document}

Here is the result:

enter image description here

How to get the last horizontal line aligned nicely with the bottom middle cells?

P.S. Sometimes I think it would be easier to draw tables in LaTeX manually with tikz.

8
  • 1
    Off-topic: \tabcolsep is defined as a length. Hence, I'd replace \renewcommand{\tabcolsep}{2pt} with \setlength{\tabcolsep}{2pt}.
    – Mico
    Feb 19, 2021 at 9:45
  • 1
    hhline package is your friend Feb 19, 2021 at 9:56
  • 1
    There is a quite new and power package, nicematrix, which is worth exploring
    – Rmano
    Feb 19, 2021 at 9:57
  • 1
    @DavidCarlisle Then please post an answer. I've been trying for at least ten minutes to make it work...
    – campa
    Feb 19, 2021 at 10:05
  • 1
    @DavidCarlisle I know what hhline does. Try it an you will see that it doesn't work in this case.
    – facetus
    Feb 19, 2021 at 11:31

3 Answers 3

3

Here is a solution with {NiceTabular} of nicematrix. That environment is similar to the classical {tabular} of array but creates PGF/Tikz nodes under the cells, rows and columns. Then, you can use Tikz to draw whatever rule you want.

\documentclass{scrbook}
\usepackage{nicematrix,tikz}

\begin{document}
\setlength{\tabcolsep}{2pt}
\begin{NiceTabular}{c||c|c|c||c}[t]
    \hline
    1&2&3&4&5\\
    \hline
    &6&7&8&\\
\CodeAfter 
\tikz \draw (1-|1) -- (2-|1) ;
\tikz \draw (1-|6) -- (2-|6) ;
\tikz \draw ([xshift=-\doublerulesep-\arrayrulewidth]3-|2) -- (3-|5) ;
\end{NiceTabular}
\end{document}

You need several compilations (because nicematrix uses PGF/Tikz nodes).

Output of the above code

1
  • TikZ is the best, my favorite library. Thanks!
    – facetus
    Feb 19, 2021 at 10:26
3

A fast and elegant way with the excellent nicematrix:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{nicematrix}
\begin{document}
\setlength{\tabcolsep}{2pt}
\begin{NiceTabular}{*{7}c}[hvlines,corners,cell-space-top-limit=3pt]
    1 & & 2 & 3 & 4 & & 5 \\
      & \makebox[0.1pt]{} & 6 & 7 & 8 & \makebox[0.1pt]{}& \\
\end{NiceTabular}
\end{document}

enter image description here

...you can create a command with the \makebox[0.1pt]{}

1
  • Thank you for pointing to this nice library. I love it.
    – facetus
    Feb 19, 2021 at 10:47
2

You can arrange an empty column so that you can control the \cline start, or you could use \hhline but you need a version of b (denoted . here) that does not force a double rule width entry into the row.

enter image description here

\documentclass{scrbook}

\usepackage{array}

\usepackage{hhline}

\makeatletter 
% add . like b but singleline
\def\HH@loop{%
  \ifx\@tempb`\def\next##1{\the\toks@\cr}\else\let\next\HH@let
  \ifx\@tempb|\if@tempswa\HH@add{\hskip\doublerulesep}\fi\@tempswatrue
          \HH@add{\@tempc\vline\@tempc}\else
  \ifx\@tempb:\if@tempswa\HH@add{\hskip\doublerulesep}\fi\@tempswatrue
      \HH@add{\@tempc\HH@box\arrayrulewidth\arrayrulewidth\@tempc}\else
  \ifx\@tempb##\if@tempswa\HH@add{\hskip\doublerulesep}\fi\@tempswatrue
         \HH@add{\@tempc\vline\@tempc\copy\@ne\@tempc\vline\@tempc}\else
  \ifx\@tempb~\@tempswafalse
           \if@firstamp\@firstampfalse\else\HH@add{&\omit}\fi
              \HH@add{\hfil}\else
  \ifx\@tempb-\@tempswafalse
           \if@firstamp\@firstampfalse\else\HH@add{&\omit}\fi
              \HH@add{\leaders\hrule\@height\arrayrulewidth\hfil}\else
  \ifx\@tempb=\@tempswafalse
       \if@firstamp\@firstampfalse\else\HH@add{&\omit}\fi
       \HH@add
          {\rlap{\copy\@ne}\leaders\copy\@ne\hfil\llap{\copy\@ne}}\else
  \ifx\@tempb t\HH@add{\rlap{\HH@box\doublerulesep\z@}}\else
  \ifx\@tempb b\HH@add{\rlap{\HH@box\z@\doublerulesep}}\else
  \ifx\@tempb .\HH@add{\smash{\rlap{\HH@box\z@\doublerulesep}}}\else
  \ifx\@tempb\@sptoken\let\next\HH@spacelet\else
  \PackageWarning{hhline}%
      {\meaning\@tempb\space ignored in \noexpand\hhline argument%
       \MessageBreak}%
  \fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi
  \next}
\makeatother

\begin{document}

\renewcommand{\tabcolsep}{2pt}
\begin{tabular}{|c||c|c|c||c|}
    \firsthline
    1&2&3&4&5\\\hline
    \multicolumn{1}{c||}{}&6&7&8&\multicolumn{1}{c}{}\\
    \cline{2-4}
\end{tabular}


\bigskip

\begin{tabular}{|c@{}c@{}||c|c|c||c|}
    \firsthline
     \multicolumn{1}{|c}{1}&&2&3&4&5\\\hline
    \multicolumn{1}{@{}c@{}}{}&&6&7&8&\multicolumn{1}{c}{}\\
    \cline{2-5}
\end{tabular}

\bigskip

\begin{tabular}{|c||c|c|c||c|}
    \firsthline
    1&2&3&4&5\\\hline
    \multicolumn{1}{c||}{}&6&7&8&\multicolumn{1}{c}{}\\
    \hhline{~|.|---|.|}
\end{tabular}



\end{document}

You must log in to answer this question.

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