我想知道我是否可以定义类似于将\newcommand
值放置在预定义表(tabular
)模板中的东西。
如果您看到上面的表格,想象一下您将重复多次(例如,通过使用新定义的命令\workpackage
)。该命令的输入参数将是:
\workpackage {2}{
\begin{itemize}......
}{none}
布局本身就是一个表格。将如此复杂的布局放入新命令中作为模板是一个好主意吗?
这个例子不起作用:(
\newcommand{\wp}[3]{
\begin{tabular}{ l l }
Effort & #1 \\
Content & #2 \\
Pre-Requisites & #3 \\
\end{tabular}
}
这是我的最终解决方案,它受到以下答案的启发:
\usepackage{tabularx}
\usepackage{xkeyval}
% define the key (arguments)
\makeatletter
\define@key{wpkeys}{title}{%
\def\wptitle{#1}
}
\define@key{wpkeys}{number}{%
\def\wpnumber{#1}
}
\define@key{wpkeys}{weeks}{%
\def\wpweeks{#1 person weeks}
}
\define@key{wpkeys}{results}{%
\def\wpresults{#1}
}
\makeatother
% end of key definition
% new command for a work package
\newcommand{\workpackage}[2][]{%
\setkeys{wpkeys}{#1}%
\subsection*{WP\wpnumber: \wptitle}
\begin{tabularx}{\linewidth}{|p{3cm}|X|}
\hline
Effort & \wpweeks \tabularnewline
\hline
Content & #2 \tabularnewline
\hline
Expected results & \wpresults \tabularnewline
\hline
\end{tabularx}
}
% end of command definition
使用方式如下:
\workpackage[number=1, title={Project Management \& Reporting}, weeks=3,results={MS1}]{
\begin{itemize}
\item foo
\item baa
\end{itemize}
}
答案1
这是一个keyvalue
界面方法。
为密钥系列定义“任意”密钥,例如,wpkeys
将\define@key
分配key-value
给与密钥名称相似的宏(更容易记住)。这些密钥可以作为\workpackage
then 的可选参数提供。
例如,实际内容作为第一个强制参数给出。
和
\setkeys{wpkeys}{prerequisites=none,leftheader={Effort},#1}%
如果没有明确给出,则可以预设一些键和一些值。
请注意,由于我的示例很短,这将使\leftheader
等的内容在下次使用\workpackage
宏时仍然有效。
\documentclass{article}
\usepackage{tabularx}
\usepackage{xkeyval}
\makeatletter
\define@key{wpkeys}{leftheader}{%
\def\leftheader{#1}
}
\define@key{wpkeys}{rightheader}{%
\def\rightheader{#1}
}
\define@key{wpkeys}{prerequisites}{%
\def\prerequisites{#1}
}
\makeatother
\newcommand{\workpackage}[2][]{%
\setkeys{wpkeys}{prerequisites=none,leftheader={Effort},#1}%
\begin{tabularx}{\linewidth}{|p{3cm}|X|}
\hline
\leftheader & \rightheader \tabularnewline
\hline
Content & #2 \tabularnewline
\hline
Pre-requisites & \prerequisites \tabularnewline
\hline
\end{tabularx}
}
\begin{document}
\workpackage[rightheader={2 person weeks}]{\begin{itemize} \item foo \item foobar \end{itemize}}
\workpackage[rightheader={5 person weeks},prerequisites={a lot}]{\begin{itemize} \item foo \item foobar \end{itemize}}
\end{document}
我把桌子设计留给了 OP
更新更好一点/更容易使用:
\documentclass{article}
\usepackage[x11names]{xcolor}
\usepackage{tabularx}
\usepackage{xkeyval}
\makeatletter
\define@key{wpkeys}{leftheader}{%
\def\leftheader{#1}
}
\define@key{wpkeys}{rightheader}{%
\def\rightheader{#1}
}
\define@key{wpkeys}{prerequisites}{%
\def\prerequisites{#1}
}
\define@key{wpkeys}{contentname}{%
\def\contentcolumnheadername{#1}
}
\define@key{wpkeys}{prerequisitesname}{%
\def\prerequisitescolumnheadername{#1}
}
\define@key{wpkeys}{contentheaderstyle}{%
\def\contentheaderstyle{#1}%
}
\define@key{wpkeys}{prerequisitesheaderstyle}{%
\def\prerequisitesheaderstyle{#1}%
}
\define@key{wpkeys}{leftheaderstyle}{%
\def\leftheaderstyle{#1}%
}
\define@key{wpkeys}{rightheaderstyle}{%
\def\rightheaderstyle{#1}%
}
% Tabular lengths etc.
\newlength\wp@leftcolumnwidth
\setlength{\wp@leftcolumnwidth}{3cm} % A default value
\define@key{wpkeys}{leftcolumnwidth}{%
\setlength{\wp@leftcolumnwidth}{#1}%
}
\define@key{wpkeys}{arraystretch}{%
\renewcommand{\arraystretch}{#1}%
}
\presetkeys{wpkeys}{%
arraystretch=1.5,
contentheaderstyle={\textbf},
prerequisitesheaderstyle={\textit},
rightheaderstyle={\large\bfseries\textcolor{blue}},
leftheaderstyle={\large\bfseries\textcolor{red}},
}{}
\newcommand{\workpackage}[2][]{%
\begingroup
\setkeys{wpkeys}{contentname={Content},prerequisitesname={Pre - Requisites},prerequisites=none,leftheader={Effort},#1}%
\begin{tabularx}{\linewidth}{|p{\wp@leftcolumnwidth}|X|}
\hline
\leftheaderstyle{\leftheader} & \rightheaderstyle{\rightheader} \tabularnewline
\hline
\contentheaderstyle{\contentcolumnheadername} & #2 \tabularnewline
\hline
\prerequisitesheaderstyle{\prerequisitescolumnheadername} & \topsep=0pt\prerequisites \tabularnewline
\hline
\end{tabularx}
\endgroup
}
\makeatother
\begin{document}
\workpackage[rightheader={2 person weeks}]{\begin{itemize} \item foo \item foobar \end{itemize}}
\workpackage[rightheader={5 person weeks},prerequisites={\begin{itemize} \item A nice list \item that ends \item after three items\end{itemize}}]{\begin{itemize} \item foo \item foobar \end{itemize}}
\end{document}