Using more \newcommand's into a single \newenvironment and \newenvironment instances counting


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}

-

migrated from stackoverflow.comJul 9 '13 at 11:11

This question came from our site for professional and enthusiast programmers.

Could you mock up what you expect the output should look like (it doesn't have to be in LaTeX)? –  Werner Jul 8 '13 at 22:18
Welcome to TeX.SX! Your post was migrated here from another Stack Exchange site. Please register on this site, too, and make sure that both accounts are associated with each other (by using the same OpenID), otherwise you won't be able to comment on or accept answers or edit your question. –  egreg Jul 9 '13 at 11:17


-

It is not entirely clear what you are after but here is a set-up for your code that will compile and produce some output.

Main file:

\documentclass{myclass}

% Main document

\begin{document}

\tracingmacros=1

\CUP{Champions}

\begin{team}{a}{b}{c}
\playerone{John}

\playertwo{Carl}

\playerthree{Smith}
\end{team}

\begin{team}{p}{q}{r}
\playerone{Scott}

\playertwo{Luke}

\playerthree{Danny}
\end{team}

\end{document}


myclass.cls:

\LoadClassWithOptions{article}

\let\@CUP\relax
\def\CUP#1{\def\@CUP{Tournament #1}}

\newenvironment{team}[3]{
\begin{center}
\@CUP
\end{center}
Argument 1 is #1; argument 2 is #2; argument 3 is #3 \par
\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}
}{}


The class file needs to set-up fonts etc. The easiest way to do this is to build on an existing class file, in this case article. See clsguide for information on writing classes and in particular loading classes and packages in them.

Now your environment team requires three arguments. Your code in the main document didn't provide any. I have called them a, b and c in the first call. These can be referenced as #1, #2 and #3 in the environment.

As Ulrich Schwarz says, if you wish to define a command inside the environment, then its arguments will be referenced as ##1 etc. Often it is significantly simpler to have these as global commands. Latex, and also the etoolbox package, provides various test commands you use within them if you wish limit their use/provide appropriate error or warning messages. Look at the source of standard classes to get some inspiration.

-