Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question

migrated from stackoverflow.com Jul 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
1  
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

2 Answers 2

up vote 4 down vote accepted

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 ;)

share|improve this answer

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.

Sample 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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.