1

I have been trying to replicate Asymptote's dot style in TikZ, having the outer diameter equal to dotfactor * linewidth(p) where p denotes the given or current pen and dotfactor is 6 by default.

What is wrong with this example, it does not compile at all?

\documentclass{article}

\usepackage{lmodern}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\usepackage{graphicx}
\usepackage{float}
\usepackage{tikz}
\usepackage{pgfkeys}

\begin{document}

\tikzset{
    dot/.style={circle,fill,minimum size=6*\pgfkeysvalueof{/tikz/line width}}
}

\begin{figure}[H]
    \centering
    \begin{tikzpicture}[scale=2]
        \coordinate [dot,label=left:$A$] (A) at (1, 0);
        \coordinate [dot,label=below:$B$] (B) at (8, 3);
        \draw (A) -- (B);
    \end{tikzpicture}
\end{figure}

\end{document}
ivankokan
  • 1,046
  • 1
    Have a look at https://tex.stackexchange.com/questions/31001/how-does-pgfkeysvalueof-work – Colo Jan 08 '20 at 14:18

1 Answers1

3

The line width is stored in \pgflinewidth.

\documentclass{article}

\usepackage{lmodern}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\usepackage{float}
\usepackage{tikz}

\begin{document}

\tikzset{
    dot/.style={circle,fill,inner sep=6*\pgflinewidth/2}
}

\begin{figure}[H]
    \centering
    \begin{tikzpicture}[scale=2]
        \coordinate [dot,label=left:$A$] (A) at (1, 0);
        \coordinate [dot,label=below:$B$] (B) at (5, 2);
        \draw (A) -- (B);
        \draw[very thick] (1,-1) coordinate[dot,label=left:$A'$] (A')
        -- (5,1) coordinate[dot,label=below:$B'$] (B');
    \end{tikzpicture}
\end{figure}
\end{document}

enter image description here

It is easy to add the dotfactor to the game.

\documentclass[tikz,border=3mm]{standalone}

\tikzset{
    dot/.style={circle,fill,minimum
    size=\pgfkeysvalueof{/tikz/dotfactor}*\pgflinewidth-\pgflinewidth,inner sep=0pt,outer sep=0pt,
    draw},
    dotfactor/.initial=6
}
\begin{document}
    \begin{tikzpicture}[scale=2]
        \coordinate [dot,label=left:$A$] (A) at (0, 0);
        \coordinate [dot,label=below:$B$] (B) at (4, 2);
        \draw (A) -- (B);
        \draw[very thick] (0,-1) coordinate[dot,label=left:$A'$] (A')
        -- (4,1) coordinate[dot,label=below:$B'$] (B');
        \draw[very thick,dotfactor=4] (0,-2) coordinate[dot,label=left:$A''$] (A'')
        -- (4,0) coordinate[dot,label=below:$B''$] (B'');
\end{tikzpicture}
\end{document}

enter image description here

EDIT: Changed to minimum width in the lower answer. It also draws the dots (which is why there is a -\pgflinewidth) in order to avoid gaps.

  • Thanks! Why inner sep instead of minimum size? – ivankokan Jan 09 '20 at 14:35
  • 1
    @ivankokan Because if you keep the inner sep at some value, the node won't shrink even if you decrease minimum size further. –  Jan 09 '20 at 14:44
  • Hm, according to 3.6 Node Size section in the manual (and considering that dots in my example are not supposed to contain any text), I guess both variants would give the expected result. Do you have some counterexample? – ivankokan Jan 09 '20 at 14:55
  • @ivankokan You can just explore what happens if you use dot/.style={circle,fill,minimum width=\pgfkeysvalueof{/tikz/dotfactor}*\pgflinewidth}, in the code above. –  Jan 09 '20 at 14:59
  • 1
    Based on https://tex.stackexchange.com/a/48596/115879, I would say that inner sep=0pt, minimum size=(\pgfkeysvalueof{/tikz/dotfactor}-1)*\pgflinewidth is exactly what I wanted ("no additional space" + "(dotfactor-1) times the line width as two line width halves will be added").

    Also, when drawing edge, one should specify .center anchors (due to outer sep gaps).

    – ivankokan Jan 09 '20 at 15:33
  • 1
    @ivankokan Yes, that also works. It is just two keys instead of one. But you are right, it is perhaps more intuitive. So I changed the answer accordingly. The gaps can be avoided by also drawing the circles (which, of course, means that we have to subtract half a line width from the radius or a line width from the diameter/width). –  Jan 09 '20 at 15:40
  • 1
  • Due to roundness and rendering issues, add outer sep = 0. (In my (1, 0) - (8, 3) case there is a gap around (1, 0).)
  • – ivankokan Jan 09 '20 at 16:20
  • 1
    @ivankokan Thanks! Done. –  Jan 09 '20 at 16:41