lines with variable width in TikZ (to convert handwritten Xournal files to tikz)

TikZ supports \foreach loops, maybe this helps when the lines are created according to a specific pattern. See here, page 504 for further information.

Moreover, \tikzset can save you some typing. It lets you define an own style for using in \draw [mystyle].

\tikzset{lw/.style = {line width=2pt}}
\draw [lw] (1,1) -- (2,2);

But if it really comes to "handdrawn" sketches, you might be better off using a vector graphics program such as Inkscape.


Here's my first attempt using to paths. The idea is to use the to to break up the path into segments and render each one separately, setting the line width on each one. The problem with this (and I can't see a way around this, but it might not be significant if the line widths don't change too much and the line angles don't change much between segments) is that the path is split into segments which means that they don't join up neatly.

\documentclass{standalone}
%\url{http://tex.stackexchange.com/a/43418/86}
\usepackage{tikz}
\tikzset{
  variable line width/.style={
    every variable line width/.append style={#1},
    to path={%
      \pgfextra{%
        \draw[every variable line width/.try,line width=\pgfkeysvalueof{/tikz/thickness}] (\tikztostart) -- (\tikztotarget);
      }%
      (\tikztotarget)
    },
  },
  thickness/.initial=0.6pt,
  every variable line width/.style={line cap=round, line join=round},
}
\begin{document}
\begin{tikzpicture}
\draw[variable line width={blue}] (1,1) to[thickness=1cm] (1,3) to[thickness=.5cm] (2,3);
\end{tikzpicture}
\end{document}

The variable line width key could be specified on the tikzpicture environment as an every draw/.style={variable line width}. To be able to write to[2pt] then one would have to do some funky stuff with the .unknown key handler (where's Ryan Reich when you need him) which would be possible, but I imagine that these are automatically generated and so I would guess that it would be possible to output syntax somewhat like the above - if not, say so and I can adapt the above to suit.

Here's the output of the (the original version of the) above:

TikZ lines with variable width

Update (2012-02-07): Added an every variable line width style which gets added to every inner path. Anything specified as an argument to the variable line width key gets appended to this for the duration of that path.

Tags:

Tikz Pgf