0

I have a small problem in my table editing using booktabs and colors. The main problem is the background color of a cell is longer than the midrule, which is not a huge problem but it is quite annoying to look at.

The problem on a table

I use midrules, toprules and bottomrules. A row of the table is given as:

XXXXXX & xx\% & xx\% & xx\% & xx\% & \cellcolor[HTML]{CAFFCA}xx\% & \cellcolor[HTML]{CAFFCA}xx\% & \cellcolor[HTML]{CAFFCA}xx\% \\

I'm not sure how to extend the rules or how to shorten the color. Any advice is welcome.

Minimum working code:

\documentclass{article}

\usepackage{booktabs}
\usepackage[table,xcdraw]{xcolor}
\usepackage{multirow}
\usepackage{adjustbox}

\begin{document}
    \begin{table}
          \centering
          \footnotesize
            \begin{tabular}{@{}l|rrrr@{}}
                \toprule
                \multicolumn{1}{c|}{\textbf{xx}} & \multicolumn{1}{c|}{\textbf{xx}} & \multicolumn{1}{c|}{\textbf{xx}} & \multicolumn{1}{c}{\textbf{xx}} & \multicolumn{1}{c}{\textbf{xx}} \\ \midrule
                xx & xx & xx & xx & \cellcolor[HTML]{FE0000}xx \\
                xx & xx & xx & xx & \cellcolor[HTML]{34FF34}xx \\ \bottomrule
            \end{tabular}
    \end{table}
\end{document}
1

In your case maybe a TikZ matrix can help.

With <matrixname>-<rownumber>-<columnnumber> you can refer to a single cell.

\documentclass{article}

\usepackage{booktabs}
\usepackage[table,xcdraw]{xcolor}
\usepackage{multirow}
\usepackage{adjustbox}

\definecolor{myred}{HTML}{FE0000}
\definecolor{mygreen}{HTML}{34FF34}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}
\begin{table}
\centering
\begin{tikzpicture}
    \matrix[
        matrix of nodes,
        row sep=0pt,
        column 1/.style={nodes={text width=3em, align=left}},
        row 1/.style={nodes={text width=3em, align=center}},
        nodes={text width=3em, align=center, font=\footnotesize}
    ] (M) {                
    xx & xx & xx & \textbf{xx} & \textbf{xx} \\[4pt]   
    xx & xx & xx & xx & |[fill=myred]|xx \\
    xx & xx & xx & xx & |[fill=mygreen]|xx \\ 
    };
    % horizontal lines
    \draw[thick] (M.north west) -- (M.north east);
    \draw[thick] (M.south west) -- (M.south east);
    \draw (M.west |- M-1-1.south west) -- (M-1-1.south west) -- (M-1-5.south east) -- (M.east |- M-1-5.south east);
    % vertical line
    \draw (M.north -| M-1-1.north east) -- (M-1-1.north east) -- (M-3-1.south east) -- (M.south -| M-3-1.south east);
\end{tikzpicture}
\end{table}
\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.