0

I want to declare a list with enumerate using the enumitem package, in which some of the items may have custom labels. However, the problem is that if a label is very long (i.e. longer than \labelwidth), the label overflows.

For instance, the following code:

\documentclass{article}

\begin{document}

\noindent
Text preceding the list.
\begin{enumerate}
    \item Item 1.
    \item[A very long label.] Item 2.
    \item Item 3.
\end{enumerate}
Text following the list.

\end{document}

produces the following list: A list with overflowing labels.

I would like there to always be a minimum distance between the left margin of the preceding text, and the left margin of each item label. In other words, I want the previous list to display like the following: A list with correctly indented labels. (The right margin should not be modified.)

This question is similar, but that question only concerns lists with default label numbering, not custom labels (the accepted answer does not work in this case).

I do not want to have to manually specify the longest label: it should be calculated automatically. (Note that, depending on the list in question, the longest label may be an automatically numbered label such as 1000., or a custom label.) Ideally, it would be possible to define a new environment replacing enumerate, or define a new option to pass to enumerate that has this modified behaviour.

(In a previous iteration of this question, I added my attempt at defining such an environment, but it did not work at all, and made the question much longer, so I have removed it.)

2
  • What you want is not entirely clear. Is the first image what you want to obtain automatically?
    – Bernard
    Apr 23, 2021 at 14:06
  • @Bernard: I've clarified my question.
    – varkor
    Apr 23, 2021 at 14:41

1 Answer 1

1

Here is a solution with enumitem:

\documentclass{article}
\usepackage[showframe]{geometry} 
\usepackage{enumitem}
\newlength{\lblwd}
\settowidth{\lblwd}{Very wide label}

\begin{document}

\begin{enumerate}[label=\arabic*., labelwidth=\lblwd, leftmargin=!]
  \item An item
  \item[Very wide label] An item
  \item An item \addtocounter{enumi}{9997}
  \item\label{last-item} An item
\end{enumerate}

\end{document} 

enter image description here

6
  • This has exactly the same problems as explicitly tagging the longest label with a label. I said I was looking for a way to do this automatically.
    – varkor
    Apr 23, 2021 at 15:46
  • You have no idea what is the longest label is?
    – Bernard
    Apr 23, 2021 at 15:48
  • @varkor: the problem for an automatic way is the optional argument of the second item, which furthermore is not numeric. For items without this optional argument, we can have an automatic way.
    – Bernard
    Apr 23, 2021 at 16:09
  • I already stated in my question that which label is widest may be subtle. I also have many lists, and I don't want to waste time annotating them manually. I am well aware that the optional argument makes it more difficult: that's the whole point of the question.
    – varkor
    Apr 23, 2021 at 16:50
  • Should all your lists have the same label width?
    – Bernard
    Apr 23, 2021 at 17:15

You must log in to answer this question.

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