Apply a color gradient to lines of a tabularx

gradienttabularx

I am trying to apply a color gradient to the horizontal lines of a tabularx environment. I have a two-column tabularx in which I want the second column to be lined with left-to-right color gradient horizontal lines (pictured below).

Is there a simple way of doing this?

Please see the MWE below.

% !TEX TS-program = xelatex
\documentclass{article}

\usepackage{fontspec}
    \newfontfamily\greekfont[Script=Greek]{Arial}
    \newfontfamily{\greekfonttt}{Arial}
\usepackage{polyglossia}
    \setdefaultlanguage{greek}

\usepackage{tabularx}
\usepackage{booktabs}
\usepackage{colortbl}
\usepackage{color}

\begin{document}    
    {\setlength\arrayrulewidth{1pt}
        \renewcommand{\arraystretch}{1.5}
            \begin{tabularx}{\textwidth}{rX}
                ΙΔΡΥΜΑ & First line \\ \arrayrulecolor{red} \cline{2-2}
                ΣΧΟΛΗ & Second line \\ \arrayrulecolor{red} \cline{2-2}
            \end{tabularx}
    }
\end{document}

Best Answer

With {NiceTabular} of nicematrix. That package provides environment similar to the classical environments {tabular} and {tabularx} but which creates PGF/Tikz nodes under the cells, rows and columsn. It's possible to use those nodes to draw with Tikz whatever rule you want after the construction of the array (in the \CodeAfter).

\documentclass{article}

\usepackage{fontspec}
    \newfontfamily\greekfont[Script=Greek]{Arial}
    \newfontfamily{\greekfonttt}{Arial}
\usepackage{polyglossia}
    \setdefaultlanguage{greek}

\usepackage{color}
\usepackage{nicematrix,tikz}

\begin{document}    
{
\setlength\arrayrulewidth{1pt}
\renewcommand{\arraystretch}{1.5}
\begin{NiceTabularX}{\textwidth}{rX}
    ΙΔΡΥΜΑ & First line \\ 
    ΣΧΟΛΗ & Second line \\ 
\CodeAfter
    \begin{tikzpicture}
    \path [left color = red]
           ([yshift=0.5\arrayrulewidth]2-|2) rectangle 
           ([yshift=-0.5\arrayrulewidth]2-|last) ; 
    \path [left color = red]
           ([yshift=0.5\arrayrulewidth]3-|2) rectangle 
           ([yshift=-0.5\arrayrulewidth]3-|last) ; 
    \end{tikzpicture}
\end{NiceTabularX}
}
\end{document}

You need several compilations (because nicematrix uses PGF/Tikz nodes under the hood).

Output of the first code


In fact, with nicematrix, it's also possible to write a command \MyHrule to use at the beginning of a row in the {NiceTabular}. That command will write in the \CodeAfter the correct instructions to draw the rule as wanted.

\documentclass{article}

\usepackage{fontspec}
    \newfontfamily\greekfont[Script=Greek]{Arial}
    \newfontfamily{\greekfonttt}{Arial}
\usepackage{polyglossia}
    \setdefaultlanguage{greek}

\usepackage{color}
\usepackage{nicematrix,tikz}


\ExplSyntaxOn

\cs_new_protected:Nn \__AlexSt_MyRule:n
  {
    \tikz \path [left~color = red]
               ([yshift=0.5\arrayrulewidth]#1-|2) rectangle 
               ([yshift=-0.5\arrayrulewidth]#1-|last) ; 
  }

\NewExpandableDocumentCommand \MyRule { }
 {
   \noalign 
     { 
       \skip_vertical:N \arrayrulewidth 
       \tl_gput_right:Nx \g_nicematrix_code_after_tl 
         { \__AlexSt_MyRule:n { \int_eval:n { \int_use:c { c@iRow } + 1 } } }
     }
 }

\ExplSyntaxOff

\begin{document}    
{
\setlength\arrayrulewidth{1pt}
\renewcommand{\arraystretch}{1.5}
\begin{NiceTabularX}{\textwidth}{rX}
  ΙΔΡΥΜΑ & First line \\ 
  \MyRule
  ΣΧΟΛΗ & Second line \\ 
  \MyRule
\end{NiceTabularX}
}
\end{document}

Moreover, with that programmation, vertical space is reserved for the rules (as does the classical \hline of array).

Output of the second code