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 tabular
environment.
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 :