3

I want to define different requirements in my thesis, some of which can be divided into smaller requirements. Building on the answer to my previous question Referencing enumeration items by number and name, I came up with the following:

\documentclass{article}

\usepackage{lipsum}
\usepackage{enumitem}
\usepackage{etoolbox}

\usepackage[showframe,pass]{geometry}

\begin{document}

\makeatletter
\newcommand*{\req}[1]{%
  \item %
  \let\originall@bel\@currentlabel%
  \appto\@currentlabel{ (#1)}%
  \emph{#1}\quad}
\newenvironment{subreqs}{\begin{enumerate}[label=\originall@bel.\arabic*,widest=9,leftmargin=0pt]}{\end{enumerate}}
\makeatother

\begin{enumerate}[label=R~\arabic*,widest*=20,leftmargin=*,series=reqs]
\req{First Important Thing} \label{req1} \lipsum[4]
\begin{subreqs}
\req{Important Subitem} \label{req2} \lipsum[2]
\end{subreqs}
\end{enumerate}

\ref{req1} and \ref{req2} are important.

\end{document}

This partly works as expected, the only problem is that the second requirement is indented with regard to the first one. This is of course expected since I use a second level of enumerate, but I'd still like to avoid it since the grouping is visible from the number itself.

enter image description here

Edit: I just realized that leftmargin (which I suspected to do something different) controls exactly what I want. However, using leftmargin=0pt instead of leftmargin=* in the definition of subreqs, as Thruston suggests does not really help, as it makes R 1.1 protrude out of the text frame (see the updated code/screenshot above).

  • 1
    Try changing your subreqs environment to define leftmargin=0pt. Does this do what you want? – Thruston Nov 13 '13 at 12:10
  • @Thruston: thanks for pointing this out, I thought that leftmargin only controlled the indentation of the wrapped lines following the item. However, setting it to 0pt does not really solve the problem either (see the updated question). – rainer Nov 13 '13 at 12:21
4

You should set leftmargin=0pt for the inner list. I suggest defining your own environment also for the outer list. There is no need for \originall@bel, but an \ignorespaces is needed.

\documentclass{article}

\usepackage{lipsum}
\usepackage{enumitem}
\usepackage{etoolbox}

\newlist{reqs}{enumerate}{1}
\newlist{subreqs}{enumerate}{1}
\setlist[reqs]{label=R~\arabic*,widest*=100,leftmargin=*}
\setlist[subreqs]{label=\thereqsi.\arabic*,leftmargin=0pt}

\makeatletter
\newcommand*{\req}[1]{%
  \item 
  \appto\@currentlabel{ (#1)}%
  \emph{#1}\quad\ignorespaces}% <--- note \ignorespaces
\makeatother

\begin{document}

\lipsum[3]
\begin{reqs}
\req{First Important Thing} \label{req1} \lipsum[4]
\begin{subreqs}
\req{Important Subitem} \label{req2} \lipsum[2]
\end{subreqs}
\end{reqs}
\lipsum[3]

\ref{req1} and \ref{req2} are important.

\end{document}

enter image description here

If you want left alignment of the labels, you can pass the label width of the outer environment to the inner one:

\documentclass{article}

\usepackage{lipsum}
\usepackage{enumitem}
\usepackage{etoolbox}

\newlist{reqs}{enumerate}{1}
\newlist{subreqs}{enumerate}{1}
\setlist[reqs]{
  label=R~\arabic*,
  widest*=100,
  leftmargin=*,
  align=left,
  before=\edef\outerlabelwidth{\the\labelwidth}
}
\setlist[subreqs]{
  label=\thereqsi.\arabic*,
  leftmargin=0pt,
  labelindent=0pt,
  labelwidth=\outerlabelwidth,
  align=left,
}

\makeatletter
\newcommand*{\req}[1]{%
  \item %
  \appto\@currentlabel{ (#1)}%
  \emph{#1}\quad\ignorespaces}
\makeatother

\begin{document}

\lipsum[3]
\begin{reqs}
\req{First Important Thing} \label{req1} \lipsum[4]
\begin{subreqs}
\req{Important Subitem} \label{req2} \lipsum[2]
\end{subreqs}
\end{reqs}
\lipsum[3]

\ref{req1} and \ref{req2} are important.

\end{document}

enter image description here

  • Thanks, this is nearly where I wanted it... I realize I'm nit-picking now, but is there any way to left-align the labels R 1 and R 1.1? I just played around with align=left, which seems to work for the first level of indentation, but moves the label of R 1.1 to the right if I add it in \setlist[subreqs]{...}. – rainer Nov 13 '13 at 12:52
  • @rainer Left alignment added – egreg Nov 13 '13 at 13:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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