1

I have a large number of tikz figures involving circular nodes (some of which are filled white), which are connected by edges (some of which are thick).

The problem I am having is that whichever line cap I use, the edge either doesn't properly meet the node (e.g. line cap=butt), or it bleeds into the white fill, as in the following MWE:

\documentclass[tikz]{standalone}
\tikzset{
    every picture/.style={line width=0.45pt, line cap=round},
    vtx/.style={circle, draw, fill, inner sep=0pt, minimum width=4.88125pt},
    whitevtx/.style={circle, draw, fill=white, inner sep=0pt, minimum width=4.88125pt},
}
\tikzstyle{very thick}=[line width=1.65pt]

\begin{document}
    \begin{tikzpicture}
        \node[vtx] (A1) at (0,0) {};
        \node[whitevtx] (B1) at (2,0) {};
        \draw[very thick] (A1) -- (B1);
    \end{tikzpicture}
\end{document}

This draws the following simple figure: Two nodes conencted by an edge

Here it is zoomed in:

Zoom of edge meeting node

Does anyone have an elegant solution to fix this? I've considered the following possibilities:

  1. Place the edges on a background pgflayer. The problem with this is that the line cap defaults to butt so then needs to be re-specified (but maybe I did something wrong and there is a solution here).

  2. Draw all nodes as coordinates first, draw the lines, and then draw the vertices over the coordinates. Ugh.

I have a very large number of these images, so the solution really does need to be elegant and simple, and ideally fixed simply by applying some new global style.

Finally, some other constraints: it's against the rules to change the specified line widths, and I would ideally also keep the line cap=round generic specifier for edges (though not necessary for edges between nodes, if these can be distinguished), as the styles set here will be applied to other types of figure too.

Edit: Thought not necessary in my particular case, a solution that could cope with really thick lines (e.g. line width=4pt) would get extra brownie points!

6
  • 1
    Maybe controlling the outer sep=?!
    – Sigur
    Apr 8, 2022 at 11:41
  • @Sigur I was struggling with this, because there are also sometimes thinner (0.45pt) edges, but line cap=butt applied to the very thick style, together with specifying outer sep=0.1 (or smaller) on the whitevtx style might actually just do it! Thanks.
    – rbrignall
    Apr 8, 2022 at 11:50
  • \tikzstyle{very thick}=[line width=1.65pt] >>> we should not redefine [very thick] (already defined, see pgfmanual) although in this case it causes no problems. Also we should use \tikzset instead of outdated tikzstyle.
    – Black Mild
    Apr 8, 2022 at 12:11
  • @BlackMild thanks for the critique of my code. Off-topic, but out of interest: why should we not redefine very thick?
    – rbrignall
    Apr 8, 2022 at 13:20
  • @rbrignall because someone (maybe you ^^) can be confused with the name very thick; or when the code is shared etc. I recommend the name my thick.
    – Black Mild
    Apr 8, 2022 at 13:33

1 Answer 1

2

Like this:

enter image description here

Edit: The background of your TikZ settings is unknown (to us), it is hard to judgement it. Anyway, from my experiences I don't see a good reason that all lines should have the same line cups (line cap=butt). Especially because in many circumstance such settings apparently may cause problems. I would rather use this settings locally where is really needed and for shorter code eventually define new stile for this, for example

lb/.style = {line cap=round}

Your problem can be simple solved, for a some extend of connection lines thickness as has note @Sigur in his comment, by adding to circles style outer sep=0pt:

\documentclass[border=3.141592]{standalone}
\usepackage{tikz}
\tikzset{
    vtx/.style={circle, draw, fill=#1, 
                inner sep=0pt, outer sep=0pt, % <---
                minimum width=5pt},
    vtx/.default = black,

        }
    

\begin{document}
    \begin{tikzpicture}
        \node[vtx]          (A1) at (0,0) {};
        \node[vtx=white]    (B1) at (2,0) {};
        \draw[very thick]   (A1) -- (B1);
    \end{tikzpicture}
\end{document}

However, for thicker connection lines you need to redesign your image to something like this:

\documentclass[border=3.141592]{standalone}
\usepackage{tikz}
\tikzset{
    vtx/.style={circle, draw, fill=#1,
                inner sep=0pt, outer sep=0pt, % <---
                minimum width=5pt},
    vtx/.default = black,
     lb/.style = {line cap=round}
        }

\begin{document}
    \begin{tikzpicture}
        \coordinate (A1) at (0,0);
        \coordinate (B1) at (2,0);
        \draw[line width=4pt]   (A1) -- (B1);
        \node[vtx]       at (A1) {};
        \node[vtx=white] at (B1) {};
        \draw[very thick,lb]  (0,0.5) -- (2,0.5);
    \end{tikzpicture}
\end{document}

enter image description here

7
  • Simple and elegant !
    – Black Mild
    Apr 8, 2022 at 12:03
  • @BlackMild, thank you very much!
    – Zarko
    Apr 8, 2022 at 12:05
  • Thanks! Though this has dropped the line cap=round global setting. Applying line cap=butt to very thick might be ok.
    – rbrignall
    Apr 8, 2022 at 12:07
  • @rbrignall, see edited answer.
    – Zarko
    Apr 8, 2022 at 12:51
  • Thanks. Yes, I had the "draw coordinates then draw lines then draw nodes" solution as an option in my question. Note that the constraint to preserve line cap=round was clearly stated in my original question - I've not said why, but I don't think that's relevant to the question.
    – rbrignall
    Apr 8, 2022 at 13:09

You must log in to answer this question.

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