
を使用して環境を定義する場合\newenvironment
、newcommand
「before」部分内で を使用して、環境内のコードでコマンドを使用できるようにすることができます。
これは単純なコマンドで可能ですか? つまり、ユーザーが外部で定義されていないマクロを引数に使用できるようなコマンドを作成できますか?
\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 氏の提案は良いものですが、このアプローチには微妙な点がいくつかあります。
マクロが明らかに数式モードで使用されているため、 と を使用して定義をローカライズするのは適切ではありません
\bgroup
。このような構成ではサブ式が作成され、その結果、スペースが固定され、行の伸縮に関与しなくなります。 と を使用すると、この問題は発生しません。\egroup
\set
\begingroup
\endgroup
セクション タイトルで
\suchthat
必要になる場合があるため、のデフォルト定義を必ず指定する必要があります。\suchthat
両方のコマンドは、同じ理由で堅牢にする必要があります。
ここに実装があります。
\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
出力