这是一个后续问题
我在其中询问了如何将带有 \newcommands 的类定义到 \newenvironments 中,并且我对这个特定问题有一些很好的答案。但是,我遇到的问题不仅仅依赖于另一个线程中描述的问题,而是更为普遍。例如,我希望创建一个类来生成如下所示的表
我用来创建此表的纯代码如下
\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}
我对此感到非常高兴,但我还希望创建一个这样的类,参考图中的示例:
- 第二行中“-Team”后面的“1”是一个递增的数字(所以我想我需要在 \newenviroment 定义中有一个计数器);
- 用大写字母写的内容(例如FIRST-STRING PLAYERS等)在表中是固定的;
- 锦标赛名称(例如冠军联赛是一种“全局”变量)
作为一个指示性示例,这里有一种我希望使用的主要文档:
\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}
我希望我的问题清楚。不幸的是,我以前从未写过任何课程,因此任何帮助都将不胜感激。
答案1
有很多方法可以做到这一点,但这里有一种方法是基于您创建的表格格式。无需为此创建文档类;您可以将其设为一个包,并将其与您想要的任何文档类一起使用。
将文档保存teams.sty
到本地texmf
文件夹中。它应该位于 中<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
示例文档
\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}