6

I write a command to help me draw circle easy:

\def\Tdot@i{circle (1mm)}
\def\Tdot@ii[#1]{circle (#1)}
\def\Tdot{%
    \@ifnextchar[%
    {\Tdot@ii}%
    {\Tdot@i}%
}

When I try, it works well:

\documentclass{minimal}

\makeatletter

\def\Tdot@i{circle (1mm)} \def\Tdot@ii[#1]{circle (#1)} \def\Tdot{% @ifnextchar[% {\Tdot@ii}% {\Tdot@i}% }

\makeatother

\begin{document} \Tdot is circle (1mm), \Tdot[5mm] is circle (5mm). \end{document}

so I try to bring it to tikz, it raise some error:

% ...

\usepackage{tikz}

\begin{document}

\begin{tikzpicture} \draw (0, 0) circle (1mm); % goodA % Package tikz Error: Giving up on this path. Did you forget a semicolon? % \draw (0, 0) \Tdot; % Package tikz Error: Giving up on this path. Did you forget a semicolon? % \draw (0, 0) \Tdot[5mm]; \end{tikzpicture}

\end{document}

Is the if-else bad for TikZ?

Peterlits Zo
  • 163
  • 4
  • TikZ uses its own parser. You can't easily modify its behavior. This parser accepts external commands at the location of the coordinates but not between the coordinates. – Paul Gaborit Nov 19 '21 at 06:26
  • 1
    Your macro has to be expandable to be used on a TikZ path. The TikZ parser expands macros until it find something that it can understand. Otherwise it gives up after 100 expansions. \@ifnextchar uses \futurelet which is not expandable. – Henri Menke Nov 19 '21 at 08:30
  • This also tell me why: https://tex.stackexchange.com/a/349843/217492. – Peterlits Zo Nov 19 '21 at 15:22

1 Answers1

7

TikZ uses its own parser. You can't easily modify its behavior. The TikZ parser accepts external commands at the location of the coordinates but not between the coordinates (not as path operators).

As a workaround, you can use a to path style...

\documentclass[tikz]{standalone}

\tikzset{ Tdot/.style={to path={circle[radius=#1]}}, Tdot/.default=1mm, }

\begin{document} \begin{tikzpicture} \draw (0,0) to[Tdot] cycle to[Tdot=3mm] cycle; \draw[red] (1,0) to[Tdot=2mm] cycle ; \end{tikzpicture} \end{document}

enter image description here

Paul Gaborit
  • 70,770
  • 10
  • 176
  • 283
  • Great idea. I think the better idea is let the Tdot be the style of node: \tikzset{Tdot/.style={shape=circle,inner sep=#1,fill=black}, Tdot/.default=0.7mm,} – Peterlits Zo Nov 19 '21 at 15:51
  • @PeterlitsZo This is indeed another solution. But in this case the circle is no longer part of the path. – Paul Gaborit Nov 19 '21 at 17:41