1

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}
5

3 Answers 3

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

0

Don't use @{} for colored tables, modified MWE is given below:

\documentclass{article}

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

\begin{document}
    \begin{table}
          \centering
          \footnotesize
            \begin{tabular}{l|rrrr}
\Xhline{1pt}
                \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}} \\ 
\Xhline{0.5pt}
                xx & xx & xx & xx & \cellcolor[HTML]{FE0000}xx \\
                xx & xx & xx & xx & \cellcolor[HTML]{34FF34}xx \\ 
\Xhline{1pt}
            \end{tabular}
    \end{table}
\end{document}

Output:

enter image description here

0

It is not very clear what your table should looks. Considering r column type and @{} (which cause your problem) on the end of table, it seems that you looking for the following:

enter image description here

\documentclass{article}

\usepackage{booktabs, 
            makecell}   % <--- new
\renewcommand\theadfont{\normalsize\bfseries}

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

\begin{document}
    \begin{table}[ht]
\centering
\begin{tabular}{@{} l  rr r
    >{\columncolor{white}[\tabcolsep][0pt]}r
                @{}}
    \Xhline{\heavyrulewidth}
    \thead{xx}  & \thead{xx}    & \thead{xx}    & \thead{xx}  & \thead{xx}  \\ \Xhline{\lightrulewidth}
    xx & xx & xx & xx & \cellcolor[HTML]{FE0000}xx \\
    xx & xx & xx & xx & \cellcolor[HTML]{34FF34}xx \\
    \Xhline{\heavyrulewidth}
\end{tabular}
    \end{table}
\end{document}

You must log in to answer this question.

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