6

So I have a gradient in my cell using TikZ, which is nice.

However I still have a few problems:

  1. The text itself has its color changed, whereas I would want only the background color to be changed. The horizontal line beneath the text also faces the same problem (it's a \hline, since I'm inside a tabular).

  2. The vertical lines aren't aligned with the rest of my tabular, if I just add |c| to the initial c in the multicolumn function.

Link to the related question: Gradient color in one cell of a table.

jlo17
  • 487
  • You should include a link to the previous question, and a minimal complete example showing the problem (starting from \documentclass and including only the code necessary for demonstrating the problem). – Jake Jun 10 '12 at 22:01
  • I added the link. The code is already in the other question, I didn't modify it. – jlo17 Jun 10 '12 at 22:03

2 Answers2

5
  1. With the code below (taken from the answer linked to in the question) I don't seem to be able to reproduce the first problem mentioned, so a minimal working example will be needed.
  2. The code below also shows how to use \vrule inside !{...} to add vertical rules.

    \documentclass{article}
    \usepackage[table]{xcolor} 
    \usepackage{array}
    \usepackage{tikz}
    \usetikzlibrary{calc}
    
    % Andrew Stacey's code from
    % http://tex.stackexchange.com/a/50054/3954
    \makeatletter
    \tikzset{%
      remember picture with id/.style={%
        remember picture,
        overlay,
        save picture id=#1,
      },
      save picture id/.code={%
        \edef\pgf@temp{#1}%
        \immediate\write\pgfutil@auxout{%
          \noexpand\savepointas{\pgf@temp}{\pgfpictureid}}%
      },
      if picture id/.code args={#1#2#3}{%
        \@ifundefined{save@pt@#1}{%
          \pgfkeysalso{#3}%
        }{
          \pgfkeysalso{#2}%
        }
      }
    }
    
    \def\savepointas#1#2{%
      \expandafter\gdef\csname save@pt@#1\endcsname{#2}%
    }
    
    \def\tmk@labeldef#1,#2\@nil{%
      \def\tmk@label{#1}%
      \def\tmk@def{#2}%
    }
    
    \tikzdeclarecoordinatesystem{pic}{%
      \pgfutil@in@,{#1}%
      \ifpgfutil@in@%
        \tmk@labeldef#1\@nil
      \else
        \tmk@labeldef#1,(0pt,0pt)\@nil
      \fi
      \@ifundefined{save@pt@\tmk@label}{%
        \tikz@scan@one@point\pgfutil@firstofone\tmk@def
      }{%
      \pgfsys@getposition{\csname save@pt@\tmk@label\endcsname}\save@orig@pic%
      \pgfsys@getposition{\pgfpictureid}\save@this@pic%
      \pgf@process{\pgfpointorigin\save@this@pic}%
      \pgf@xa=\pgf@x
      \pgf@ya=\pgf@y
      \pgf@process{\pgfpointorigin\save@orig@pic}%
      \advance\pgf@x by -\pgf@xa
      \advance\pgf@y by -\pgf@ya
      }%
    }
    \newcommand\tikzmark[2][]{%
    \tikz[remember picture with id=#2] #1;}
    \makeatother
    % end of Andrew's code
    
    \newcommand\ShadeCell[3]{%
      \begin{tikzpicture}[overlay,remember picture]%
        \shade[#3] ( $ (pic cs:#1) + (0pt,8.5pt) $ ) rectangle ( $ (pic cs:#2) + (-0.2pt,-3.5pt) $ );
      \end{tikzpicture}%
    }%
    
    \begin{document}
    
    \ShadeCell{start1}{end1}{%
      top color=blue,bottom color=magenta!70,opacity=0.5}
    \ShadeCell{start2}{end2}{%
      left color=red,right color=green,opacity=0.5}
    
    \begin{tabular}{| l | c |}
    \hline
    Uncolored cell & \multicolumn{1}{!{\tikzmark{start1}} c !{\vrule\tikzmark{end1}}}{Colored cell} \\
    \hline
    \multicolumn{1}{!{\vrule\tikzmark{start2}} l !{\vrule\tikzmark{end2}}}{Another colored cell} & Another uncolored cell \\
    \hline
    \end{tabular}
    
    \end{document}
    

enter image description here

Moriambar
  • 11,466
Gonzalo Medina
  • 505,128
  • Regarding the first question, well I actually used the first code you gave, rather than the second much more complicated one. I thought the second one was just to have a different behavior on the first run. – jlo17 Jun 10 '12 at 22:09
  • @jlo17 no; I included the second code to show how to place the shade behind the text (I mentioned this in my answer to the other question). – Gonzalo Medina Jun 10 '12 at 22:12
  • @jlo17 if you want to use babel with french (or frenchb) option, you need to use \newcommand\tikzmark[2][]{ \tikz[remember picture with id=#2] {#1;}} (see the extra pair of braces) in my code. – Gonzalo Medina Jun 10 '12 at 22:14
  • This is getting very complicated. I am unable to reproduce your code snippet in my own code without adding dozens of new problems. It just went back to changing the text color too with no reason. If just adding a completely unrelated command such as a usepackage[french]{babel} or a vertical line just breaks the code, it makes it unstable and maintainable, and thus usable. Thanks for answering my questions though, you're very good. But I need an easier solution on how to add a gradient to a cell's background. I'll mark this one as solved but the other one as unsolved until I find a easier one. – jlo17 Jun 10 '12 at 23:20
  • Sorry for giving up. – jlo17 Jun 10 '12 at 23:31
  • @jlo17 No problem! I've improved the code in the original question and I also added an explanation on how to use it; it's not complicated after you get the hang of it. As you can see in the new example (in the original question), vertical rules don't break the code and using the french option for babel is also not a problem. If you are still experiencing problems with the new code and its usage, perhaps we could discuss them. – Gonzalo Medina Jun 11 '12 at 02:56
2

With {NiceTabular} of nicematrix, it's very easy to add a gradient of color in the cell of a tabular.

\documentclass{article}
\usepackage{nicematrix}
\usepackage{tikz}

\begin{document}

\begin{NiceTabular}{lc}[hvlines] Uncolored cell & \Block[tikz={top color=blue,bottom color=magenta!70,opacity=0.5}]{}{} Colored cell \ \Block[tikz={left color=red,right color=green,opacity=0.5}]{}{} Another colored cell & Another uncolored cell \end{NiceTabular}

\end{document}

You need several compilations.

Output of the above code

F. Pantigny
  • 40,250