
\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(N)우리:
\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}