8

Is it possible to draw a line which has line cap=round at the beginning and line cap=rect at the end? In the MWE below, that would correspond with drawing this whole thing as one black line instead of a red line on top of a black line:

\documentclass{article}

\usepackage{tikz}

\begin{document}
    \begin{center}
    \begin{tikzpicture}[scale=2]
        
        \draw[line width=0.5cm,line cap=round] (0,0) -- (1,0);
        \draw[red,line width=0.5cm,line cap=rect] (1,0) -- (2,0);
        
    \end{tikzpicture}
    \end{center}
\end{document}

MWE output:

enter image description here

4
  • No I think it's impossible, unless a line is imitated by filling. Commented Jun 6, 2023 at 3:28
  • 1
    I am OK with imitating a line by filling, but I would like a solution which can deal with multi-segment lines (A)--(B)--(C), which I think would require a lot of work for proper corner beveling/rounding. Commented Jun 6, 2023 at 3:56
  • What do you mean by multi-segment lines? Are these paths connect at B (i.e. B is a coordinate) or disconnected (B is a node). In the latter, you should use two separate paths (or edges). Commented Jun 6, 2023 at 13:04
  • I meant for A, B, and C to be coordinates (one continuous path) Commented Jun 6, 2023 at 15:12

3 Answers 3

8

This is exactly why the Cap arrow tips of the arrows.meta library were made:

Recall that a cap is a way of ending a line. The graphic languages underlying TikZ (PDG, PostScript or SVG) all support three basic types of line caps on a very low level: round, rectangular, and “butt”. Using cap arrow tips, you can add new caps to lines and use different caps for the end and the start.

However, while the true caps round and rect extend over the actual points of the path, the Cap arrow tips (like all default arrow tips) will only touch these points.

This is either actually exactly what you want in the first place or a bit confusing when combined with the real caps.

However, we can alter the Cap tips a bit to act like real caps (the one with '). Though, again, that's probably not what you want.

In the second picture below, you can see using two kinds of Caps for connecting multiple nodes. It's usually always better to use separate paths for connecting nodes, especially when dealing with arrow tips (edges create separate paths).

Since using the Butt Cap has no advantage over the butt cap unless you need it to be an arrow tip, I'm using two kinds of arrow tips in this example.

Code

\documentclass[tikz]{standalone}
\usetikzlibrary{arrows.meta}
\colorlet{Green}{green!70!black}
\colorlet{Red}  {magenta!70!black}
\tikzset{cap or left arrow/.code=% don't use this
  \ifcsname pgfset#1cap\endcsname\csname pgfset#1cap\endcsname\def~{cap}\else
    \pgfsetarrowsstart{#1[Red]}\def~{\textcolor{Red}{tip}}\fi}
\begin{document}
\tikz[line width=5mm, x=3cm, y=1.5cm, no/.tip=,
  Butt  Cap'/.tip = {Butt  Cap[sep = 0 +-.5]},
  Round Cap'/.tip = {Round Cap[sep = 0 +-.5]}]
\foreach[count=\i] \cap in {butt, rect, round, Butt Cap, Butt Cap', Round Cap, Round Cap'}
  \foreach[count=\j] \Arrow in            {no, Butt Cap, Butt Cap', Round Cap, Round Cap'}
      \path[shift={(\j, \i)}]
        (-.5cm, -.5cm) edge[help lines, to path=grid(\tikztotarget)](2.5cm,.5cm)
        (0, 0) edge[-{\Arrow[Green]}, cap or left arrow=\cap]
          node[above=3mm, scale=.5]{\texttt{\cap} ~ -- \texttt{\Arrow} \color{Green}tip}
          +(right:2cm);

\tikz\matrix[column sep=5mm]{
\node foreach[count=\i]\t in {A, B, C} [draw, circle] (\t) at (\i*60:3) {\t};
\path[Round Cap - Triangle Cap, line width=5mm]
  (A) edge (B)
  (B) edge (C)
  (C) edge[bend right] node[sloped, white]{Round Cap} (A);
&
\node foreach[count=\i]\t in {A, B, C} [draw, circle] (\t) at (\i*60:3) {\t};
\path[line cap=round, - Triangle Cap, line width=5mm]
  (A) edge (B)
  (B) edge (C)
  (C) edge[bend right] node[sloped, white]{round cap} (A);
\\};
\end{document}

Output

enter image description here

enter image description here

1
  • Thanks for pointing out the fine-print, which can be easily overlooked.
    – MS-SPO
    Commented Jun 6, 2023 at 13:36
7

Here are a few ways to do it (there are even more). Basic ideas:

  • make lines wide enough \begin{tikzpicture}[line width=8pt,]
  • 1st one is just a draw without any options \draw (0, 0) -- (2, 0);
  • 2nd uses [line cap=round, -Butt Cap], i.e. the right side cut-off
  • 3rd uses [line cap=round, Butt Cap-], t.e. the left side cut-off
  • 4th uses [{Round Cap[]}-], i.e. makes the left side round
  • 5th for reference [<-] of the syntax, i.e. you replace the < in options by {*whatever*}

result

\documentclass[10pt,border=3mm,tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}% provides Butt Cap etc.

\begin{document}

 \begin{tikzpicture}[
    line width=8pt,
 ]
    \draw                               (0, 0) -- (2, 0);
    \draw [line cap=round, -Butt Cap]   (0,-1) -- (2,-1);
    \draw [line cap=round, Butt Cap-]   (0,-2) -- (2,-2);
    \draw [{Round Cap[]}-]              (0,-3) -- (2,-3);
    \draw [<-]                          (0,-4) -- (2,-4);
 \end{tikzpicture}

\end{document}

BTW: The way the 4th one looks like seems to depend on magnification, at least of the pdf-viewer ...

res2

4

Round Cap at one end, Butt Cap at the other. (Requires arrows.meta.)

enter image description here

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{arrows.meta}

\begin{document}
    \begin{center}
    \begin{tikzpicture}[scale=2]
        
        \draw[line width=0.5cm, Round Cap-Butt Cap] (0,0) -- (2,0);
        
    \end{tikzpicture}
    \end{center}
\end{document}

You must log in to answer this question.

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