# Wedged/tapering paths in TikZ

## Initial question

Is there an easy way to vary the thickness of a path along its length to create a wedge (for instance, to create a stereochemical cue)?

e.g. cis-cyclohexane-1,2-diol, drawn in ACD/ChemSketch.

The expanding waves decorator looks perfect for receding bonds, but there is no listed equivalent for approaching bonds.

## Playing with expanding waves decorator

Based on the thread that Jake linked, I had a look at variables that can be tweaked with the expanding waves decorator. I don't think the current PGF/TikZ manual explicitly mentions it, but the expanding waves decorator has a segment length variable, as used in this example - setting it to my linewidth of 0.4 pt yields a solid wedge.

As Jake points out, this is the Wrong Way to approach this problem for performance reasons.

-
If you want a general solution for varying the thickness of a (possibly curved) path, take a look at Altermundus' answer to Trait with Variable Thickness. However, this might be overkill if all you're really looking for is a triangle. –  Jake Jun 30 '11 at 4:43
@Jake - Strange that I didn't see that when searching for precedents. That is indeed total overkill, however it gives me an idea - if I control the spacing of waves with the expanding waves decorator, they could converge into a wedge... Trying this... –  Richard Terrett Jun 30 '11 at 5:09
Oh no, don't do that! Decorations are really slow, and you won't get a very "proper" result with this approach. Give it an hour or so, once Altermundus and Andrew Stacey see this, they will no doubt come up with something stunningly simple and beautiful. –  Jake Jun 30 '11 at 5:12
@Jake - I suspected speed would be an issue. I had a go at it before I read your reply. Looks good on screen and is reasonably performant however I don't know how it would work in bulk or when printed. –  Richard Terrett Jun 30 '11 at 5:30

Here's a triangle decoration. It replaces the first segment of a path with an isosceles triangle. You use it by specifying the option triangle path in a draw or fill command. The width can be specified with the optional argument (i.e. triangle path=2cm will make the triangle 2 cm wide).

The following commands

\draw [fill=yellow,triangle path] (0,0) -- (3,2);
\fill [triangle path=4ex] (4,1) -- (7,0);


will yield

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations}

\begin{document}

\pgfdeclaredecoration{triangle}{start}{
\state{start}[width=0.99\pgfdecoratedinputsegmentremainingdistance,next state=up from center]
{\pgfpathlineto{\pgfpointorigin}}
\state{up from center}[next state=do nothing]
{
\pgfpathlineto{\pgfqpoint{\pgfdecoratedinputsegmentremainingdistance}{\pgfdecorationsegmentamplitude}}
\pgfpathlineto{\pgfqpoint{\pgfdecoratedinputsegmentremainingdistance}{-\pgfdecorationsegmentamplitude}}
\pgfpathlineto{\pgfpointdecoratedpathfirst}
}
\state{do nothing}[width=\pgfdecorationsegmentlength,next state=do nothing]{
\pgfpathlineto{\pgfpointdecoratedinputsegmentfirst}
\pgfpathmoveto{\pgfpointdecoratedinputsegmentlast}
}
}

\tikzset{
triangle path/.style={decoration={triangle,amplitude=#1}, decorate},
triangle path/.default=1ex}

\begin{tikzpicture}
\draw [fill=yellow,triangle path] (0,0) -- (3,2);
\fill [triangle path=4ex] (4,1) -- (7,0);
\end{tikzpicture}
\end{document}

-
That works brilliantly –  Richard Terrett Jun 30 '11 at 5:53
Very nice. Once of these days I'll have to learn about decorations. (I'd've used a to path, but only because I'm scared of decorations.) –  Andrew Stacey Jun 30 '11 at 13:32