[Tex/LaTex] Stroke with variable thickness

tikz-pgf

Can you create lines with variable thickness (line width) in Tikz?
enter image description here

Best Answer

This is possible but it's not easy and the control of the line width is not very fine but it's an idea and I think it's possible to get a better code. The code below is from an idea of Mark Wibrow to change the color of a line. I modified the code to change the width but if you only want to change the color this code is perfect :

\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{decorations}
\begin{document}

\makeatletter

\pgfkeys{/pgf/decoration/.cd,
         start color/.store in =\startcolor,
         end color/.store in   =\endcolor
}

\pgfdeclaredecoration{width and color change}{initial}{
 \state{initial}[width=0pt, next state=line, persistent precomputation={%
   \pgfmathdivide{50}{\pgfdecoratedpathlength}%
   \let\increment=\pgfmathresult%
   \def\x{0}%
 }]{}
 \state{line}[width=.5pt,   persistent postcomputation={%
     \pgfmathadd@{\x}{\increment}%
     \let\x=\pgfmathresult%
   }]{%
   \pgfsetlinewidth{\x/40*0.075pt+\pgflinewidth}%
   \pgfsetarrows{-}%
   \pgfpathmoveto{\pgfpointorigin}%
   \pgfpathlineto{\pgfqpoint{.75pt}{0pt}}%
   \pgfsetstrokecolor{\endcolor!\x!\startcolor}%
   \pgfusepath{stroke}%
 }
 \state{final}{%
   \pgfsetlinewidth{\pgflinewidth}%
   \pgfpathmoveto{\pgfpointorigin}%
   \color{\endcolor!\x!\startcolor}%
   \pgfusepath{stroke}% 
 }
}

\makeatother

\tikz\draw[ line width=.4pt, decoration={width and color change,   
start color=yellow, end color=red}, decorate] (0cm,0cm) arc
(0:120:4cm) ;                                           

\end{document} 

To modifiy this code you need to adapt \pgfsetlinewidth{\x/40*0.075pt+\pgflinewidth} enter image description here

Related Question