# Fading line with same color at both the ends

I wanted a fading line with the same color at both the ends and a second color in the middle. Here is the crude MWE I came up with:

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[baseline]
\path[left color=white,right color=black]
(0,0) rectangle (.5\textwidth,.5pt);%
\path[left color=black,right color=white]
(.5\textwidth,0) rectangle (\textwidth,.5pt);
\end{tikzpicture}
\end{document}


This leaves a tiny tiny gap in the center. (Or is it just an optical illusion I get because I know I have used two path commands?)

Is there a better way of doing it? May be, with only one path/ fill/ rectangle command?

• Did you read Path following color gradient in TikZ? – Paul Gaborit Mar 20 '15 at 6:14
• Did you try to fade your line using a single rectangle with a middle color key? – Paul Gaborit Mar 20 '15 at 6:16
• @PaulGaborit I had seen that conversation earlier. Too complex for a newbie like me :) – deshmukh Mar 20 '15 at 7:13
• @PaulGaborit middle color was excellent. Looked up the exact syntax in texdoc tikz and done. Will you please give that comment as an answer? I will then accept it. – deshmukh Mar 20 '15 at 7:14

You may use the middle color key (after right color and left color keys):
\documentclass{article}