다른 명령의 인수 내에서 명령을 사용할 수 있게 하려면 어떻게 해야 합니까?

다른 명령의 인수 내에서 명령을 사용할 수 있게 하려면 어떻게 해야 합니까?

를 사용하여 환경을 정의할 때 "이전" 부분 내부를 \newenvironment사용하여 newcommand환경 내부의 코드에서 명령을 사용할 수 있도록 할 수 있습니다.

간단한 명령으로 가능합니까? 즉, 외부에 정의되지 않은 매크로를 사용자가 인수로 사용할 수 있도록 명령을 만들 수 있습니까?

예: 수학적 집합 정의를 조판하기 위한 명령을 코딩 하고 다음과 같이 내부에서 명령이 사용되기를 \set원한다고 가정합니다.\suchthat

\set{x\in X \suchthat x > 42}

이것은 쉽지만 \suchthat명령이 \set.

답변1

명령을 다음과 같이 정의할 수 있습니다.

\newcommand\set[1]{%%
   \begingroup
     \def\suchthat{... some definition...}%%
     ... other macro content ....
    \endgroup}

그리고 이는 \suchthat매크로 내에서 사용할 수 있게 됩니다.

\bgroup및 를 사용하여 이 작업을 수행할 수 있지만 \egroup수학 모드에서 사용할 경우 간격이 처리되는 방식에 대한 결과가 포함된 하위 공식이 생성됩니다. 및 여기 \begingroup에서는 \endgroup새 매크로의 정의를 지역화합니다. 다음 중 하나를 사용하여 로컬 명령 , 및 을 정의할 \def\edef있습니다 \newcommand.

@barbarabeeto의 제안에 따라 서문에서 다음과 같이 할 수 있습니다.

\makeatletter
\def\ae@suchthat{... definition of such that ...}
\newcommand\aset[1]{%%
  \let\suchthat\ae@suchthat
    the body of the macro: #1
  \let\suchthat\undefined
}
\makeatother

이것이 훨씬 낫습니다. barbarabeeto가 제안한 이유에 더해, 이를 통해 나중에 문서에서 액세스할 수 있는 값을 매크로에서 정의하거나 설정할 수도 있습니다. 접근 방식 을 사용하면 \begingroup/\endgroup세계화해야 하지만 원하는 것이 아닐 수도 있습니다.

하이브리드 접근 방식은 다음과 같습니다.

\makeatletter
\def\ae@suchthat{... definition of such that ...}
\newcommand\aset[1]{%%
  \begingroup
    \let\suchthat\ae@suchthat
      the body of the macro: #1
  \endgroup
}
\makeatother

답변2

A. Ellett의 제안은 훌륭하지만 이 접근 방식에는 몇 가지 미묘함이 있습니다.

  1. 매크로가 수학 모드에서 명확하게 사용되기 때문에 정의를 \bgroup및 로 지역화하는 \egroup것은 좋지 않습니다 . \set그러한 구성은 공간이 동결되고 선의 확장 및 축소에 참여하지 않는 결과로 하위 공식을 만들 수 있습니다. 사용하고 \begingroup있으며 \endgroup이 문제를 겪지 않습니다.

  2. 섹션 제목에 \suchthat원하는 내용이 있을 수 있으므로 어쨌든 기본 정의를 제공해야 합니다.\suchthat

  3. 같은 이유로 두 명령 모두 강력하게 작성되어야 합니다.

구현은 다음과 같습니다.

\documentclass{article}
\usepackage{etoolbox}

\makeatletter
\newrobustcmd{\suchthat}{%
  \@latex@error{Use \noexpand\suchthat only in \string\set}
    {You are allowed to use \noexpand\suchthat only in the\MessageBreak
     argument of \string\set}%
}
\newcommand\giga@suchthat{\mid}
\newrobustcmd{\set}[1]{%
  \begingroup
  \let\suchthat\giga@suchthat
  \{\,#1\,\}%
  \endgroup
}
\makeatother

\begin{document}

\tableofcontents

\section{The set $\set{x\in X \suchthat x>42}$}

Let's study $\set{x\in X \suchthat x>42}$.

\suchthat

\end{document}

터미널의 출력은 다음과 같습니다.

! LaTeX Error: Use \suchthat only in \set.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.27 \suchthat

? h
You are allowed to use \suchthat only in the
argument of \set

출력

여기에 이미지 설명을 입력하세요

관련 정보