The answer 969/29410 showed that it is possible to use optional arguments in environment definitions:
\documentclass{article}
\newenvironment{hello}[1][world]{\noindent Hello #1, }{Bye now!\\}
\begin{document}
\begin{hello}[Bob]\end{hello}
\end{document}
This works fine and produces
Hello Bob, Bye now!
Why is it that the optional argument cannot be used in the closing statement?
\documentclass{article}
\newenvironment{hello}[1][world]{\noindent Hello, }{Bye now, #1!\\}
\begin{document}
\begin{hello}[Bob]\end{hello}
\end{document}
line 2: Illegal parameter number in definition of \endhello. ...][world]{\noindent Hello, }{Bye now, #1!\}
line 4: You can't use `macro parameter character #' in horizontal mode. \begin{hello}[Bob]\end{hello}
line 4: Underfull \hbox (badness 10000) in paragraph
\end{part}of the environment. See this. You can, however, do\def\MyEnvOptArg{#1}in the\beginpart and use\MyEnvOptArginstead of#1in the\endpart. – Phelype Oleinik Feb 12 '19 at 11:37xparseorenviron… – TeXnician Feb 12 '19 at 12:00