10

I'm using the enumitem package to define a legal list:

\usepackage{enumitem}
\newlist{legal}{enumerate}{10}
\setlist[legal]{label*=\arabic*.}

The output for a list looks like this:

1. foo
  1.1 bar
    1.1.1 bla
  1.2 baz
    1.2.1 blubb
2. fuuu

But that gets somewhat clobbered if you have many items and deep nesting. So I want all the labels to align on the leftmost side but keep the indentation for the items themselves like so:

1. foo
1.1    bar
1.1.1      bla
1.2    baz
1.2.1      blubb
2. fuuu

I guess this would make searching for an entry in that list easier. Any ideas on how to achieve that?

I have already tried the align option of the enumitem package but that didn't work as intended.

3 Answers 3

6

Here's a solution without any packages and guaranteeing that indentation of the item text will be preserved for every value of the counters < 100 (the other solution exhibits a bad behaviour if any of the counters surpasses 9).

EDIT: this new version allows nesting up to six levels and corrects a little spacing issue of the first version:

\documentclass{article}
\usepackage{lipsum}

\newlength\labwd
\makeatletter
\newcommand\labelenumv{}
\newcommand\labelenumvi{}
\newcounter{enumv}[enumiv]
\newcounter{enumvi}[enumv]
\newenvironment{myenum}
    {\advance\@enumdepth\@ne
    \ifnum \@enumdepth >6\@toodeep\else
    \edef\@enumctr{enum\romannumeral\the\@enumdepth}
    \renewcommand\labelenumi{\arabic{enumi}.}
    \renewcommand\labelenumii{\labelenumi\arabic{enumii}.}
    \renewcommand\labelenumiii{\labelenumii\arabic{enumiii}.}
    \renewcommand\labelenumiv{\labelenumiii\arabic{enumiv}.}
    \renewcommand\labelenumv{\labelenumiv\arabic{enumv}.}
    \renewcommand\labelenumvi{\labelenumv\arabic{enumvi}.}
    \renewcommand\theenumii{\arabic{enumii}}
    \renewcommand\theenumiii{\arabic{enumiii}}
    \renewcommand\theenumiv{\arabic{enumiv}}
    \renewcommand\p@enumii{\theenumi.}
    \renewcommand\p@enumiii{\theenumi.\theenumii.}
    \renewcommand\p@enumiv{\theenumi.\theenumii.\theenumiii.}
    \renewcommand\p@enumv{\theenumi.\theenumii.\theenumiii.\theenumiv.}
    \renewcommand\p@enumvi{\theenumi.\theenumii.\theenumiii.\theenumiv.\theenumv.}
    \begin{list}{\csname label\@enumctr\endcsname}{%
        \usecounter\@enumctr
        \setlength\labelwidth{0pt}
        \setlength\labelsep{0pt}
        \setlength\leftmargin{15pt}
        \setlength\labwd{\ifcase\@enumdepth\or -15pt\or -30pt\or -45pt\or -60pt\or -75pt\or -90pt\fi}%
        \renewcommand\makelabel[1]{\hspace*{\labwd}##1\hfill}%
    }%
    \fi%
    }
    {\ifnum \@enumdepth >6\else\end{list}\fi}
\makeatother


\begin{document}

\lipsum[2]
\begin{myenum}\ignorespaces
  \item\label{a} First item.
  \begin{myenum}\ignorespaces
    \item\label{b} First subitem.
    \begin{myenum}\ignorespaces
      \item\label{c} First subsubitem.
      \item Second subsubitem.
      \begin{myenum}
        \item\label{d} First subsubsubitem.
        \item Second subsubsubitem.
        \begin{myenum}
          \item\label{e} First subsubsubsubitem.
          \item \lipsum[1]
        \end{myenum}
      \end{myenum}
      \item Third subsubitem.
    \end{myenum}
  \item Second subitem.
  \end{myenum}
  \item Second item.
\end{myenum}

\begin{myenum}
  \setcounter{enumi}{8}
  \item First item.
  \begin{myenum}
    \setcounter{enumii}{8}
    \item First subitem.
    \begin{myenum}
      \setcounter{enumiii}{8}
      \item First subsubitem.
      \item Second subsubitem.
      \begin{myenum}
        \setcounter{enumiv}{8}
        \item First subsubsubitem.
        \item Second subsubsubitem. 
      \end{myenum}
      \item Third subsubitem.
    \end{myenum}
  \item Second subitem.
  \end{myenum}
  \item Second item.
\end{myenum}

\end{document}

enter image description here

4
  • This also has slight problems with the indent for multiline items (first line is indented one character too far). Also I slightly modified it to handle deeper lists and proper error messages on \@toodeep. Edit: seems like I can not post it as comment, will do new answer.
    – NEOatNHNG
    Apr 17, 2012 at 14:08
  • @NEOatNHNG: I've updated my answer. Apr 17, 2012 at 18:02
  • Hmm, another issue with this solution: when using labels and references a reference to an item shows like 4(a)ii. To solve this add the following lines after each \renewcommand\labelenumXX{\labelenumX\arabic{enumXX}.} definition: \renewcommand\theenumXX{labelenumXX} and \renewcommand{\p@enumXX}{}
    – NEOatNHNG
    Apr 18, 2012 at 14:29
  • @NEOatNHNG: I've added the necessary re-definitions to my code; I suppressed the final dot from the string used in the cross references. Apr 18, 2012 at 17:21
4

You could use leftmargin=0pt, labelwidth=! which means enumitem will calculate the with of the label to suit a left margin of 0pt. Combining with align=left gives the desired result.

\setlist[legal]{label*=\arabic*., leftmargin=0pt, labelsep=1.5em, align=left, labelwidth=!}

Setting labelsep is not strictly required but I find that 1.5em is more appropriate than the default value.

4
  • 2
    Unfortunately, when some counter gets to be greater than 9, the alignment will be wrong. Apr 11, 2012 at 22:38
  • Right you are, but I can't seem to get around this.
    – spet
    Apr 12, 2012 at 9:06
  • At first it would not accept labelwidth=! because apparently Ubuntu ships a way old texlive version (2009[sic]) which still has version 2.2 of enumitem. So I did manually install an up-to-date texlive. Now it works just fine. Thank you.
    – NEOatNHNG
    Apr 12, 2012 at 16:15
  • This solution makes problems when the items are more than one line. Then the items are wrapped under the label.
    – NEOatNHNG
    Apr 17, 2012 at 12:56
0

Here I tried to modify the code by @GonzaloMedina to allow deeper nesting and resolve a problem with the \@toodeep error handling (not added as comment because it's too long).

\documentclass{article}
\usepackage{lipsum}

\newlength\labwd
\makeatletter
\newcommand\labelenumv{}
\newcommand\labelenumvi{}
\newcounter{enumv}[enumiv]
\newcounter{enumvi}[enumv]
\newenvironment{myenum}
    {\advance\@enumdepth\@ne
    \ifnum \@enumdepth >6\@toodeep\else
    \edef\@enumctr{enum\romannumeral\the\@enumdepth}
    \renewcommand\labelenumi{\arabic{enumi}.}
    \renewcommand\labelenumii{\labelenumi\arabic{enumii}.}
    \renewcommand\labelenumiii{\labelenumii\arabic{enumiii}.}
    \renewcommand\labelenumiv{\labelenumiii\arabic{enumiv}.}
    \renewcommand\labelenumv{\labelenumiv\arabic{enumv}.}
    \renewcommand\labelenumvi{\labelenumv\arabic{enumvi}.}
    \begin{list}{\csname label\@enumctr\endcsname}{%
        \usecounter\@enumctr
        \setlength\labelwidth{5pt}
        \setlength\labelsep{\z@}
        \setlength\leftmargin{15pt}
        \setlength\itemindent{5pt}
        \setlength\labwd{\ifcase \@enumdepth \or -15pt\or -30pt\or -45pt\or -60pt\or -75pt\or -90pt\fi}
        \renewcommand\makelabel[1]{\hspace*{\labwd}##1\hfill}
    }%
    \fi
    }
    {\ifnum \@enumdepth >6\else\end{list}\fi}
\makeatother


\begin{document}

\lipsum[2]
\begin{myenum}
  \item First item.
  \begin{myenum}
    \item First subitem.
    \begin{myenum}
      \item First subsubitem.
      \item Second subsubitem.
      \begin{myenum}
        \item First subsubsubitem.
        \item Second subsubsubitem.
        \begin{myenum}
          \item First subsubsubsubitem.
          \item \lipsum[1]
        \end{myenum}
      \end{myenum}
      \item Third subsubitem.
    \end{myenum}
  \item Second subitem.
  \end{myenum}
  \item Second item.
\end{myenum}

\begin{myenum}
  \setcounter{enumi}{8}
  \item First item.
  \begin{myenum}
    \setcounter{enumii}{8}
    \item First subitem.
    \begin{myenum}
      \setcounter{enumiii}{8}
      \item First subsubitem.
      \item Second subsubitem.
      \begin{myenum}
        \setcounter{enumiv}{8}
        \item First subsubsubitem.
        \item Second subsubsubitem. 
      \end{myenum}
      \item Third subsubitem.
    \end{myenum}
  \item Second subitem.
  \end{myenum}
  \item Second item.
\end{myenum}

\end{document}

One open issue is the handling indents in multiline-items — currently the indent of the first line of such an item is about one character too deep — and probably there is a nicer way to write the hacks around it (I'm a LaTeX programming n00b).

Suggestions are welcome.

2
  • @PeterGrill: This is an answer to my original question. I would rather have commented on the answer provided by @GonzaloMedina but that is not possible because one can not post long text in the comments and just describing the parts I've modified to improve the answer would have been confusing. The nesting issue can not be solved using enumitem as suggested in the question you've linked if I can't get enumitem to do the indentation as I have described in the question. I have modified the solution to make it a MWE
    – NEOatNHNG
    Apr 17, 2012 at 15:40
  • Please see my updated answer for a fix. Apr 17, 2012 at 18:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .