1

I am trying to create a table using the tabularx environment in Latex that includes various \cmidrule of different colors. However the lines created are not on the same vertical line (as seen in the screenshot below, the code is provided later in the question) :

Table screenshot showing that the vertical alignment of \cmidrule of different colors is not respected

I have found this answer (https://tex.stackexchange.com/a/35821/301750) that gives a satisfactory result for tables in a tabularenvironment. However, I have to use \tabularx in order to use a special column type I modified as well as column widths (see code).

% !TEX program = lualatex 

\documentclass{article}
\usepackage[table]{xcolor} % For row coloring
\usepackage{tabularx}      % For the tabularx environment
\usepackage{colortbl}      % For coloring the rules
\usepackage{array}         % For adjusting column widths
\usepackage{booktabs}      % For better horizontal rules

% Vertical centering for 'X' type columns
\renewcommand\tabularxcolumn[1]{m{#1}}

% from https://tex.stackexchange.com/a/35821/301750
% Correct for \cmidrule colour adjustment/vertical skip
\newcommand{\corcmidrule}[1][2pt]{% \corcmidrule[<len>]
    \\[\dimexpr-\normalbaselineskip-\belowrulesep-\aboverulesep-#1\relax]%
}

\begin{document}

\begin{tabular}{lll}
    column 1 & column 2 & column 3 \\
    \cmidrule[2pt](lr){1-1}\corcmidrule\arrayrulecolor{blue}%
    \cmidrule[2pt](lr){2-2}\corcmidrule\arrayrulecolor{black}%
    \cmidrule[2pt](lr){3-3}
    text & text & text\\ \cmidrule[0.4pt](lr){1-1}\cmidrule[0.4pt](lr){2-2}\cmidrule[0.4pt](lr){3-3}
    text & text & text\\ \cmidrule[0.4pt](lr){1-1}\cmidrule[0.4pt](lr){2-2}\cmidrule[0.4pt](lr){3-3}
\end{tabular}

\begin{table}[htbp]
    \centering
    \rowcolors{2}{white}{gray!5} % Alternate row colors
    \footnotesize
    \caption{Issue with vertical placement of cmidrule}
    \begin{tabularx}{\linewidth}{
        >{\hsize=1.1\hsize\centering}X
        >{\hsize=0.8\hsize\centering}X
        >{\hsize=1.2\hsize\centering}X
        >{\hsize=1\hsize\centering}X
        >{\hsize=1.3\hsize\centering}X
        >{\hsize=0.7\hsize\centering}X
        >{\hsize=1.4\hsize\centering}X
        >{\hsize=1\hsize\centering}X
        >{\hsize=0.5\hsize\centering}X
        >{\hsize=0.8\hsize\centering}X
        >{\hsize=1.2\hsize\centering\arraybackslash}X
    }
    \arrayrulecolor{gray}\toprule
    col 1 & col 2 & col 3 & col 4 & col 5 & col 6 & col 7 & col 8 & col 9 & col 10 & col 11 \\
    \arrayrulecolor{orange}\cmidrule(lr){1-1}\cmidrule(lr){3-3}\cmidrule(lr){4-4}\cmidrule(lr){7-7}
        \arrayrulecolor{gray}\cmidrule(lr){2-2}\cmidrule(lr){5-5}\cmidrule(lr){6-6}\cmidrule(lr){8-8}
    content 1 & content 2 & content 3 & content 4 & content 5 & content 6 & content 7 & content 8 & content 9 & content 10 & content 11 \\
    \arrayrulecolor{gray}\bottomrule
    \end{tabularx}
\end{table}

\end{document}

I would assume that the issue lies in the use of '\\' in the \corcmidrule definition but I could not be sure at all...

I know the code does not show the tabularx table using the \corcmidrule but it just does not work. The \corcmidrule is used in the tabular environment to show the desired result. I tried changing the code using a \vspace instead of \\ but it does not fix it. I must agree, I do not really understand what the code does so I am asking for help.

Thanks in advance.

In case you cannot run the code: here are the two resulting tables :

enter image description here

1 Answer 1

1
  • Your table, even if you in table use \footnotesize font size is bigger than defaults article size of the \textwidth. A remedy can be enlarge text block size by use geometry package.
  • Writing your second table by use of the tabularray package is simple to get desired result.
  • Beside this code is shorter:
\documentclass{article}
%\usepackage{geometry}
\usepackage{xcolor} % For row coloring
\usepackage{tabularray}      % For the tabularx environment
\UseTblrLibrary{booktabs}      % For better horizontal rules

\begin{document}

\begin{table}[htbp]
\footnotesize
    \caption{Issue with vertical placement of cmidrule}
    \begin{tblr}{colspec = {*{11}{X[c,m]}},
                 colsep  = 3pt,
                 row{even} = {gray!5}
                 }
    \toprule[fg=gray]
    col 1   & col 2     & col 3     & col 4     & col 5 & 
    col 6   & col 7     & col 8     & col 9     & col 10 & 
    col 11 \\
    \cmidrule[lr, fg=orange]{1-1}
    \cmidrule[lr, fg=gray]{3-3}
    \cmidrule[lr, fg=orange]{4-4}
    \cmidrule[lr, fg=orange]{7-7}
    \cmidrule[lr, fg=gray]{2-2}
    \cmidrule[lr, fg=gray]{5-5}
    \cmidrule[lr, fg=orange]{6-6}
    \cmidrule[lr, fg=gray]{8-8}
content 1   & content 2 & content 3 & content 4 & content 5     & 
content 6   & content 7 & content 8 & content 9 & content 10    & 
content 11 \\
    \bottomrule[fg=gray]
\end{tblr}
    \end{table}
\end{document}

enter image description here

2
  • Indeed, in my document I used another \documentclass that has a wider layout, sorry for the mistake... The packages you used seems very interesting but now I am tempted to change all of my table with it, oh well;) Anyways thanks a lot I am going to have to play around with it. Did you learn about of this package using a particular resource? Sep 17 at 18:01
  • @AlexJJoohn, transition from tabularx to tabularray is not so hard. Of course you need some time to learn some specific of it syntax, but package documentation is concise and thoroughly. Another possibilities is not use colored rules. To master it, you need to thoroughly study colortblr package documentation. BTW, if you load [table]{xcolor}˙you not need to load colortbl` package. It is loaded by table option.
    – Zarko
    Sep 17 at 19:12

You must log in to answer this question.

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