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) :
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 \
\cmidrule2pt{1-1}\corcmidrule\arrayrulecolor{blue}%
\cmidrule2pt{2-2}\corcmidrule\arrayrulecolor{black}%
\cmidrule2pt{3-3}
text & text & text\ \cmidrule0.4pt{1-1}\cmidrule0.4pt{2-2}\cmidrule0.4pt{3-3}
text & text & text\ \cmidrule0.4pt{1-1}\cmidrule0.4pt{2-2}\cmidrule0.4pt{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 :



tabularxtotabularrayis 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 studycolortblrpackage documentation. BTW, if you load[table]{xcolor}˙you not need to loadcolortblpackage. It is loaded bytable` option. – Zarko Sep 17 '23 at 19:12