12

I am just trying to get the table to look like the picture in the LaTeX Wiki book:

Table example

In this example shown, the very top and bottom \hline are thicker and darker than the ones in the middle of the table. The example does not show any special formatting, but when I do this in my document they all have the same thickness and darkness (the lines are all identical). I want the horizontal \hline to be thinner and lighter in the middle of the table as shown in the picture. Playing with the height and width had no discernible effect on this from what I could tell. How do I do this?

Werner
  • 603,163
Dan
  • 969
  • 3
  • 9
  • 23

4 Answers4

14

You can define your own \thickhline which is based on \hline, but with a different \arrayrulewidth:

enter image description here

\documentclass{article}

\makeatletter
\def\thickhline{%
  \noalign{\ifnum0=`}\fi\hrule \@height \thickarrayrulewidth \futurelet
   \reserved@a\@xthickhline}
\def\@xthickhline{\ifx\reserved@a\thickhline
               \vskip\doublerulesep
               \vskip-\thickarrayrulewidth
             \fi
      \ifnum0=`{\fi}}
\makeatother

\newlength{\thickarrayrulewidth}
\setlength{\thickarrayrulewidth}{2\arrayrulewidth}

\begin{document}

\begin{tabular}{ l | c | r }
  \hline
  1 & 2 & 3 \\
  \hline
  4 & 5 & 6 \\
  \hline
  7 & 8 & 9 \\
  \hline
\end{tabular}
\qquad
\begin{tabular}{ l | c | r }
  \thickhline
  1 & 2 & 3 \\
  \hline
  4 & 5 & 6 \\
  \hline
  7 & 8 & 9 \\
  \thickhline
\end{tabular}

\end{document}

In the above example, \thickhline inserts an \hrule that has a width double that of \arrayrulewidth. The latter has a default of 0.4pt. You can change \thickhline's width to whatever you want by adjusting \thickarrayrulewidth.

Werner
  • 603,163
  • This is very close, but the default \hrule width is still much thicker than that shown in the example. The standard \hrule is already thick enough, I need an even thinner one in between. I tried just reversing 2\arrayrulewidth to arrayrulewidth\2 but that won't compile. – Dan Jan 04 '18 at 22:42
  • @Dan: Then adjust \arrayrulewidth to something smaller than 0.4pt. Note that your viewer might snap to certain widths, making things appear thicker/thinner than they truly are. – Werner Jan 04 '18 at 22:44
  • I tried 0.1 and 0.01 and neither is thin enough. It barely made any change. It almost looks like maybe the inner lines are in a lighter gray in that image, maybe that will help. Not sure why their example appears to use formatting but they use it as a basic example without showing that formatting. – Dan Jan 04 '18 at 22:47
  • @Dan: Try this paste. Output: Adobe Reader; SumatraPDF, both @ 400% zoom. – Werner Jan 04 '18 at 23:08
  • So the default settings look nothing like the example they showed in their book is the issue here. I expected it to look like the book. They drew the image in the book as an SVG, it's not a LaTeX screenshot. – Dan Jan 04 '18 at 23:14
  • 1
    @Dan -- i haven't followed the links, but \arrayrulewidth/2 isn't the correct syntax. if you want a fractional value, you have to use a decimal: .5\arrayrulewidth. (or 0.5 if you're really fastidious.) – barbara beeton Jan 04 '18 at 23:17
  • @Dan: I assume their rendering as SVG allows for different zoom levels within the browser to still present the content in a reasonable fashion. Not exactly sure how they rendered the original tables though. – Werner Jan 04 '18 at 23:22
  • I've marked this as accepted answer (and upvoted) because the issue here is the book not showing an accurate representation of LaTeX output but instead they drew their example with a vector graphic (view SVG source here). – Dan Jan 04 '18 at 23:22
  • @barbarabeeton thanks, that was a typo sorry. But I did not know how to do that so thank you. – Dan Jan 04 '18 at 23:23
11

Three other possibilities:

  • the makecell package, in addition to allowing linebreaks in standard cells, defines the \Xhline and \Xcline commands which have the rule thickness as a mandatory argument.
  • the boldline package (from the shipunov bundle) defines \hlineB and clineB which take a numeric argument (how many times \arrayrulewidth, which defaults to 0.4pt). In addition, you can have varying thickness vertical rules, with V{some number} in the table preamble, in the place of |.
  • booktabs – which isn't really designed for that ;o) – allows to have thick horizontal rules compatible with vertical lines, with the \specialrule command, which takes three arguments: its thickness and the padding above and below. So I defined a shortcut for which the padding is set to 0. Advantage of booktabs: it's compatible with \arrayrulecolor whereas the first two are not.

Demo:

\documentclass{article}
\usepackage{array, boldline, makecell, booktabs}
\newcommand\btrule[1]{\specialrule{#1}{0pt}{0pt}}
\usepackage[svgnames, table]{xcolor}

\begin{document}

\begin{tabular}{l!{\vline width 1pt}c | r }
  \arrayrulecolor{IndianRed} \Xhline{1pt}
  1 & 2 & 3 \\
  \hline
  4 & 5 & 6 \\
  \hline
  7 & 8 & 9 \\
  \Xhline{1pt}
\end{tabular}
\qquad
\begin{tabular}{ lV{2.5}c | r }
  \hlineB{2.5}
  1 & 2 & 3 \\
  \hline
  4 & 5 & 6 \\
  \hline
  7 & 8 & 9 \\
  \hlineB{2.5}
\end{tabular}
\qquad
\begin{tabular}{ l!{\color{IndianRed}\vline width 1pt}c | r }
  \arrayrulecolor{IndianRed}
  \btrule{1pt}
  1 & 2 & 3 \\
  \arrayrulecolor{black} \hline
  4 & 5 & 6 \\
  \hline
  7 & 8 & 9 \\
  \arrayrulecolor{IndianRed}\btrule{1pt}
\end{tabular}

\end{document}  

enter image description here

Bernard
  • 271,350
4

You can define your \thickhline using TeX primitives:

\def\thickhline{\noalign{\hrule height.8pt}}

So, the example from this other answer looks like:

\documentclass{article}

\def\thickhline{\noalign{\hrule height.8pt}}

\begin{document}

\begin{tabular}{ l | c | r } \hline 1 & 2 & 3 \ \hline 4 & 5 & 6 \ \hline 7 & 8 & 9 \ \hline \end{tabular} \qquad \begin{tabular}{ l | c | r } \thickhline 1 & 2 & 3 \ \hline 4 & 5 & 6 \ \hline 7 & 8 & 9 \ \thickhline \end{tabular}

\end{document}

with exactly the same output.

Dan
  • 969
  • 3
  • 9
  • 23
wipet
  • 74,238
2

With {NiceTabular} of nicematrix, you specify all the characteristic of a TikZ rule (eg thick) in optional argument of | and \Hline (the version of \hline in nicematrix).

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

\begin{document}

\setlength{\arrayrulewidth}{0.2pt}

\begin{NiceTabular}{|c|[tikz=thick]c|c|} \Hline[tikz=thick] 1 & 2 & 3 \ \Hline 4 & 5 & 6 \ \Hline 7 & 8 & 9 \ \Hline[tikz=thick] \end{NiceTabular}

\end{document}

Output of the first code

If you want, you can define shortcuts with the standard commands \newcolumntype and \newcommand (or \def of TeX, etc.).

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

\begin{document}

\setlength{\arrayrulewidth}{0.2pt}

\newcolumntype{I}{|[tikz=thick]} \newcommand{\MyHline}{\Hline[tikz=thick]}

\begin{NiceTabular}{|cIc|c|} \MyHline 1 & 2 & 3 \ \Hline 4 & 5 & 6 \ \Hline 7 & 8 & 9 \ \MyHline \end{NiceTabular}

\end{document}

The output is the same.

It's still possible to add an option to that command \MyHline and that letter I to change the color of the rule.

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

\begin{document}

\setlength{\arrayrulewidth}{0.2pt}

\newcolumntype{I}{|[tikz=thick]} \newcommand{\MyHline}{\Hline[tikz=thick]}

\begin{NiceTabular}{|cI[color=blue]c|c|} \MyHline[color=red] 1 & 2 & 3 \ \Hline 4 & 5 & 6 \ \Hline 7 & 8 & 9 \ \MyHline \end{NiceTabular}

\end{document}

Output of the third code

However, nicematrix provides a tool to define both the letter and the command for the same type of rule (and these definitions will be available only in the environments of nicematrix).

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

\begin{document}

\setlength{\arrayrulewidth}{0.2pt}

\NiceMatrixOptions { custom-line = { command = MyHline , letter = I , tikz = { color = red, line width = 1 pt } , total-width = 1pt } }

\begin{NiceTabular}{|cIc|c|} \MyHline 1 & 2 & 3 \ \Hline 4 & 5 & 6 \ \Hline 7 & 8 & 9 \ \MyHline \end{NiceTabular}

\end{document}

With this code, a reservation is done for the rules (vertically and horizontally) thanks to the key total-width.

Output of the last code

F. Pantigny
  • 40,250