\@ifnextchar in TikZ

\@ifnextchar in TikZ

Folgendes funktioniert wie erwartet:

\documentclass{minimal}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
    \makeatletter
    \node (01) at (0, 0) {};
    \draw (0, 0) -- (0, 01);
    \makeatother
\end{tikzpicture}
\end{document}

Aber wenn ich eines der 01s durch ersetze \@ifnextchar1001, was nach meinem Verständnis gleichwertig sein sollte, bricht es ab:

\documentclass{minimal}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
    \makeatletter
    \node (\@ifnextchar1001) at (0, 0) {};
    \makeatother
\end{tikzpicture}
\end{document}

gibt

! Argument of \XC@definec@lor has an extra }.
<inserted text> 
                \par 
l.6   \node (\@ifnextchar1001)
                              at (0, 0) {};

Und

\documentclass{minimal}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
    \makeatletter
    \draw (0, 0) -- (0, \@ifnextchar1001);
    \makeatother
\end{tikzpicture}
\end{document}

Erträge

! Argument of \XC@definec@lor has an extra }.
<inserted text> 
                \par 
l.6   \draw (0, 0) -- (0, \@ifnextchar1001)
                                          ;

Was ist in diesem Fall der relevante Unterschied zwischen 01und \@ifnextchar1001? Wie kann ich dies – oder eine ähnliche Konstruktion – wie vorgesehen zum Laufen bringen?

Antwort1

\@ifnextcharist aufgrund seiner internen Verwendung von nicht erweiterbar \futurelet. Ein Knotenname muss erweiterbar sein, er wird intern innerhalb von \csname… verwendet \endcsname.

Wenn Sie nur einfache Ziffern vergleichen möchten, reicht ein Test mit der normalen Makroargumentanalyse aus:

\documentclass{minimal}
\usepackage{tikz}

% \IfNextChar
% #1: the char for testing
% #2: code for true
% #3: code for false
% #4: next char
\makeatletter
\newcommand{\IfNextChar}[4]{%
  \ifx#1#4\@empty\@empty
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi
  {#2}{#3}%
}
\makeatother

\begin{document}
\begin{tikzpicture}
    \node (\IfNextChar1001) at (0, 0) {};
\end{tikzpicture}
\end{document}

Gleiches gilt für die Verwendung von \@ifnextcharals Koordinate.

Antwort2

Sie können die Zahl einfach mithilfe der PGF-Mathematik von der Basis 10 in die Basis 10 umwandeln. Vielleicht übertrieben, vielleicht aber auch nicht.

\documentclass{article}
\usepackage{tikz}
\begin{document}
\pgfmathbasetodec{\myconvresult}{001}{10}
\pgfmathsetmacro\myresult{\myconvresult+1}\myresult

\pgfmathbasetodec{\myconvresult}{0214}{10}
\pgfmathsetmacro\myresult{\myconvresult+1}\myresult

\pgfmathbasetodec{\myconvresult}{000035}{10}
\pgfmathsetmacro\myresult{\myconvresult+1}\myresult

\pgfmathbasetodec{\myconvresult}{301}{10}
\pgfmathsetmacro\myresult{\myconvresult+1}\myresult
\end{document}

Arithmetik in Basis 10

Es wundert mich, wenn es dafür nicht irgendwo eine Einstellung gibt …

verwandte Informationen