Por que isso não compila? O argumento de \h tem um { extra

Por que isso não compila? O argumento de \h tem um { extra
\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}

Responder1

usar

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

Coloque o interior \h[..]entre colchetes

Responder2

Se você definir comandos com argumento opcional por meio de xparse, poderá aninhá-los.

\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}$

Também usei alignedpara conseguir um melhor alinhamento.

insira a descrição da imagem aqui

Não há razão para atormentar seu código com arquivos \ensuremath.

informação relacionada