명령 뒤에 공백을 피하기 위한 csname 메서드의 단점

명령 뒤에 공백을 피하기 위한 csname 메서드의 단점

나는 이것과 비슷한 문제를 겪었습니다.LaTeX 명령 뒤의 공백

나의 첫 번째 시도는 다음과 같았습니다.

\newcommand{\satip}{SAT\textgreater IP}

이로 인해 명령 뒤의 공백이 소모된다는 알려진 문제가 발생합니다.

\satip is a cool Protocol. %Produces: SAT>IPis a cool Protocol.
                             space missing ^^^

나는 주변을 검색하여 언급된 질문을 찾았습니다. 제공된 솔루션은 많은 도움이 되었지만 그 어느 것도 완전히 만족스럽지는 않았습니다. \satip/라텍스 문서에서는 조금 이상해 보이지만 \satip{}훨씬 더 마음에 듭니다. {}명령 뒤에 넣는 것을 잊어버리면 출력에 공백이 누락됩니다. 따라서 잘못 사용하면 오류가 발생하고 싶습니다.

가능한 해결책:

\def\satip#{SAT\textgreater IP}
%\satip is a cool Protocol. %doesn't compile, error

이 방법으로 여는 중괄호가 적용되지만 중괄호에는 다음 내용이 포함될 수 있습니다.

\satip{is} a cool Protocol.

이것은 잘 컴파일되지만 의미가 없으므로 오류가 발생하고 싶습니다. 현재 문제를 처리하는 방법은 다음과 같습니다.

\expandafter\def\csname satip{}\endcsname \relax{SAT\textgreater IP}
\def\satip#1{\csname satip{}\endcsname #1\relax}

%\satip{is} a cool Protocol. %Use of \satip{} doesn't match its definition.
%\satip is a cool Protocol. %Use of \satip{} doesn't match its definition.
\satip{} is a cool Protocol. %works

이제 내 질문:

이 매크로에는 두 번째 확장 단계가 필요합니다. 그게 문제를 일으킬 수 있나요? 다른 문제가 있나요? (이런 곳은 이전에 발견한 적이 없었기 때문입니다.)

추신: 제목이 좋지 않아서 죄송합니다. 더 나은 것을 생각해 내지 못했습니다. 자유롭게 편집하세요.

답변1

당신은 항상 사용할 수 있습니다

\newcommand*\satip{SAT\textgreater IP}
\satip{} is a cool protocol

문제가 보이지 않습니다.


그런데 마지막 정의에서는 de 매크로를 이름 satip{}(중괄호) 으로 정의합니다.포함됨매크로 이름에서) 뒤에 토큰이 옵니다 \relax. 매크로 에 와 #1사이를 넣으면 가 비어 있는 경우에만 작동할 수 있습니다 (즉, 빈 중괄호가 제공되는 경우에만 ).\endcsname\relax\satip#1\satip{} is...


이것이 당신이 원하는 것을 달성할 수 있을까요?

\newcommand*\satip[1]
 {\if\relax\detokenize{#1}\relax
    SAT\textgreater IP%
  \else
    \GenericError{} % <- I don't know what this argument does
       {Wrong use of \string\satip{}.} % <- short version
       {Wrong use of \string\satip. You must use \string\satip\space followed by an empyt argument `{}'.}% <- long version
  \fi}

답변2

{}나는 보조 매크로의 이름으로 사용하지 않을 것이지만 방법은 타당합니다:

\newcommand{\satip}[1]{\csname satip\string+\endcsname #1\relax}
\expandafter\def\csname satip\string+\endcsname\relax{%
  SAT\textgreater IP%
}

\satip{x}사용되는 경우 오류가 발생합니다.

! Use of \satip+ doesn't match its definition.
<argument> x

그러나 \satip ip그렇지 않습니다. 당신은해야합니다단계:

\newcommand{\satip}{}% initialize
\protected\def\satip#{\csname satip\string+\endcsname}
\expandafter\def\csname satip\string+\endcsname#1{%
  \csname satip\string+\string+\endcsname #1\relax
}
\expandafter\def\csname satip\string+\string+\endcsname\relax{%
  SAT\textgreater IP%
}

이제 \satip x및 둘 다 \satip{x}오류를 유발합니다.

! Use of \satip doesn't match its definition.
l.14 \satip x

? 
! Use of \satip++ doesn't match its definition.
<argument> x

l.16 \satip{x}

? 

\protected의 정의 앞에 주의하여 \satip"인수 이동" 컨텍스트에서 확장되지 않도록 하세요.

추상 버전:

\documentclass{article}

\makeatletter
\newcommand\definestringcommand[2]{%
  \@ifdefinable#1{\@definestringcommand#1{#2}}%
}

\newcommand{\@definestringcommand}[2]{%
  \begingroup
  \escapechar=\m@ne % get rid of the backslash
  % require brace
  \protected\xdef#1##{\expandafter\noexpand\csname\string#1\string+\endcsname}%
  % examine the argument
  \expandafter\xdef\csname\string#1\string+\endcsname##1{%
    \expandafter\noexpand\csname\string#1\string+\string+\endcsname##1\relax
  }%
  \expandafter\gdef\csname\string#1\string+\string+\endcsname\relax{#2}%
  \endgroup
}
\makeatother

\definestringcommand{\satip}{SAT\textgreater IP}

\begin{document}

\satip is nice

\satip{x} is nice

\satip{} is nice

\end{document}

이것이 유용한지 여부는 귀하에게 결정을 맡깁니다.

다른 구현: 을 확인한 후 성공할 경우 두 토큰을 모두 소모하는지 {확인합니다 .}

\documentclass{article}

\makeatletter
\newcommand\definestringcommand[2]{%
  \@ifdefinable#1{\@definestringcommand#1{#2}}%
}

\newcommand{\@definestringcommand}[2]{%
  \begingroup
  \escapechar=\m@ne % get rid of the backslash
  % require brace
  \protected\xdef#1##{%
    \expandafter\noexpand\csname\string#1\string+\endcsname
  }%
  \expandafter\gdef\csname\string#1\string+\endcsname{%
    #2%
    \afterassignment\@checkrightbrace\let\@forget= % the space counts
  }
  \endgroup
}
\newcommand{\@checkrightbrace}{%
  \@ifnextchar\egroup{\let\@forget= }{\@strcmderr\let\@forget= }%
}
\newcommand{\@strcmderr}{%
  \@latex@error{Non empty group}{The braces must contain nothing}%
}
\makeatother

\definestringcommand{\satip}{SAT\textgreater IP}

\begin{document}

\satip is nice

\satip{x} is nice

\satip{} is nice

\end{document}

관련 정보