\@ifnextchar em TikZ

\@ifnextchar em TikZ

O seguinte funciona conforme o esperado:

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

Mas quando substituo um dos 01s por \@ifnextchar1001, que deveria ser equivalente no meu entendimento, ele quebra:

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

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

e

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

rendimentos

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

Qual é a diferença relevante entre 01e \@ifnextchar1001neste caso? Como posso fazer com que esta – ou uma construção semelhante – funcione conforme pretendido?

Responder1

\@ifnextcharnão é expansível devido ao uso interno de \futurelet. Um nome de nó deve ser expansível, ele é usado internamente dentro de \csname... \endcsname.

Se você deseja apenas comparar dígitos simples, um teste usando a análise normal de argumentos de macro é suficiente:

\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}

O mesmo se aplica ao uso de \@ifnextcharcomo coordenada.

Responder2

Você pode simplesmente converter o número da base 10 para a base 10 usando a matemática PGF. Talvez exagero, talvez não.

\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}

aritmética na base 10

Estou surpreso se não houver uma configuração para isso em algum lugar ....

informação relacionada