[Tex/LaTex] Using more \newcommand’s into a single \newenvironment and \newenvironment instances counting

environments

I am trying to write a class (it is my first class) in LaTeX to manage some teams in a tournament, but I am incurring some problems related to \newenvironment and \newcommand. For instance, I wish to have some "global" variables that can be displayed at any point, and some "local" variables that can be viewed only within a certain environment. The first problem I have is that I don't know how to pass the number of arguments to the \newenvironment when multiple \newcommand are defined into it. I have tried to Google, but I haven't found any answer.

There are also some other problems that I think I will incur into later on. For instance, every time I instantiate such a new environment, I wish a counter that increases with the instances(perhaps I can do it easily with a \newcounter, but now I get stuck in the aforementioned point). Finally, it would be great if I can pass a color as an argument to the \newenvironment that I can use in a \colorbox{} command to display nice colors for each team. But those are problem that I may solve later on. Now, to be more clear, here is an attempt code that I wrote and that I cannot make it work:

% Copied from internet
\let\@CUP\relax
\def\CUP#1{\def\@CUP{Tournament #1}}
%


\newenvironment{team}[3]{
    \@CUP
    \newcommand{\playerone}[1]{This is the first player: #1}
    \newcommand{\playertwo}[1]{This is the second player: #1}
    \newcommand{\playerthree}[1]{This is the third player: #1}
}{}



% Main document

…
\begin{document}

CUP{Champions}

\begin{team}
        \playerone{John} \\
        \playertwo{Carl} \\
        \playerthree{Smith}
\end{team}

\begin{team}
        \playerone{Scott} \\
        \playertwo{Luke} \\
        \playerthree{Danny}
\end{team}

% Some counter goes somewhere.

\end{document}

Best Answer

The most important thing about nested definitions is to get the number of #s right: in your \newenvironment's first part, #1, #2, #3 refer to the three parameters you pass. Within the definition of the inner \newcommand, use ##1 to refer to the parameter in the command you're defining, and still use #1, #2, #3 to refer to the parameters to the environment. If you had a \newcommand inside your \newcommand, its arguments would be ####1, ####2, etc., and I'd advise you to change the structure to maintain your sanity ;)

Related Question