我正在尝试定义一个自定义usecase
环境,并能够在tabular
环境中以列表的形式排版路径。
\documentclass{scrartcl}
\usepackage{booktabs,multicol,enumitem}
\setlist{leftmargin=*,nosep}
\newcommand{\tabhead}[1]{\textbf{\sffamily#1}}
\newcounter{usecase}
\newenvironment{usecase}[1]{
\refstepcounter{usecase}
\begin{center}
\begin{tabular}[t]{lp{0.8\linewidth}}
\toprule
\tabhead{case~\theusecase} & \tabhead{#1}\\
\midrule
}
{\bottomrule\end{tabular}\end{center}}
\newcommand{\actor}[1]{actor & #1\\}
% This doesn't work.
\newenvironment{brokenmainpath}{
main path & \begin{minipage}{\linewidth}\begin{enumerate}}
{\end{enumerate}\end{minipage}\\}
% This does.
\newenvironment{mainpath}{
\begin{minipage}{\linewidth}\begin{enumerate}}
{\end{enumerate}\end{minipage}}
\begin{document}
% This works.
\begin{usecase}{important use case}
\actor{actor 1}
main path & \begin{mainpath}
\item step 1
\item step 2
\end{mainpath}\\
\end{usecase}
% This is what I would like to type to get output in the above image.
%\begin{usecase}{important use case}
% \actor{actor 1}
% \begin{mainpath}
% \item step 1
% \item step 2
% \end{mainpath}
%\end{usecase}
\end{document}
问题是,当我将 包含main path &
到环境定义中(如brokenmainpath
)时,它应该始终出现在任何用例的左列中,然后是右列中以枚举列表形式出现的路径步骤,我得到了一大堆错误(准确地说是 18 个)。它们都是这样的
额外},或忘记了\endgroup。\unskip \hfil } \hskip \tabcolsep \endtemplate l.36 \begin{brokenmainpath}
另外,“主路径”的顶部对齐似乎不起作用。任何帮助都将不胜感激。
答案1
您可以使用 Salim Bou 发布的链接中的答案,或者不使用环境而是使用命令mainpath
(但这种方式\verb
不起作用):
\documentclass{scrartcl}
\usepackage{booktabs,multicol,enumitem}
\setlist{leftmargin=*,nosep}
\newcommand{\tabhead}[1]{\textbf{\sffamily#1}}
\newcounter{usecase}
\newenvironment{usecase}[1]{
\refstepcounter{usecase}
\begin{center}
\begin{tabular}[t]{lp{0.8\linewidth}}
\toprule
\tabhead{case~\theusecase} & \tabhead{#1}\\
\midrule
}
{\\\bottomrule\end{tabular}\end{center}}
\newcommand{\actor}[1]{actor & #1\\}
\newcommand\mainpath[1]{%
main path &\begin{minipage}{\linewidth}\begin{enumerate}%
#1%
\end{enumerate}\end{minipage}%
}
\begin{document}
\begin{usecase}{important use case}
\actor{actor 1}
\mainpath{%
\item step 1
\item step 2
}
\end{usecase}
\end{document}
请注意,我已将您的usecase
环境更改为包含\\
之前的版本\bottomrule
。
编辑:对于顶部对齐使用\begin{minipage}[t]{<width>}
。下面是一个使用的示例environ
,因此是一个适合您的问题和顶部对齐的环境。
\documentclass{scrartcl}
\usepackage{booktabs,enumitem}
\usepackage{environ}
\setlist{leftmargin=*,nosep}
\newcommand{\tabhead}[1]{\textbf{\sffamily#1}}
\newcounter{usecase}
\newenvironment{usecase}[1]{
\refstepcounter{usecase}
\begin{center}
\begin{tabular}[t]{lp{0.8\linewidth}}
\toprule
\tabhead{case~\theusecase} & \tabhead{#1}\\
\midrule
}
{\\\bottomrule\end{tabular}\end{center}}
\newcommand{\actor}[1]{actor & #1\\}
%\newenvironment{mainpath}{\mainpth}{}
\NewEnviron{mainpath}{%
\xdef\EnvironmentContents{\unexpanded\expandafter{\BODY}}%
\gdef\TableRowContents{%
main path &%
\begin{minipage}[t]{\linewidth}\begin{enumerate}%
\EnvironmentContents%
\end{enumerate}\end{minipage}%
}%
\aftergroup\TableRowContents%
}
\begin{document}
\begin{usecase}{important use case}
\actor{actor 1}
\begin{mainpath}%
\item step 1
\item step 2
\end{mainpath}
\end{usecase}
\end{document}