![라텍스의 어설션](https://rvso.com/image/305761/%EB%9D%BC%ED%85%8D%EC%8A%A4%EC%9D%98%20%EC%96%B4%EC%84%A4%EC%85%98.png)
저는 긴 라텍스 문서를 작성하고 있으며 수학 기호를 정의하기 위해 많은 매크로를 사용하고 있습니다. 표기법에 충돌이 없는지 확인하고 싶습니다. 예를 들어, 다음과 같습니다.
\newcommand{\symbolone}{A}
\newcommand{\symboltwo}{A}
일어나서는 안 된다. 라텍스에 내가 할 수 있는 주장 같은 것이 있나요?
\assert{\symbolone != \symboltwo}
?
답변1
\singledef
내 솔루션에는 제어 시퀀스(예:)를 정의하는 매크로가 도입되었지만 \def
다음 두 가지 기능을 더 제공합니다.
- 제어 순서가 정의되어 있는지 확인합니다. 그렇다면 오류 메시지가 인쇄되고 제어 시퀀스는 원래 의미를 유지합니다.
- 정의된 제어 시퀀스의 이름을
\singledeflist
내부 매크로에 저장합니다. 다음 제어 시퀀스가 을 통해 정의되면\singledef
정의되지만 동일한 제어 시퀀스( 를 통해 동일\ifx
)가 정의되고 에 저장되었는지 확인됩니다\singledeflist
. 그렇다면 오류 메시지가 인쇄되고 새 제어 순서가 정의되지 않은 상태로 유지됩니다.
코드:
\def\singledeflist{}
\def\singledef#1{%
\ifx#1\undefined
\def\next{\singledefA#1}%
\afterassignment\next
\expandafter\def\expandafter#1%
\else
\errmessage{The \string#1 is defined already}%
\expandafter\def\expandafter\tmp
\fi
}
\def\singledefA#1{\def\next{\singledefB#1}\expandafter\next\singledeflist\end}
\def\singledefB#1#2{%
\ifx#2\end
\expandafter\def\expandafter\singledeflist\expandafter{\singledeflist#1}%
\else
\ifx#1#2%
\errmessage{The \string#1 is the same as \string#2}%
\let#1=\undefined
\singledefC
\fi
\expandafter\next
\fi
}
\def\singledefC#1\end{\fi\fi}
\singledef\A{aha} % this is like \def\A{aha}
\singledef\B{bha} % this is like \def\B{bha}
\singledef\C{bha} % this prints the error: The \C is the same as \B
답변2
@egreg는 두 개의 완전히 확장된 문자열을 비교하는 방법을 보여주었습니다(두 개의 완전히 확장된 문자열이 동일한지 테스트하는 경우) 여기에 그의 기술을 적용할 수 있습니다.
기호를 재사용하는 경우 오류 메시지를 추가했습니다.
\documentclass{article}
\usepackage{pdftexcmds}
\newcommand{\symbolI}{A}
\newcommand{\symbolII}{\pi r^2}
\newcommand{\symbolIII}{\pi r^2}
\makeatletter
\newcommand{\TestSameSymbols}[2]{%
\ifnum\pdf@strcmp{#1}{#2}=\z@
\typeout{%
ERROR: \noexpand#2 (#2) is the same as \noexpand#1.%
}
\fi%
}
\makeatother
\begin{document}
\TestSameSymbols{\symbolI}{\symbolII}
\TestSameSymbols{\symbolII}{\symbolIII}
\[ \symbolI = \symbolII \]
\end{document}