4

This is a follow-up question

in which I asked how to define classes with \newcommands into \newenvironments, and I had some good answers to this particular issue. However, the problem that I have does not rely only on the issue described in the other thread, but it is more general. for instance, I wish to create a class to generate tables like the following

Example

The plain code that I use to create this table is the following

\documentclass[landscape, 12pt]{report}

\usepackage{graphicx}
\usepackage{enumerate}
\usepackage{enumitem}
\setlist{nolistsep}
\usepackage[top=0.1cm, bottom=0cm, left=0.1cm, right=-0cm]{geometry}
\usepackage[dvipsnames,usenames]{xcolor}

\begin{document}
\noindent


\colorbox{cyan!20}{
\begin{tabular}{|p{12.6cm}|}
    \hline
    Champions League \\
    - Team 1: The Hawks\\
    \hline\\
    FIRST-STRING PLAYERS:
    \begin{enumerate}
         \item John
         \item Carl
         \item Smith
     \end{enumerate} \
     RESERVE PLAYERS:
     \begin{enumerate}
         \item Anthony
         \item Luke
     \end{enumerate} \\
     \hline
     COACH: Robert\\\hline
\end{tabular}
}

\end{document}

And I am quite happy with that, but I wish also to create a class such, referring to the example in the picture:

  1. The "1" after "- Team" in the second line is an incresing number (so I guess I need a counter inside a \newenviroment definition);
  2. What is written with capital letters (e.g. FIRST-STRING PLAYERS, etc.) is fixed in the table;
  3. The tournament name (e.g. Champions League is a kind of "global" variable)

As an indicative example, here is a kind of main document I wish to use:

\documentclass{Teams}

\begin{document}

\CUP{Champions League}

\begin{team}
\TeamColor{Cyan!20}
\TeamName{The Hawks}
\FSPlayers{
    \begin{enumerate}
    \item John
    \item Carl
    \item Smith
\end{enumerate}
}
\RPlayers{
\begin{enumerate}
    \item Anthony
    \item Luke
\end{enumerate}
}   
\Coach{Robert}
\end{team}

I hope that my problem is clear. Unfortunately, I have never written any class before, so any help will be greatly appreciated.

Moriambar
  • 11,466
Barzi2001
  • 477

1 Answers1

7

There are many ways to do this, but here's one building on the tabular format that you created. There's no need to create a document class for this; you would want to make it a package instead, and use it with whatever document class you wanted.

Save the teams.sty document in your local texmf folder. It should go in <path-to-local-folder>/tex/latex/.

teams.sty

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{teams}[2013/08/16 Team formatting environment]
\RequirePackage{graphicx}
\RequirePackage[dvipsnames,usenames]{xcolor} % load xcolor before realboxes
\RequirePackage[xcolor]{realboxes} % to use boxes in environments
\RequirePackage{etoolbox} % for easy list processing
\RequirePackage{array} % for tables
\RequirePackage{enumitem}
% Create user commands for the fixed names in case you want to change them
% This is probably a bit of an overkill in this case, but this is how 
% you should do things to make them changeable when needed.
\newcommand{\leaguename}[1]{\gdef\teams@league{#1}}
\newcommand{\teamname}[1]{\gdef\teams@team{#1}}
\newcommand{\firststringname}[1]{\gdef\teams@first{#1}}
\newcommand{\reservesname}[1]{\gdef\teams@reserve{#1}}
\newcommand{\coachname}[1]{\gdef\teams@coach{#1}}

% Setup some defaults (formatting will be done by \namefont)
\firststringname{First string players:}
\reservesname{Reserve players:}
\coachname{Coach:}
\leaguename{Champions League}
\teamname{- Team:}

% Setup some formatting parameters
\newcommand{\namefont}{\MakeUppercase}
\newcommand{\teamboxcolor}{cyan!20}

% Two commands for the first string and reserves
\newcommand{\firststring}[1]{%
{\namefont\teams@first}
\begin{enumerate}
  \renewcommand*{\do}[1]{\item ##1}
  \docsvlist{#1}
\end{enumerate}\\}
\newcommand{\reserves}[1]{%
{\namefont\teams@reserve}
\begin{enumerate}
  \renewcommand*{\do}[1]{\item ##1}
  \docsvlist{#1}
\end{enumerate}\\}

% Command for coach (but will be provided as an argument of the environment)
\newcommand{\coach}[1]{\gdef\teams@coachprint{\namefont\teams@coach #1}}

% Set up a team counter
\newcounter{teamcount}
\renewcommand\theteamcount{\arabic{teamcount}}

% Make a column type for the table
\newcolumntype{T}{|p{12.6cm}|}

% Now create a team environment
% This takes two obligatory arguments, and one optional one
% \begin{team}[color]{<team name>}{<coach name>}
%
\newenvironment{team}[3][\teamboxcolor]
{\setlist{nolistsep}
 \coach{#3}
 \Colorbox{#1}
 \bgroup
 \begin{tabular}{T}
   \hline
    \teams@league\\
    \refstepcounter{teamcount}\teams@team\ \theteamcount: #2\\
    \hline\\
}
{  \hline
   \teams@coachprint\\
   \hline
 \end{tabular}
 \egroup
}
\endinput

Sample document

\documentclass{article}
\usepackage{teams}
\begin{document}
\noindent

\begin{team}{The Hawks}{Robert}
\firststring{John,Carl,Smith}
\reserves{Anthony,Luke}
\end{team}

\begin{team}{The Owls}{Bob}
\firststring{Fred, Joe, Dave}
\reserves{Roger, Harry}
\end{team}

\leaguename{National Hockey League}
\begin{team}[red!30]{The Red Wings}{Mike Babcock}
\firststring{Henrik Zetterberg, Pavel Datsyuk,Justin Abdelkader}
\reserves{Drew Miller, Joakim Andersson,Patrick Eaves}
\end{team}
\end{document}

Output

output of code

David Carlisle
  • 757,742
Alan Munn
  • 218,180
  • You suggests me to use a .sty, but I read in the class guide link that "If the commands could be used with any document class, then make them a package; and if not, then make them a class". That's why I was using a class, since it the output is a very particular document, and I don't think it will be used in any other document. Finally, I wonder how to find a guide to understand commands like \gdef\teams@coachprint. Are they TeX or LaTeX commands? – Barzi2001 Aug 29 '13 at 07:55
  • Creating your own class is more work, and for this application not necessary. Also, fundamentally the functionality here provides simply an environment to display a team; it's not the design of a whole document. As for the \gdef commands, yes, they are TeX not LaTeX; sorry about that. A good (free) guide to TeX can be found in TeX by Topic, which should be part of your distribution documentation. The proper LaTeX way would be the following: define each of the internal macros first with \newcommand, and then replace the \gdef command with \renewcommand. – Alan Munn Aug 29 '13 at 11:38
  • For example: \newcommand{\teams@coachprint}{} and then use \newcommand{\coach}[1]{\renewcommand{\teams@coachprint}{\namefont\teams@coach #1}} – Alan Munn Aug 29 '13 at 11:41
  • Great! And what about the "@" symbol. When it is used? Any guide? – Barzi2001 Sep 03 '13 at 07:30
  • The @ symbol is commonly used inside class and package files to create macro names that are protected from the end user, since @ is not permitted in user defined macro names. See What do \makeatletter and \makeatother do? – Alan Munn Sep 03 '13 at 13:13