40

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

4
  • I think no. But we can fill. And PSTricks can.
    – Leo Liu
    Mar 26, 2011 at 5:50
  • For PSTricks, see variableLW option in manual pst-news10.pdf.
    – Leo Liu
    Mar 26, 2011 at 5:52
  • 1
    Might this be done using pgfplots and point meta?
    – gerrit
    May 30, 2012 at 13:36
  • 1
    Asking even more than Paul Gessler, it would be great not only variable width going from length A to length B, but may be a specified function (so that you can get, for instance position 0 = width 1cm, position .2 = width .7cm, position .4 = width 1.6cm, position .83 = width 2.2cm, position 1 = width .8cm).
    – Manuel
    Aug 11, 2014 at 15:33

2 Answers 2

48

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

1
  • @Regis da Silva A better solution is to add an option to modify the width (possible only with simple forms and some cases) but like Leo writes, I think the simplest way it's to describe a path and then to fill it Mar 26, 2011 at 13:34
15

It is possible to increase line width using a foreach statement. Here is an example for a 3D helix:

\documentclass{article}
\usepackage{pgfplots}    

\begin{document}
\begin{tikzpicture}
    \begin{axis}[width=7cm, height=7cm, xmin=-1.05, 
                        xmax=1.05, axis lines=none, view={0}{25}]
        \foreach \x in {0,0.5,...,12.0}
       {\edef\temp{\noexpand\addplot3[blue, line width=1+\x/2 pt, 
                           domain=\x:\x+0.5,samples y=0] 
    ( { cos( deg(x) ) }, { sin( deg(x) ) }, { x } );
       } \temp } 
       \draw[>=latex,->] (105,100,10) -- (105,100,180);
       \node at (95,90,178) { $z$ }; 
    \end{axis} 
\end{tikzpicture}
\end{document}

result

1
  • Welcome to tex.sx! A tip: If you indent lines by 4 spaces, then they are marked as a code sample. You can also highlight the code and click the "code" button (with "101010" on it).
    – Seamus
    Apr 6, 2011 at 12:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .