5

How to obtain something like this ?

enter image description here

The only way that I have found for trimming horizontal lines in a table is using booktabs. The problem with booktabs is that it doesn't manage vertical lines well.

I tried this code:

\documentclass{article} 
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{xcolor}
\usepackage{colortbl}
\usepackage{booktabs}

\begin{document}

\arrayrulecolor{gray}
\begin{tabular}{!{\color{gray}\vline}ll!{\color{gray}\vline}}
  a & b \\
  c & d \\
  \cmidrule(rl){1-2}
  e & f
\end{tabular}

\end{document}

But what I get the following output, which is not what I am looking for: enter image description here

4 Answers 4

5

enter image description here

\documentclass{article} 
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{xcolor}
\usepackage{colortbl}


\begin{document}

\arrayrulecolor{red}
\begin{tabular}{|ll|}
  a & b \\
  c & d \\
\multispan{2}{\color{red}\vline\hspace*{.5em}\leaders \hrule height\arrayrulewidth\hskip\fill\hspace*{.5em}\vline}\\
  e & f
\end{tabular}

\end{document}
2
  • Why if I put your code in a \newcommand with options like this: \newcommand{\htrimmedline}[1][2]{\multispan{#1}{\color{gray!40}\hspace*{.2em}\vline\hspace{.2em}\leaders \hrule height\arrayrulewidth\hskip\fill\hspace*{.2em}\vline\hspace{.2em}}\\} when I compile I get the error Misplaced omit, while if I put it in a newcommand without optional argument like this: \newcommand{\htrimmedline}[1]{\multispan{#1}{\color{gray!40}\hspace*{.2em}\vline\hspace{.2em}\leaders \hrule height\arrayrulewidth\hskip\fill\hspace*{.2em}\vline\hspace{.2em}}\\}, it works?
    – Robbo
    Jan 9, 2014 at 12:34
  • 1
    @Robbo \multicolumn/\multispan and friends have to expand to a sequence of tokens starting with \omit as TeX uses a special pre-scan to see if it should or should not start a normal table cell. You can have nothing before \omit not even \relax (which does nothing). The machinery used for testing if there is an optional argument does not work purely by expansion. Jan 9, 2014 at 12:52
2

Here's a way using scalerel's \stretchleftright macro:

\documentclass{article} 
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{xcolor}
\usepackage{booktabs}
\usepackage{colortbl}
\usepackage{scalerel}
\begin{document}
\arrayrulecolor{gray}
\stretchleftright{\color{gray}|}{%
\begin{tabular}{ll}
  a & b \\
  c & d \\
  \cmidrule(rl){1-2}
  e & f
\end{tabular}%
}{\color{gray}|}
\end{document}

enter image description here

2

With {NiceTabular} of nicematrix, you have directly the expected output because, in that environment, the vertical rules are never broken.

\documentclass{article} 
\usepackage{xcolor}
\usepackage{nicematrix}
\usepackage{booktabs}

\begin{document}

\begin{NiceTabular}{|ll|}[rules/color=gray]
  a & b \\
  c & d \\
  \cmidrule(rl){1-2}
  e & f
\end{NiceTabular}

\end{document}

Output of the above code

1

I will give an alternative solution with tblr environment of tabularray package.

Note that you need to put trim option lr inside the square brackets.

\documentclass{article} 

\usepackage{xcolor}
\usepackage{tabularray}
\UseTblrLibrary{booktabs}

\begin{document}

\begin{tblr}{|[gray]ll|[gray]} 
  a & b \\
  c & d \\
  \cmidrule[gray,lr]{1-2}
  e & f
\end{tblr}

\end{document}

enter image description here

You must log in to answer this question.

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