\newcommand로 나만의 명령 만들기
프리앰블에 \newcommand{\name}{replacement}를 쓰고, 인수 #1부터 #n이 필요하면 [n]을 붙이세요. 그때부터 \name은 텍스트와 수식 모두에서 그 정의로 바뀝니다.
인수 없는 명령
명령을 만들면 입력이 줄고 표기가 일관됩니다. 프리앰블에서 한 번 정의하고 어디서든 쓰세요.
집합과 제품 이름의 단축 명령
\documentclass{article}
\usepackage{amssymb}
\newcommand{\R}{\mathbb{R}}
\newcommand{\product}{Scissor LaTeX}
\begin{document}
Every $x \in \R$ has a square $x^2 \ge 0$.
This page was typeset by \product.
\end{document}인수와 기본값
이름 뒤의 [n]은 명령이 받는 인수의 개수를 나타내며, 정의 안에서는 #1, #2 등으로 씁니다. 두 번째 대괄호는 첫 인수에 기본값을 주어 선택 인수로 만듭니다.
인수와 선택적인 첫 인수
\documentclass{article}
\newcommand{\vect}[1]{\mathbf{#1}}
\newcommand{\norm}[1]{\left\lVert #1 \right\rVert}
\newcommand{\greet}[2][Hello]{#1, #2!}
\begin{document}
The length of $\vect{v}$ is $\norm{\vect{v}}$.
\greet{Erika} \greet[Good morning]{John}
\end{document}기존 명령 바꾸기
\renewcommand는 이미 있는 명령을 바꾸고, \providecommand는 아직 없을 때만 명령을 정의합니다. 이미 쓰인 이름에 \newcommand를 쓰면 TeX 설치 환경에서는 오류이므로, 그럴 때는 \renewcommand를 쓰세요.
\renewcommand와 \providecommand
\documentclass{article}
\renewcommand{\emph}[1]{\textbf{#1}}
\providecommand{\R}{\mathbb{R}}
\begin{document}
This is \emph{emphasised} in bold now, and $\R$ is defined.
\end{document}연산자 이름
sin, log 같은 함수 이름은 올바른 간격과 함께 똑바른 글자로 조판됩니다. \DeclareMathOperator(amsmath)로 나만의 연산자를 만들 수 있으며, 별표 붙은 형식은 \max처럼 디스플레이 수식에서 범위를 아래에 둡니다.
\DeclareMathOperator와 별표 붙은 형식
\documentclass{article}
\usepackage{amsmath}
\DeclareMathOperator{\Var}{Var}
\DeclareMathOperator*{\argmax}{arg\,max}
\begin{document}
\[
\Var(X) = \mathbb{E}\left[(X - \mu)^2\right],
\qquad
\hat{\theta} = \argmax_{\theta} L(\theta)
\]
\end{document}나만의 환경
\newenvironment{name}[n]{before}{after}는 환경을 정의합니다. "before" 코드는 \begin{name}에서, "after" 코드는 \end{name}에서 실행됩니다. 인수는 "before" 부분에서 쓸 수 있습니다.
인수가 있는 환경
\documentclass{article}
\newenvironment{task}[1]
{\par\noindent\textbf{Task #1.}\ }
{\par}
\begin{document}
\begin{task}{44}
Measure the rainfall every morning.
\end{task}
\end{document}작동하는 것
| 명령 | Scissor LaTeX에서 | 참고 |
|---|---|---|
\newcommand, \renewcommand, \providecommand | 작동함 | 인수는 최대 9개, 기본값이 있는 선택적 첫 인수. 별표 붙은 형식도 가능. |
\DeclareRobustCommand | 작동함 | \newcommand와 같음. |
\newenvironment, \renewenvironment | 작동함 | 인수와 기본값 포함. |
\DeclareMathOperator, \DeclareMathOperator* | 작동함 | |
\def\name{...}, \gdef, \edef, \xdef | 일부 작동 | 단순한 정의와 #1 형식의 인수는 작동함. \def\pt(#1,#2){...} 같은 구분자 매개변수는 일치하지 않아 출력이 깨짐. |
\let\new\old | 작동함 | |
\NewDocumentCommand, \RenewDocumentCommand (xparse) | 지원하지 않음 | 정의는 버려지고, 명령을 쓴 곳에서 알 수 없는 명령으로 보고됨. |
\DeclarePairedDelimiter (mathtools) | 작동함 | |
\makeatletter, \makeatother | 받아들임 | 이름에 @가 들어간 명령은 실행되지 않음. |
정의는 조판 전에 전개되므로, 명령을 텍스트, 수식, 캡션, 섹션 제목에서 쓸 수 있습니다. 무한 반복을 막기 위해 전개는 안전 한도에서 멈추며, 문제 탭에 메시지가 표시됩니다.
xparse 명령
xparse의 \NewDocumentCommand는 읽지 않습니다. 정의는 버려지고, 명령을 쓴 곳에서는 메시지와 함께 이름을 건너뛰며 인수는 텍스트에 남습니다.
\NewDocumentCommand는 지원하지 않습니다
\documentclass{article}
\usepackage{xparse}
\NewDocumentCommand{\pair}{m m}{(#1, #2)}
\begin{document}
The pair \pair{a}{b} loses its parentheses and comma.
\end{document}문제 탭: Skipped 1 unknown command: \pair
해결하려면 같은 명령을 \newcommand{\pair}[2]{(#1, #2)}로 쓰세요. 선택 인수가 필요하다면 기본값이 있는 \newcommand로 흔한 경우를 처리할 수 있습니다.
TeX 설치 환경과 다른 점
xparse와 mathtools 설명서는 CTAN에 있습니다: xparse, mathtools. 연산자와 함수 이름은 합, 적분, 함수 이름도 참고하세요.
질문과 답변
LaTeX에서 새 명령은 어떻게 정의하나요?
프리앰블에 \newcommand{\name}{text}를 쓰세요. 인수가 두 개라면 이름 뒤에 [2]를 붙이고 텍스트에서 #1과 #2를 쓰세요.
\newcommand와 \def의 차이는 무엇인가요?
\newcommand는 이름이 비어 있는지 확인하고 선택 인수를 지원합니다. \def는 저수준 TeX 명령으로, 묻지 않고 덮어씁니다. \newcommand와 \renewcommand를 쓰는 것이 좋습니다.
Scissor LaTeX에서 \NewDocumentCommand가 작동하나요?
아니요. 그런 명령은 필수 인수와 선택 인수 하나를 처리할 수 있는 \newcommand로 다시 쓰세요.
이 페이지의 모든 예제는 사이트를 빌드할 때 Scissor LaTeX로 조판됩니다. 마지막 업데이트: 2026-11-07