
\newpoint{<point name>}{<point style>}
のようなマクロを作成するマクロ、つまり を作成したいと思います\Point<point name>{<x coord>}{<y coord>}{<label>}
。
私は見た引数として渡された名前を使用して新しいマクロを作成するマクロを定義するにはどうすればよいでしょうか?作成されたマクロに引数を受け取らせる方法を示していないため、これは途中までです。
私は試した:
\newcommand*{\newpoint}[2]{%
\tikzset{#1/.style={#2}}%
\newcounter{point#1}\setcounter{point#1}{0}%
\def\csname Point#1\endcsname (##1,##2)|##3;{%
\stepcounter{point#1}\fill[#1] (##1,##2) circle (2pt) node[above](#1-\thepoint#1){##3};}%
}
しかし、どうやら、正直言って、うまくいきませんでしたUse of \csname doesn't match it's definition
...もう、自分が何をしているのか分かりません。
これが私のM(いいえ)私たちは:
\documentclass[tikz, border=2mm]{standalone}
\newcommand*{\newpoint}[2]{%
\tikzset{#1/.style={#2}}%
\newcounter{point#1}\setcounter{point#1}{0}%
\def\csname Point#1\endcsname (##1,##2)|##3;{%
\stepcounter{point#1}\fill[#1] (##1,##2) circle (2pt) node[above](#1-\thepoint#1){##3};}%
}
\newpoint{A}{red}
\begin{document}
\begin{tikzpicture}
\PointA(1,2)|A;
\end{tikzpicture}
\end{document}
答え1
@daleif が述べたように、\expandafter
の前に置きます\def
。そして、 を取得するには、そこで\thepointA
を使用する必要があります\csname ... \endcsname
。これがあなたが望んでいることだと思います:
\newcommand*{\newpoint}[2]{%
\tikzset{#1/.style={#2}}%
\newcounter{point#1}\setcounter{point#1}{0}%
\expandafter\def\csname Point#1\endcsname (##1,##2)|##3;{%
\stepcounter{point#1}\fill[#1] (##1,##2) circle (2pt) node[above](#1-\csname thepoint#1\endcsname){##3};}%
}
\newpoint
そして、ここでは、@egreg による疑似カウンターを使用したソリューションに基づき、マクロが\point<name>
呼び出されるたびに疑似カウンターが増分される、 の各呼び出しに対する実際のカウンターを使用しないソリューションを示します。
\newcounter{pointnumber}
\newcommand\StepPointNumber[1]{%
\setcounter{pointnumber}{\csname number@point#1\endcsname}%
\stepcounter{pointnumber}
\expandafter\xdef\csname number@point#1\endcsname{\thepointnumber}%
\expandafter\show\csname number@point#1\endcsname
}
\newcommand*{\newpoint}[2]{%
\tikzset{#1/.style={#2}}%
\expandafter\def\csname number@point#1\endcsname{0}%
\expandafter\def\csname Point#1\endcsname (##1,##2)|##3;{%
\StepPointNumber{#1}%
\fill[#1] (##1,##2) circle (2pt) node[above](#1-\csname number@point#1\endcsname){##3-\csname number@point#1\endcsname};}%
}
答え2
演算処理を実行する場合を除き、新しいカウンターを割り当てる必要はないと思います。
ただし、重要な点は、\csname
正しい方法で使用する必要があるということです。
\documentclass[tikz, border=2mm]{standalone}
\newcommand*{\newpoint}[2]{%
\tikzset{#1/.style={#2}}%
\expandafter\def\csname number@point#1\endcsname{0}%
\expandafter\def\csname Point#1\endcsname (##1,##2)|##3;{%
\fill[#1] (##1,##2) circle (2pt) node[above](#1-\csname number@point#1\endcsname){##3};}%
}
\newpoint{A}{red}
\begin{document}
\begin{tikzpicture}
\PointA(1,2)|A;
\end{tikzpicture}
\end{document}