5

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
  • 1
    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
  • 1
    @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
4

You may use the middle color key (after right color and left color keys):

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.