The problem is that when TeX reads the environment command it detects where all the #1
are, but the \input
is not processed. When the environment is executed (that is, the underlying \myenvir
command is expanded), all the #1
seen previously are replaced by the argument Hello
. So using
\begin{myenvir}{Hello}
\mycommand{World}
\end{myenvir}
is the same as (minus the comments)
% \begin{myenvir}{Hello}
\input{fcommand}
\begin{center}
% environment contents
\mycommand{World}
% \end{myenvir}
\end{center}
The Hello
was lost because it wasn't used anywhere. Now the \input
is expanded:
% \begin{myenvir}{Hello}
\newcommand{\mycommand}[1]
{ test:command arg=##1 environment arg=#1}
\begin{center}
% environment contents
\mycommand{World}
% \end{myenvir}
\end{center}
Then, after the definition is done, the expansion of \mycommand
yields:
% \begin{myenvir}{Hello}
% \newcommand{\mycommand}[1] %%% Already defined
% { test:command arg=##1 environment arg=#1}
\begin{center}
% environment contents
test:command arg=#1 environment arg=World
% \end{myenvir}
\end{center}
Ans now you're trying to write #1
in the middle of the text, which raises the error:
! You can't use `macro parameter character #' in horizontal mode.
l.40 test:command arg=#
1 environment arg=World