TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'd like to have a colored line that starts with transparent 100 and ends with trasparent, let's say, 30.

I tried this code:

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{calc,fadings,decorations.markings}


\makeatletter

\pgfkeys{/pgf/decoration/.cd,
         start shade/.store in = \startshade,
         end shade/.store in   = \endshade,
}

\pgfdeclaredecoration{shade change}{initial}{
\state{initial}[
width                     = \pgflinewidth,
next state                = line,
persistent precomputation = {\pgfmathdivide{(\startshade-\endshade)/100}{\pgfdecoratedpathlength}%
                             \let\@increment=\pgfmathresult
                             \pgfmathsetmacro\@pitch{\pgfdecoratedpathlength/100}
                             \def\@@n{0}
                             \def\@sh{\startshade}}
]{}

\state{line}[
width                      =  \pgflinewidth,
persistent postcomputation = {\pgfmathsetmacro\@@n{\@@n+1}
                              \pgfmathsetmacro\@sh{\startshade/100-\@increment*\@@n}}]
{
\pgfsetstrokeopacity{\@sh}
\pgfpathmoveto{\pgfpointorigin}
\pgfpathlineto{\pgfqpoint{\@pitch pt}{0pt}}
\pgfusepath{stroke}}

\state{final}{
\pgfpathmoveto{\pgfpointorigin}%
\pgfsetstrokeopacity{\@sh}
 \pgfusepath{stroke}%
}
}

\makeatother


\begin{document}
\centering
\begin{tikzpicture}
\draw[
line width = 2pt,
decoration = {shade change,
              start shade = 100,
              end shade   = 30},
decorate] (0,0)--++(3,3)node[midway,above]{$I$};

\end{tikzpicture}
\end{document}

but the result is the following: enter image description here

Where do I do wrong?

share|improve this question
up vote 7 down vote accepted

Something like that :

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{calc,fadings,decorations.markings}


\makeatletter

\pgfkeys{/pgf/decoration/.cd,
         start shade/.store in = \startshade,
         end shade/.store in   = \endshade,
}

\pgfdeclaredecoration{shade change}{initial}{
\state{initial}[
width                     = \pgflinewidth,
next state                = line,
persistent precomputation = {\pgfmathdivide{(\startshade-\endshade)/100}%
 {\pgfdecoratedpathlength}%
                             \let\@increment=\pgfmathresult
 \pgfmathsetmacro\@pitch{\pgfdecoratedpathlength/100}
                             \def\@@n{0}
                             \def\x{\@@n}
 \def\@sh{\startshade}}
]{}

\state{line}[
width                      =  \pgflinewidth,
persistent postcomputation = {\pgfmathsetmacro\@@n{\@@n+1}
                     \pgfmathsetmacro\@sh{\startshade/100-\@increment*\@@n}}]
{%
\pgfsetstrokecolor{black!\@@n!white}%  
\pgfpathmoveto{\pgfpointorigin}
\pgfpathlineto{\pgfqpoint{\@pitch pt}{0pt}}
\pgfusepath{stroke}}

\state{final}{
\pgfpathmoveto{\pgfpointorigin}%
\pgfsetstrokecolor{black!\@@n!white}%  
 \pgfusepath{stroke}%
}
}

\makeatother


\begin{document}
\centering
\begin{tikzpicture}
\draw[
line width = 2pt,
decoration = {shade change,
              start shade = 100,
              end shade   = 30},
decorate] (0,0)--++(3,3)node[midway,above]{$I$};

\end{tikzpicture}
\end{document}

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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