왜 컴파일되지 않습니까? \h의 인수에는 추가 {가 있습니다.

왜 컴파일되지 않습니까? \h의 인수에는 추가 {가 있습니다.
\documentclass{article}
\usepackage{amsmath}

\begin{document}
\newcommand{\x}[1][1]{\ensuremath{a_{n-#1}}}
\newcommand{\e}[1][\left(1+\dfrac{1+\sqrt{5}}{2}\right)-\left(\dfrac{1-\sqrt{5}}{2}\right)^{n-2}\left(1+\dfrac{1-\sqrt{5}}{2}\right)]{\ensuremath{\\\\=\dfrac{\left(\dfrac{1+\sqrt{5}}{2}\right) ^{n-2}#1}{\sqrt{5}}}}
\newcommand{\f}{\ensuremath{\left(\dfrac{1-\sqrt{5}}{2}\right)^{n-2}}}
\newcommand{\g}[1][n-1]{\ensuremath{\left(\dfrac{1-\sqrt{5}}{2}\right)^{#1}}}
\newcommand{\h}[1][n-1]{\ensuremath{\left(\dfrac{1+\sqrt{5}}{2}\right)^{#1}}}
\newcommand{\n}{\ensuremath{\left(\dfrac{1+\sqrt{5}}{2}\right) ^{n-2}}}



$a_n=\x+\x[2]\e[-\f+\h\g]\e\e[\h[2]]$



\end{document}

답변1

사용

\e[{\h[n-1]}] 

내부를 \h[..]중괄호에 넣습니다.

답변2

를 통해 선택적 인수를 사용하여 명령을 정의하면 xparse중첩할 수 있습니다.

\documentclass{article}
\usepackage{amsmath}
\usepackage{xparse}

\newcommand{\grplus}{\dfrac{1+\sqrt{5}}{2}}
\newcommand{\grminus}{\dfrac{1-\sqrt{5}}{2}}

\newcommand{\eopt}{%
  \left(1+\grplus\right)-\f\left(1+\grminus\right)%
}

\NewDocumentCommand{\x}{O{1}}{a_{n-#1}}
\NewDocumentCommand{\e}{O{\eopt}}{\\&=\dfrac{\n #1}{\sqrt{5}}}

\NewDocumentCommand{\g}{O{n-1}}{\left(\grminus\right)^{#1}}
\NewDocumentCommand{\h}{O{n-1}}{\left(\grplus\right)^{#1}}

\newcommand{\n}{\g[n-2]}
\newcommand{\f}{\h[n-2]}

\begin{document}

$\begin{aligned}[t]
a_n&=\x+\x[2]\e[-\f+\h\g]\e\e[\h[2]]
\end{aligned}$

나는 또한 aligned더 나은 정렬을 얻기 위해 사용했습니다.

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

쓸모없는 코드로 당신을 괴롭힐 이유가 없습니다 \ensuremath.

관련 정보