2

So, I have a very simple table I'm trying to get working. The idea is to have the first, second, and last column/row separators be thick and colored a shade of green, while the first column and first row are given a background of pale yellow. At the same time I still need normal column/row separators.

However, my initial attempts to achieve this result have been met with what I believe to be multiple small issues. To list them, the most major issue is that the first and second column separators extend below the bottom of the table. Second, the normal \hline is drawing over the vertical rules, when I would prefer the latter. Third, changing the cell color draws over parts of the separators, making them appear thinner in certain places. Fourth, the normal vertical rules don't display consistently when bordered by colored cells.

Here is my minimal working example. I used the xcolor package to help define the colors and colortbl to add colors to the \hrule in the newcommands and color to the \newcolumntypes.

\documentclass[10pt]{article}
\usepackage{xcolor}
\usepackage{colortbl}

\definecolor{forestgreen}{RGB}{  39, 159,  39}
\definecolor{paleyellow}{RGB}{ 255, 255, 212}

\newcommand{\ywl}{\cellcolor{paleyellow}}
\newcommand{\grline}{\noalign{\color{forestgreen}\hrule height 1.6pt}}
\newcolumntype{(}{@{\color{forestgreen}\vrule width 1.6pt\hskip\tabcolsep}}
\newcolumntype{"}{@{\hskip\tabcolsep\color{forestgreen}\vrule width 1.6pt\hskip\tabcolsep}}
\newcolumntype{)}{@{\hskip\tabcolsep\color{forestgreen}\vrule width 1.6pt}}

\begin{document}

\begin{tabular}{(c"c|c|c|c)}
\grline
\ywl &\ywl 0 &\ywl a &\ywl b &\ywl c \\
\grline
\ywl 0 & 0 & 0 & 0 & 0 \\
\hline
\ywl a & 0 & a & b & c \\
\hline
\ywl b & 0 & 0 & 0 & 0 \\
\hline
\ywl c & 0 & a & b & c \\
\grline
\end{tabular}

\end{document}
2
  • Welcome to TeX.SX! Can you please expand the code snippet that you have posted to a full minimal working example. It is much easier to help you if we can start with some compilable code that illustrates your problem. A MWE should start with a \documentclass command, include any necessary packages and be as small as possible to demonstrate your problem. At the moment we have to guess what packages etc you are using before we can compile your code.
    – user30471
    Mar 27, 2020 at 13:14
  • 1
    Hey Andrew. Thanks for the advice. My spacing actually put the \documentclass command on the same line as the beginning of the code block, which I believe prevented it from showing. I've gone through and fixed that, and tried to remove some unnecessary packages at the same time. Is this a better MWE?
    – Rithaniel
    Mar 27, 2020 at 13:50

3 Answers 3

3

Here is a version using a tikz matrix:

enter image description here

Since you only wrote about \hline and the colored vertical lines, please keep in mind that with the below code, all colored lines are drawn on top of the black lines. This applies to the vertical colored line that is drawn on top of the cossing black hlines but also applies to the horizontal colored line.

\documentclass{article}
\usepackage{xcolor}
\definecolor{forestgreen}{RGB}{  39, 159,  39}
\definecolor{paleyellow}{RGB}{ 255, 255, 212}

\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}

\begin{tikzpicture}[cell/.style={rectangle,draw=black}, nodes in empty cells]
  \matrix(table)[
  matrix of nodes,
  row sep =-\pgflinewidth,
  column sep = -\pgflinewidth,
  nodes={anchor=center,text height=2ex,text depth=0.25ex, minimum width=2em, draw=black},
  column 1/.style = {nodes={fill=paleyellow}},
  row 1/.style={nodes={fill=paleyellow}},
  ] 
  {
 & 0 & a & b & c \\
 0 & 0 & 0 & 0 & 0 \\
 a & 0 & a & b & c \\
 b & 0 & 0 & 0 & 0 \\
 c & 0 & a & b & c \\
  };
  \draw[line width=1.6pt, forestgreen](table-1-1.north west)--(table-1-5.north east) -- (table-5-5.south east) -- (table-5-1.south west) -- cycle;
  \draw[line width=1.6pt, forestgreen](table-1-1.north east)--(table-5-1.south east) ;
  \draw[line width=1.6pt, forestgreen](table-1-1.south west)--(table-1-5.south east) ;
\end{tikzpicture}

\end{document}

Update with \mymatcols and \mymatrows from here: With the before mentioned commands, the maximum number of columns and rows in that matrix it determined automatically. Upon changing the number of columns/rows in the matrix, the green colored lines are automatically adjusted as well.

\documentclass{article}
\usepackage{xcolor}
\definecolor{forestgreen}{RGB}{  39, 159,  39}
\definecolor{paleyellow}{RGB}{ 255, 255, 212}

\usepackage{tikz}
\usetikzlibrary{matrix}

\makeatletter
\tikzset{store number of columns in/.style={execute at end matrix={
\xdef#1{\the\pgf@matrix@numberofcolumns}}},
store number of rows in/.style={execute at end matrix={
\xdef#1{\the\pgfmatrixcurrentrow}}}}
\makeatother

\begin{document}

\begin{tikzpicture}[cell/.style={rectangle,draw=black}, nodes in empty cells]
  \matrix(table)[
  matrix of nodes,
  row sep =-\pgflinewidth,
  column sep = -\pgflinewidth,
  nodes={anchor=center,text height=2ex,text depth=0.25ex, minimum width=2em, draw=black},
  column 1/.style = {nodes={fill=paleyellow}},
  row 1/.style={nodes={fill=paleyellow}},
  store number of columns in=\mymatcols,
  store number of rows in=\mymatrows
  ] 
  {
 & 0 & a & b & c \\
 0 & 0 & 0 & 0 & 0 \\
 a & 0 & a & b & c \\
 b & 0 & 0 & 0 & 0 \\
 c & 0 & a & b & c \\
  };
  \draw[line width=1.6pt, forestgreen](table-1-1.north west)--(table-1-\mymatcols.north east) -- (table-\mymatcols-\mymatrows.south east) -- (table-\mymatrows-1.south west) -- cycle;
  \draw[line width=1.6pt, forestgreen](table-1-1.north east)--(table-\mymatrows-1.south east) ;
  \draw[line width=1.6pt, forestgreen](table-1-1.south west)--(table-1-\mymatcols.south east) ;
\end{tikzpicture}

\end{document}
8
  • I actually didn't know this was something you could do with tixz. I'm a little unfamiliar with the package, so what I see is slightly intimidating. How would this be adapted to adding columns, rows, or changing the sizes of columns and rows?
    – Rithaniel
    Mar 27, 2020 at 14:23
  • You can change the width of the columns using minimum width. Setting this to a larger value will increase the width of all rows. If you want to add more rows and colmns, you can simply place their contents in the set of {} as I did with & 0 & a & b & c \\ 0 & 0 & 0 & 0 & 0 \\ a & 0 & a & b & c \\ b & 0 & 0 & 0 & 0 \\ c & 0 & a & b & c \\ .
    – leandriis
    Mar 27, 2020 at 14:32
  • The last thing to adjust if you add more rows and columns are the green lines. They are added using the \draw commands. The first one draws the line at the outer edge of the table. It starts at table-1-1.north west which corresponds to the upper left corner of the cell in the first column and first row. table-1-5.north east corresponds to the upper left corner of the cell in the first column and fifth row. Depending on how many rows/columns you added, you will have to change these values.
    – leandriis
    Mar 27, 2020 at 14:36
  • With the help of column 1/.style = {nodes={fill=paleyellow}}, row 1/.style={nodes={fill=paleyellow}}, i specified that every cell in the first column and in the first row should be colored paleyellow. So if you add more rows and columns, the background color is applied automatically. I hope this helps. If something remains unclear, please don't hesitate to ask.
    – leandriis
    Mar 27, 2020 at 14:37
  • @Rithaniel: I have juste edited my answer to add a modification. This will allow you to change the number of columns and rows without having to worry about the green lines as they will now also adapt automatically.
    – leandriis
    Mar 27, 2020 at 14:54
1

For coloring the column separators

enter image description here

\documentclass{article}

\usepackage{hhline,colortbl}

\begin{document}
\arrayrulecolor{green}
\arrayrulewidth=2mm
\begin{tabular}{
 !{\color{red}\vrule width 2pt}
 l
 |
 c
 !{\color{blue}\vrule width 2pt}
 c
 ||
}
 one & two & three\\
\hline
  1 & 2 & 3\\%
\noalign{
\color{yellow}
\hrule height 5pt
}%
4&5&6\\
\hline
\end{tabular}
\end{document}

For coloring the cells/content

\documentclass{article}
\usepackage[svgnames,table]{xcolor}% note the table option
\usepackage{booktabs}
\renewcommand{\arraystretch}{2}
\makeatletter
\newcommand*{\minuscellcolor}{}
\def\minuscellcolor\ignorespaces{%
  % \ignorespaces not really needed, because \@ifnextchar gobbles spaces
  \@ifnextchar{T}{\cellcolor{green!40}}{}%
}
\newcolumntype{C}{>{\minuscellcolor}c}
\makeatother

\begin{document}
\begin{center}

 \begin{tabular}{*{8}C}                    \toprule\rowcolor{gray!30}
 p & q & p & $\vee$ & [$\neg$ & (p & $\wedge$ & q)]      \\ 
 T & T & T & T & F & T & T & T                           \\ 
 T & F & T & T & T & T & F & F                           \\
 F & T & F & T & T & F & F & T                           \\
 F & F & F & T & T & F & F & F                           \\ \bottomrule
\end{tabular}

\end{center}
\end{document}

enter image description here

1
  • 1
    OP wrote " Second, the normal \hline is drawing over the vertical rules, when I would prefer the opposite (normal \hline being behind the vertical rules).". Wile your first example definitely shows how to achieve differently colored lines, in this example, the horizontal lines are still drawn above of the vertical lines. Could you please comment on that?
    – leandriis
    Mar 27, 2020 at 13:26
1

Here is what you can do with {NiceTabular} of nicematrix.

The key hvlines of {NiceTabular} draws all the rules (in black).

Then, I use Tikz to draw the green rules by using the PGF/Tiks nodes created by nicematrix under the cells, rows and columns.

The code is independant of the number of rows and columns in the tabular.

\documentclass[10pt]{article}
\usepackage{xcolor}
\usepackage{nicematrix,tikz}

\definecolor{forestgreen}{RGB}{  39, 159,  39}
\definecolor{paleyellow}{RGB}{ 255, 255, 212}

\begin{document}

\begin{NiceTabular}{ccccc}[hvlines]
\CodeBefore
  \rowcolor{paleyellow}{1}
  \columncolor{paleyellow}{1}
\Body
   &  0 & a & b & c \\
  0 & 0 & 0 & 0 & 0 \\
  a & 0 & a & b & c \\
  b & 0 & 0 & 0 & 0 \\
  c & 0 & a & b & c \\
\CodeAfter
  \tikz [forestgreen, line width = 1pt]
    \draw (1-|1) rectangle (last-|last) 
          (1-|2) -- (last-|2) 
          (2-|1) -- (2-|last) ; 
\end{NiceTabular}

\end{document}

You need several compilations (because of the PGF/Tikz nodes)

Output of the above code

You must log in to answer this question.

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