Skip to content

Commit

Permalink
Added an autowidest key
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaacOscar committed Nov 8, 2023
1 parent 1bdcad0 commit bb71bb5
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 16 deletions.
Binary file modified enumitem.pdf
Binary file not shown.
30 changes: 30 additions & 0 deletions enumitem.sty
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{enumitem}[2019/06/20 v3.9 Customized lists]

\RequirePackage{eqparbox}% Needed by autowidest

% Notes
% ~~~~~
%
Expand Down Expand Up @@ -145,6 +147,7 @@
\newif\ifenit@sepfrommargin
\newif\ifenit@lblfrommargin
\newif\ifenit@calcwidest
\newif\ifenit@autowidest
\newif\ifenit@nextline
\newif\ifenit@boxdesc

Expand Down Expand Up @@ -277,6 +280,7 @@
\fi\fi}

\def\enitkv@enumitem@widest#1{%
\enit@autowidestfalse% Manually using widest overrides autowidest
\ifcase\enit@type % enumerate
\expandafter\let\csname enit@cw@\@enumctr\endcsname\relax
\@namedef{enit@widest@\@enumctr}##1{\enit@format{#1}}%
Expand All @@ -289,12 +293,34 @@
\expandafter\let\csname enit@widest@\@enumctr\endcsname\relax}

\enitkv@key{}{widest*}{%
\enit@autowidestfalse% Manually using widest* overrides autowidest
\setcounter{enit@cnt}{#1}%
\expandafter\edef\csname enit@cw@\@enumctr\endcsname
{\the\c@enit@cnt}%
\expandafter\edef\csname enit@widest@\@enumctr\endcsname##1%
{##1{\the\c@enit@cnt}}}

\enitkv@key{}{autowidest}[]{%
% \eqboxwidth gets the widest width of the calls to \eqp@compute@width with the same argument
\enitkv@setkeys{widest}{\hbox to \eqboxwidth{enit@autowidest@\EnumitemId}{}}
\enit@autowidesttrue}
% \enit@autowidest needs to be called after \makelabel is set up properly
% (thus it is not called as part of \enit@calcleft since that gets called in the wrong place for descriptions)
\newcommand\enit@autowidest{%
\ifenit@autowidest\ifenit@calcwidest% autowidest only makes sense if used with an =* option
\let\enit@makelabel=\makelabel% Save the original \makelabel
% Override \makelabel so that it also keeps track of label widths
\def\makelabel##1{
\savebox\enit@inbox{\enit@makelabel{#1}}% Stop \eqp@compute@width from typseting this twice
% \eqp@compute@width will compute the widest width by typesting \usebox\enit@inbox
% and then returns \eqp@produce@box{<widest width>}{\usebox\enit@inbox}
% \eqp@produce@box is globally defined by each of eqparbox's various \eq commands so it's safe to modify
% It's modified below to simply ignored the <widest width>, as this will be handled by the
% widest key itself
\global\let\eqp@produce@box=\@secondoftwo%
\eqp@compute@width{enit@autowidest@\EnumitemId}{\usebox\enit@inbox}}
\fi\fi}

\enitkv@key{}{labelindent*}{%
\enit@lblfrommargintrue
\ifnum\enit@calc=\z@
Expand Down Expand Up @@ -936,6 +962,7 @@
\enit@tagid
\enit@sepfrommarginfalse
\enit@calcwidestfalse
\enit@autowidestfalse
\let\enit@widest@\relax
\let\enit@resuming\z@
\let\enit@series\relax
Expand Down Expand Up @@ -1376,6 +1403,7 @@
\enit@normlabel\@itemlabel\@itemlabel
\enit@ref
\enit@calcleft
\enit@autowidest
\enit@before
\enit@negwidth}%
\enit@keyfirst
Expand Down Expand Up @@ -1413,6 +1441,7 @@
\def\makelabel##1{\enit@align{\enit@format{##1}}}%
\enit@preset{#2}{#1}{#4}%
\enit@calcleft
\enit@autowidest
\enit@before
\enit@negwidth}%
\enit@keyfirst
Expand Down Expand Up @@ -1461,6 +1490,7 @@
\enit@svlabel{\enit@format{##1}}%
\aftergroup\enit@postlabel
\fi}%
\enit@autowidest% This needs to go after the \makelabel definition above
\enit@before
\enit@negwidth}%
\enit@keyfirst
Expand Down
22 changes: 6 additions & 16 deletions enumitem.tex
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
\definecolor{notes}{rgb}{.75, .3, .3}%

\makeatletter
\expandafter\def\csname [email protected]\endcsname{} % Because it's loaded by eqparbox but breaks the decl environment
\newenvironment{desc}
{\if@nobreak
\vskip-\lastskip
Expand Down Expand Up @@ -573,7 +574,7 @@ \subsection{Horizontal spacing of labels}
\end{note}

\begin{desc}
|widest=<string>|\qquad|widest*=<integer>|\qquad|widest|
|widest=<string>|\qquad|widest*=<integer>|\qquad|widest|\qquad|autowidest|
\end{desc}

To be used in conjunction with the \texttt{*}-values, if desired. It
Expand All @@ -597,6 +598,9 @@ \subsection{Horizontal spacing of labels}
you might need change the computed parameter (eg, |itemindent=*| with
|align=left|).

\New{3.X} You can use |autowidest| to have \textsf{enumitem}
work out the actual widest label for you, however it requires at least two runs of \LaTeX.

\begin{desc}
|labelsep*=<length>|
\end{desc}
Expand Down Expand Up @@ -863,21 +867,7 @@ \subsection{Injecting code}
assigned to each list, returned by |\EnumitemId|.

\begin{example}
Here is an example of how to combine a |\label| with |\EnumitemId|,
and |after| to automatically set the width of the list label to the
widest one (provided the ref is the same as the label):\footnote{See
\texttt{https://tex.stackexchange.com/questions/29322/%
how-to-make-enumerate-items-align-at-left-margin}.}
\begin{verbatim}
\SetEnumitemKey{widestlabel}
{labelwidth = \widthof{\ref{enum-\EnumitemId}},
after = \label{enum-\EnumitemId}}
\end{verbatim}
Then just use the key |widestlabel|.
\end{example}

\begin{example}
Reverse counting is also doable, but somewhat trickier, and we need
Here is an example of reverse counting, but it is somewhat tricky, and we need
some ``external'' help. Here is a possible solution, but not the
only one (and very likely not even the best -- for example, |start|
is in fact no-op).
Expand Down

0 comments on commit bb71bb5

Please sign in to comment.