TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

Is it possible to make the line cap in TikZ always be vertical (or horizontal or a 90 degree edge)? Maybe as an option to line cap or even better as an arrow tip.

I imagine a type vertical resulting in some thing like this vertical

horizontal could look like this

and a 90 degree corner, which is rotate so that one of it’s edges is parallel to the horizon. (Other that the triangle 90 degree cap.)
90 degree corner

I created the images with a clipping, but clipping the path is no option …

share|improve this question
The third looks possible, but the first two could be quite tricky because the shape you'd have to draw is somewhat difficult to compute. – Loop Space May 19 '12 at 18:05
@AndrewStacey: Thanks, I fear that. I guess it’s impossible to lengthen the path an cut it to get a vertcial/horizontal end, isn’t it? – Tobi May 21 '12 at 7:36
The difficulty would be knowing how to lengthen it. In your horizontal example, imagine that the actual path ended as the boundary of the curve first touched the horizontal line. Then you'd need to extend the line until the other side also touched it, but in your example this is quite clearly curved. How do we figure out that curving? One could do a straight extension, I guess. – Loop Space May 21 '12 at 7:52
@AndrewStacey: Ahh … I see. But could you provide an example for the 90 degree edge? – Tobi May 21 '12 at 8:16
I've been looking at the code and I don't think this is possible (or at least, easily possible) as an arrow tip. The point is that you need to know about the absolute coordinates when drawing the arrow but the arrow is rotated by a canvas transformation and these are not remembered so there's no way to say "invert the current canvas transformation". It might be possible via a decoration, though. – Loop Space May 22 '12 at 17:00
up vote 4 down vote accepted

Okay, here's a method for the third using a decoration. In a decoration, the transformation is set as a coordinate transformation which means that we can figure out what it does. That means we can go between the transformed coordinate system, in which the x-axis points along the line and the y-axis is normal to it, and the page system, in which the x-axis is horizontal and the y-axis vertical. We need to use both, which is why I don't think that this can be done with arrow tips as those are set using a canvas transformation which cannot be accessed to the same degree as a coordinate transformation.

Update 2012-05-23 Revamped version, now does horizontal and vertical ends. There are slight artefacts as it goes from the main path to the filled ends - don't know how to deal with those. There's also an "auto" version which selects the "best" of "box", "horizontal", or "vertical" depending on the angle of the path.


  \pgfmathsetmacro\boxed@xd{\boxed@xa - \boxed@xo}
  \pgfmathsetmacro\boxed@yd{\boxed@yb - \boxed@yo}
  \pgfmathsetmacro\boxed@sf{\boxed@xd * \boxed@xc + \boxed@yd * \boxed@yc > 0 ? 1 : -1}
  \pgfmathsetmacro\boxed@xd{\boxed@sf * \boxed@xd + \boxed@xo + \boxed@xc}
  \pgfmathsetmacro\boxed@yd{\boxed@sf * \boxed@yd + \boxed@yo + \boxed@yc}

  \pgfmathsetmacro\boxed@yd{(\boxed@ya - \boxed@yb) *\boxed@yc > 0 ? \boxed@ya + \boxed@yc: \boxed@yb + \boxed@yc}
  \pgfmathsetmacro\boxed@xd{(\boxed@ya - \boxed@yb) *\boxed@yc > 0 ? \boxed@xb + (\boxed@ya - \boxed@yb) * \boxed@xc/\boxed@yc: \boxed@xa +  (\boxed@yb - \boxed@ya) * \boxed@xc/\boxed@yc}

  \pgfmathsetmacro\boxed@xd{(\boxed@xa - \boxed@xb) *\boxed@xc > 0 ? \boxed@xa + \boxed@xc: \boxed@xb + \boxed@xc}
  \pgfmathsetmacro\boxed@yd{(\boxed@xa - \boxed@xb) *\boxed@xc > 0 ? \boxed@yb + (\boxed@xa - \boxed@xb) * \boxed@yc/\boxed@xc: \boxed@ya +  (\boxed@xb - \boxed@xa) * \boxed@yc/\boxed@xc}

  \pgfmathparse{atan2(abs(\boxed@xc),abs(\boxed@yc)) > 60 ? "horizontal" : (atan2(abs(\boxed@xc),abs(\boxed@yc)) > 30 ? "box" : "vertical")}
  \csname box@end@\pgfmathresult\endcsname  

  line cap start/.style={
    add line cap={.1}{#1}
  line cap end/.style={
    add line cap={-.1}{#1}
  line cap type/.style={
    line cap start=#1,
    line cap end=#1,
  line end path style/.style={
%    draw=red,line width=1pt
  add line cap/.style 2 args={
      mark=at position #1\pgflinewidth with {
          \pgfmathsetmacro\boxed@xc{\boxed@xo - \the\pgf@x}
          \pgfmathsetmacro\boxed@yc{\boxed@yo - \the\pgf@y}
          \csname box@end@#2\endcsname
        \path[line end path style] (\boxed@xa,\boxed@ya) -- (\boxed@xa + \boxed@xc,\boxed@ya + \boxed@yc) -- (\boxed@xd pt,\boxed@yd pt) -- (\boxed@xb + \boxed@xc,\boxed@yb + \boxed@yc) -- (\boxed@xb,\boxed@yb);
\begin{tikzpicture}[line width=1cm]
\draw[line cap type=box] (1,0) .. controls +(1,-2) and +(-1,-1) .. (6,0);
\draw[line cap type=box] (1,-4) .. controls +(1,2) and +(-1,1) .. ++(6,0);

\begin{tikzpicture}[line width=1cm]
\draw[line cap type=horizontal] (1,0) .. controls +(1,-2) and +(-1,-1) .. (6,0);
\draw[line cap type=horizontal] (1,-4) .. controls +(1,2) and +(-1,1) .. ++(6,0);

\begin{tikzpicture}[line width=1cm]
\draw[line cap type=vertical] (1,0) .. controls +(1,-2) and +(-1,-1) .. (6,0);
\draw[line cap type=vertical] (1,-4) .. controls +(1,2) and +(-1,1) .. ++(6,0);

\begin{tikzpicture}[line width=1cm]
\draw[line cap type=auto] (1,0) .. controls +(1,-2) and +(-1,-1) .. (6,0);
\draw[line cap type=auto] (1,-4) .. controls +(1,2) and +(-2,1) .. ++(6,0);



squared-off line endings

share|improve this answer

Your Answer


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.