3

I want to add a numeration column at a table with a column-color, but without rules - that means, it seems I cannot use \hline.
If I use \cline{2-4} the rules gone lost at the colored column.

What do I have to do?

Hint: There are certainly solutions with TikZ, nicematrix, etc. But I would like to implement this with table tools that are as elementary as possible.

enter image description here

\documentclass[]{article}
\usepackage{colortbl}
\usepackage{xcolor}
\begin{document}

\definecolor{mygreen}{HTML}{B9D5A5}

\setlength\arrayrulewidth{0.8125pt} % seems to be needed...
\setlength\tabcolsep{1pt}% seems to be needed...

\newcolumntype{P}{>{\centering\arraybackslash}p{3mm}}
\newcolumntype{Q}{>{\columncolor{mygreen}}P}

\def\x{$\scriptstyle\times$}
\newcommand\No[1]{\footnotesize\texttt{#1}}
\newcommand\n[1]{\multicolumn{1}{r|}{\No{#1}}}

\begin{tabular}{r | P | P | Q |}
\multicolumn{1}{c}{}  
     & \multicolumn{1}{c}{\No{1.}}  
         & \multicolumn{1}{c}{\No{2.}}  
              &  \multicolumn{1}{c}{\No{3.}}  \\ \hline
\n{1} &  & & \x \\  \hline % Problem here
\n{2}  &  & &\x  \\ \cline{2-4}
\n{3}  &  & &\x \\ \cline{2-4}
\n{4}  &  & &\x \\ \cline{2-4}
\n{5}  &  & &\x \\ \cline{2-4}
\n{6}  &  & &\x \\ \cline{2-4}
\n{7}  &  & &\x \\ \cline{2-4}
\end{tabular}
\end{document}

3 Answers 3

4

You can use \hhline (see https://tex.stackexchange.com/a/252071/4427).

\documentclass[]{article}
\usepackage{colortbl,hhline}
\usepackage{xcolor}

\definecolor{mygreen}{HTML}{B9D5A5}

\begin{document}

\begin{center} % will also keep settings local
\setlength\arrayrulewidth{0.8125pt} % seems to be needed...
\setlength\tabcolsep{1pt}% seems to be needed...

\newcolumntype{P}{w{c}{3mm}}% <--- THIS IS GOOD, p{3mm} isn't
\newcolumntype{Q}{>{\columncolor{mygreen}}P}

\newcommand{\x}{$\scriptstyle\times$}
\newcommand\No[1]{\footnotesize\texttt{#1}}

\begin{tabular}{r | P | P | Q |}
\multicolumn{1}{c}{}  
  & \multicolumn{1}{c}{\No{1.}}  
  & \multicolumn{1}{c}{\No{2.}}  
  & \multicolumn{1}{c}{\No{3.}}  \\ \hhline{~|---}
\No{1} & & & \x \\ \hhline{~|---}
\No{2} & & & \x \\ \hhline{~|---}
\No{3} & & & \x \\ \hhline{~|---}
\No{4} & & & \x \\ \hhline{~|---}
\No{5} & & & \x \\ \hhline{~|---}
\No{6} & & & \x \\ \hhline{~|---}
\No{7} & & & \x \\ \hhline{~|---}
\end{tabular}

\end{center}

\end{document}

enter image description here

  1. hhline is a standard LaTeX package among the required ones in any distribution
  2. DON'T use \def
  3. w{c}{3mm} is much better than >{\centering\arraybackslash}p{3mm}
  4. I see no reason for \n.

Here's the output if you replace the definition of \x with

\newcommand{\x}{$\vcenter{\hbox{$\scriptstyle\times$}}$}

enter image description here

6

With {NiceTabular} of nicematrix you can use the keys first-row and first-col and, then, the key hvlines to require all the rules to be drawn, except in the so-called "first row" and "first column".

\documentclass[]{article}
\usepackage{nicematrix}

\begin{document}

\definecolor{mygreen}{HTML}{B9D5A5}
\newcommand{\x}{$\scriptstyle \times$}
\setlength{\tabcolsep}{2pt}
\NiceMatrixOptions{code-for-first-col = \scriptsize}

\begin{NiceTabular}{cc>{\columncolor{mygreen}}c}[first-col,first-row,hvlines,color-inside]
   & 1. & 2. &  3. \\
1  &    &    &  \x \\ 
2  &    &    &  \x \\ 
3  &    &    &  \x \\ 
4  &    &    &  \x \\ 
5  &    &    &  \x \\ 
6  &    &    &  \x \\ 
7  &    &    &  \x \\ 
\end{NiceTabular}

\end{document}

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

Output of the above code


In fact, it's also possible to put an automatic numbering of the rows by putting \arabic{iRow} in the value of code-for-first-col.

\documentclass[]{article}
\usepackage{nicematrix}

\begin{document}

\definecolor{mygreen}{HTML}{B9D5A5}
\newcommand{\x}{$\scriptstyle \times$}
\setlength{\tabcolsep}{2pt}
\NiceMatrixOptions{code-for-first-col = \scriptsize \arabic{iRow}}

\newcommand{\RowNumber}{\arabic{}}

\begin{NiceTabular}{cc>{\columncolor{mygreen}}c}[first-col,first-row,hvlines,color-inside]
& 1. & 2. &  3. \\
&    &    &  \x \\ 
&    &    &  \x \\ 
&    &    &  \x \\ 
&    &    &  \x \\ 
&    &    &  \x \\ 
&    &    &  \x \\ 
&    &    &  \x \\ 
\end{NiceTabular}

\end{document}

The output is the same.

1

With tabularray you can automate the cell content (see preto option):

\documentclass{article}
\usepackage{xcolor}
\definecolor{mygreen}{HTML}{B9D5A5}
\newcommand{\x}{$\scriptstyle\times$}
\usepackage{tabularray}
\UseTblrLibrary{counter}
\newcounter{mycountr}
\newcommand{\myr}{\stepcounter{mycountr}\arabic{mycountr}}
\newcounter{mycountc}
\newcommand{\myc}{\stepcounter{mycountc}\arabic{mycountc}}

\begin{document}
\begin{tblr}{  
  columns={.5cm, colsep=0pt},
  rows={.5cm, rowsep=0pt},
  cells={c,m},stretch=0,
  hline{2-Z}={2-Z}{wd=0.8125pt},
  vline{2-Z}={2-Z}{wd=0.8125pt},
  cell{1}{2-4}={preto={\myr .}, font=\footnotesize\ttfamily},
  cell{2-Z}{1}={preto={\myc}, font=\footnotesize\ttfamily},
  cell{2-Z}{4}={preto={\x}, bg=mygreen},
  }
  &    &    &  \\   
  &    &    &  \\ 
  &    &    &  \\       
  &    &    &  \\ 
  &    &    &  \\ 
  &    &    &  \\ 
  &    &    &  \\ 
  &    &    &  \\ 
\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 .