3

I wanted to write the code for this specific table in Latex (the image just above). I want to define the width of the table borders, the color of the table borders and also the white space between the two cells. I also wanted the text to be horizontally and vertically centered.

enter image description here

I tried something like this :

\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{tabularx, graphicx}


\definecolor{green}{RGB}{0,255,0}
\definecolor{blue}{RGB}{0,0,255}

\begin{document}

\setlength{\arrayrulewidth}{1mm}
\renewcommand{\arraystretch}{2}

\noindent
\begin{table}[h!]
    \centering
    \begin{tabular}{cc}
        \arrayrulecolor{green}
        \begin{tabular}{|>{\centering\arraybackslash}m{5cm}|}
        \hline
         \color{green} GREEN \\
         \hline
        \end{tabular}
    & 
        \arrayrulecolor{blue}
        \begin{tabular}{|>{\centering\arraybackslash}m{5cm}|}
        \hline
             \color{blue} BLUE \\
        \hline
            \end{tabular}
    \end{tabular}
\end{table}



\end{document}

I edited my code, it works, but I have a warning that I can't suppress : "You have used a \hline command in the wrong place, probably outside a table. If the \hline command is written inside a table, try including \ before it."

It seems that I can't change the border's color inside a table.

1
  • 3
    Is this always going to be the one-row-two-columns table? If yes, you might consider more convenient approaches: a mix of \parbox or minipage and \fcolorbox (of the xcolor package); tikz; or tcolorbox etc.
    – Celdor
    Mar 15 at 10:58

4 Answers 4

2

You wouldn't necessarily need a table for this. You could place two boxes horizontally next to each other with a white space of your size:

\documentclass{article}
\usepackage[svgnames]{xcolor}

\begin{document}
{%
  \centering
  \setlength\fboxrule{1mm}   % Line width 
  \setlength\fboxsep{6pt}    % Padding       
  \fcolorbox{Green}{Green!5}{%  or white instead of the tint Green!5
    \parbox{4cm}{\centering\textcolor{Green}{GREEN}}}%
  \hspace{2em}%A
  \fcolorbox{Blue}{Blue!5}{%
    \parbox{4cm}{\centering\textcolor{Blue}{BLUE}}}
\par}
\end{document}

enter image description here


Alternatively, you could use tikz and have a few more options, for example rounded corners. However, you would need to learn tikz, which is a huge package with extra modules and might be a little scary at first.

\documentclass{article}
\usepackage[svgnames]{xcolor}
\usepackage{tikz}
\usetikzlibrary{positioning}

\newcommand\boxtext{Sample sample sample sample sample sample sample}

\tikzset{
  box/.style = {
    draw,
    line width = 1mm,
    color=#1, fill=#1!5,
    align=center, text width=4cm, font=\normalfont,
    inner sep=6pt+1mm, outer sep=0pt,
    rounded corners,
  },
}


\begin{document}
{%
  \centering
  \tikz[node distance = 2em] {
    \node (G) [box=Green] {\boxtext};
    \node (B) [box=Blue, right=of G] {\boxtext};}
  \par}
\end{document}

enter image description here

6

As a workaround, you can use {NiceTabular} of nicematrix for the small tabular in the second cell of your main tabular. The environment {NiceTabular} has its own key for the colors of the rules.

\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{tabularx, graphicx}
\usepackage{nicematrix}


\definecolor{green}{RGB}{0,255,0}
\definecolor{blue}{RGB}{0,0,255}

\begin{document}

\setlength{\arrayrulewidth}{1mm}
\renewcommand{\arraystretch}{2}

\noindent
\begin{table}[h!]
    \centering
    \begin{tabular}{cc}
        \arrayrulecolor{green}
        \begin{tabular}{|>{\centering\arraybackslash}m{5cm}|}
        \hline
         \color{green} GREEN \\
         \hline
        \end{tabular}
    & 
        \begin{NiceTabular}{|>{\centering\arraybackslash}m{5cm}|}[rules/color=blue]
        \hline
             \color{blue} BLUE \\
        \hline
        \end{NiceTabular}
    \end{tabular}
\end{table}

\end{document}

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

Output of the above code

3
  • Thank you for your feedback, however it seems that sometimes the line height is too high. Moreover, it is annoying to have to compile several times.
    – Mo0nKizz
    Mar 16 at 7:24
  • nicematrix draws its lines by using Tikz (in fact PGF which is a sublayer of Tikz). In the PDF such lines have not the same structure as lines drawn by (raw) TeX. That's why the PDF viewers don't render those rules exactly in the same way (it's linked to the antialiasing techniques). If you use only rules of nicematrix, the output will be perfect and homogeneous. For rules drawn by nicematrix with PGF, you should use \Hline instead of \hline (but, in your case, I would use the key hvlines of {NiceTabular}). Mar 16 at 9:52
  • Concerning the multiple compilations, there are tools (such as latexmk) which do the successive compilations for you (for instance, Overleaf uses latexmk, afaik). Mar 16 at 9:56
2

Here is my solution using hhline and colortbl.

\documentclass{article}
\usepackage{hhline,colortbl}

%New column type so that text is centered
\newcolumntype{M}[1]{>{\centering\arraybackslash}m{#1}}

%Custom colors if needed
\definecolor{green}{RGB}{0,255,0}
\definecolor{blue}{RGB}{0,0,255}


\begin{document}

\setlength\arrayrulewidth{2pt} % thickness of table lines
\renewcommand{\arraystretch}{2}  % horizontal spacing

%create a new hline called myhline with the colors needed
\newcommand\myhline[1]{%
\hhline{%
>{\arrayrulecolor {green}}%
#1|-% solid green colored line
>{}%
#1|~%  no line
>{\arrayrulecolor{blue}}%
|-#1%  solid blue colored line
}}


% start array
\begin{tabular}{
 !{\color{green}\vrule width 2pt } % vertical line color, line width
 M{5cm}  % custom column type, column width
 !{\color{green}\vrule width 2pt} 
 M{1cm}
 !{\color{blue}\vrule width 2pt}
 M{5cm}
 !{\color{blue}\vrule width 2pt}
}


\myhline{}% Draw custom line
\color{green} GREEN& &\color{blue} BLUE\\
\myhline{}% Draw custom line
\end{tabular}


\end{document}


Here is the result. enter image description here

References:

Colortbl manual/guide

Hhline manual/guide

How to center column values in a table?

2
  • Thank you for your feedback. However with your solution you can't specify custom color.
    – Mo0nKizz
    Mar 16 at 9:13
  • \definecolor{green}{RGB}{0,255,0}<br/> \definecolor{blue}{RGB}{0,0,255}<br/> One can simply add these lines to the preample (your original example had them, so I thought you might be able to tweak this code to your liking). However, I will edit my answer to add them to my code.
    – sasaak
    Mar 16 at 9:21
2

A tabularray solution is missing:

\documentclass{article}
\usepackage{xcolor}
\usepackage{tabularray}

\begin{document}
\begin{table}[h!]
    \centering
    \begin{tblr}{colspec={Q[c,m,5cm, fg=green]cQ[c,m,5cm, fg=blue]},
        hline{1-2}={1}{green, wd=1mm},
        vline{1-2}={green, wd=1mm},
        hline{1-2}={3}{blue, wd=1mm},
        vline{3-4}={blue, wd=1mm},
        rows={ht=1cm},stretch=0
        }
        GREEN && BLUE \\
    \end{tblr}
\end{table}
\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 .