2

The following code creates two 2-by-2 tables one below the other. In the first table a horizontal line separates the two rows of the second column, whereas in the second table a horizontal line separates the two rows of the first column.

\documentclass{article}
\usepackage{multirow}
\begin{document}
\begin{tabular}{ |c||c| }
\hline
A & B\\
\cline{2-2}
C & D\\
\hline
\end{tabular}

\vspace{.3cm}

\begin{tabular}{ |c||c| }
\hline
A & B\\
\cline{1-1}
C & D\\
\hline
\end{tabular}
\end{document}

When compiled with pdflatex, the following output is produced.

Different behavior of cline when used on the left vs. on the right

Note the difference between the first and the second table. In the first table the horizontal separator is contained between the column's two vertical borders, but in the second table the horizontal separator protrudes beyond the column's right border all the way to the right column's left border.

I'd like the horizontal separator in the second table to behave in a similar way to the horizontal separator in the first table. How can this be achieved?

3 Answers 3

3

Use hhline for that. Also, note the difference for the extremities of the upper and lower lines.

\documentclass{article}
\usepackage{multirow}
\usepackage{hhline}

\begin{document}
\begin{tabular}{ |c||c| }
\hline
A & B\\
\cline{2-2}
C & D\\
\hline
\end{tabular}

\vspace{.3cm}

\begin{tabular}{ |c||c| }
\hhline{|--|}
A & B\\
\hhline{|~||-|}
C & D\\
\hhline{|--|}
\end{tabular}
\end{document} 

enter image description here

4

A possible solution with tblr environment of tabularray package. There are leftpos and rightpos options for hlines and clines in version 2021N (already in CTAN, need several days to reach TeXLive or MiKTeX).

When leftpos/rightpos has value 0, the hline/cline will touch the first vline; when leftpos/rightpos has value 1, the hline/cline will touch all vlines.

\documentclass{article}

\usepackage{tabularray}

\begin{tblr}{ |c||c| }
\hline
A & B\\
\cline[leftpos=0]{2-2}
C & D\\
\hline
\end{tblr}

\vspace{.3cm}

\begin{tblr}{ |c||c| }
\hline
A & B\\
\cline[rightpos=0]{1-1}
C & D\\
\hline
\end{tblr}

\end{document}

enter image description here

2

The environment {NiceTabular} of nicematrix has (at this time) no option to trim the command \cline (maybe in a future version).

However, that answer wants to show that nicematrix provides tools to programm such a command without using the internals of nicematrix.

Indeed, the environment {NiceTabular} creates PGF/Tikz nodes under the cells, rows and columns of the tabular and provide access to these nodes to the user.

In the following programmation, the command \mycline has a starred version \mycline* which trims the rule on the right side.

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


\ExplSyntaxOn
\makeatletter

\NewDocumentCommand { \mycline } { s m }
  { \__aad_mycline:nw {#1}#2 \q_stop \ignorespaces }

\cs_new_protected:Npn \__aad_mycline:nw #1#2-#3\q_stop
  {  
    \tl_gput_right:Nx \g_nicematrix_code_after_tl
      {
        \__aad_mycline:nnnn
          { \int_use:c { c@iRow } }  
          { #2 }
          { \int_eval:n { #3 + 1 } }
          { 
            \bool_if:nTF { #1 } 
              { \dim_eval:n { \arrayrulewidth + \doublerulesep } }
              { 0 pt }
          }
      }
  }

% #1 is the number of row
% #2 is the number of the first column of the \mycline
% #3 is the number of the last column of the \mycline
% #4 is the length to trim on the *right* side
\cs_new_protected:Nn \__aad_mycline:nnnn 
  { \tikz \draw (#1-|#2) -- ([xshift=-#4]#1-|#3) ; }

\makeatother
\ExplSyntaxOff

\begin{document}

\begin{NiceTabular}{|c||c|}
\hline
A & B\\
\mycline{1-1}
C & D\\
\hline
\end{NiceTabular}

\bigskip
\begin{NiceTabular}{|c||c|}
\hline
A & B\\
\mycline*{1-1}
C & D\\
\hline
\end{NiceTabular}

\end{document}

Output of the above code

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

You must log in to answer this question.

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