jbezos / enumitem

Customize enumerate, itemize and description
MIT License
45 stars 5 forks source link

Nested list modified by parents #47

Closed tobiasBora closed 10 months ago

tobiasBora commented 10 months ago

If I have a nested list (enumitem-based) like:

\begin{itemize}
\item A
\item A
\item
  \begin{enumerate}[nosep,leftmargin=3cm,labelsep=0pt]
  \item B
  \item K
    \begin{itemize}
    \item Z
    \item P
    \end{itemize}
  \end{enumerate}
\end{itemize}

the nested list will take the value of the parent, giving weird results like:

enter image description here

(see how Z/P are close to the label)

Is it possible to "isolate" the setting to a single list? I guess I could manually write all parameters, in the definition of itemize, but I don't know what are the default values, nor what is the list of all values to set (and it seems a bit dirty).

MWE

\documentclass{article}
\usepackage{xcolor}
\usepackage{enumitem}
\newcommand*{\marvosymbol}[1]{{\fontfamily{mvs}\fontencoding{U}\fontseries{m}\fontshape{n}\selectfont\char#1}}
\setlist[itemize]{label={\strut\textcolor{blue!50!white}{\marvosymbol{123}}},leftmargin=6mm,}

\begin{document}

\begin{itemize}
\item A
\item A
\item
  \begin{enumerate}[nosep,leftmargin=3cm,labelsep=0pt]
  \item B
  \item K
    \begin{itemize}
    \item Z
    \item P
    \end{itemize}
  \end{enumerate}
\end{itemize}

\end{document}
muzimuzhi commented 10 months ago

\labelsep is initially .5em in standard document classes.

Example below uses

\expanded{\noexpand\setlist[itemize]{labelsep=\the\labelsep}}

to give labelsep a default value.

Note this is different from setting labelsep=.5em, since em is a relative unit, but here we just need the exact length of .5em at the time of loading document class.

Full example

```tex \documentclass{article} \usepackage{enumitem} \setlist{nosep} % for tighter output \setlist[itemize]{label={\texttt{+}}} % pass curent value of \labelsep to "labelsep" key \expanded{\noexpand\setlist[itemize]{labelsep=\the\labelsep}} \begin{document} \begin{itemize} \item A \item A \begin{enumerate} \item default \texttt{labelsep} \item K \begin{itemize} \item Z \item P \end{itemize} \end{enumerate} \item A \begin{enumerate}[labelsep=0pt] \item local, zero \texttt{labelsep} \item K \begin{itemize} \item Z \item P \end{itemize} \end{enumerate} \end{itemize} \end{document} ```

image

tobiasBora commented 10 months ago

Thanks a lot. But I guess there are many other properties one might want to preserve no? (like nosep would certainly modify other lengths) Is there a way to reset them all to their default value?

muzimuzhi commented 10 months ago

enumitem v3.9 (2019/06/20) doesn't have such feature.

Below is an attempt that caches values of all but \leftmargin list parameters used by LaTeX2e kernel as default value sets, by typesetting three list envs with all nesting levels supported by LaTeX2e kernel in hook begindocument (affected by \setlist used in preamble), and restore default values at beginning of such list envs, after \@list<roman-nesting-level> but before enumitem global (set by \setlist) and local (passed as env opt-arg) settings are applied.

Currently the restoration is unconditional. To provide a new option as trigger, enumitem needs to support selective key setting in some sort.

Full attempt

```tex \documentclass[twocolumn]{article} \usepackage{enumitem} \usepackage{xpatch} \makeatletter % store defaults in \cs{enit@@defaults} \def\enit@savedefaults#1#2{% \ExpandArgs{ne}\enit@savedefaults@wrap{#1}{% % to match the 1st-arg passed to \enit@preset \UseName{str_case_e:nn}{#1}{% {enumerate}{enum}% {itemize}{item}% {description}{description}% }% }{#2}{1}% } % #1 = envname, #2 = short envname, #3 = max depth, #4 = current depth \def\enit@savedefaults@wrap#1#2#3#4{% \ifnum#4>#3\relax \else \begin{#1} \enit@savedefaults@item{#2} \ExpandArgs{nne}\enit@savedefaults@wrap{#1}{#2}{#3}{\inteval{#4+1}} \end{#1} \fi } \def\enit@savedefaults@item#1{% \item x \ExpandArgs{c}\xdef{enit@#1\romannumeral\the\@listdepth @defaults}{% \enit@savedefaults@loop % list parameters used by latex kernel \itemindent \itemsep \labelsep \labelwidth % \leftmargin % resettig this one is wired \listparindent \parsep \partopsep \rightmargin \topsep % new enumitem parameter \labelindent is already reset in \enit@preset \enit@savedefaults@stop }% } \def\enit@savedefaults@loop#1{% \ifx#1\enit@savedefaults@stop \else #1=\the#1% \expandafter\enit@savedefaults@loop \fi } \let\enit@stop=\relax % save defaults per list env type, per nesting level % In preamble, switching from v-mode to h-mode throws "Missing \begin{document}" error. % It's not the case after \document sets \everypar{} in between executing % hooks `begindocument/before` and `begindocument`. \AddToHook{begindocument}{% \setbox\@tempboxa=\vbox{% \enit@savedefaults{enumerate}{4}% \enit@savedefaults{itemize}{4}% \enit@savedefaults{description}{6}% yes description has max depth of 6 }% \setbox\@tempboxa=\hbox{}% } \makeatother % TODO: % - support new max-depth limit set by \setlistdepth % - check if it works for \newlist \begin{document} \def\test{ \begin{itemize} \item A \item A \begin{enumerate} \item B \item B \begin{itemize} \item C \item C \item C \begin{itemize} \item 123 \item 123 \item 123\rlap{\rule{\textwidth}{.4pt}} \end{itemize} \end{itemize} \end{enumerate} \item A \begin{enumerate}[labelsep=0pt, listparindent=20pt] \item B \item B \begin{itemize}[nosep] \item C \item C \item C \begin{itemize} \item 123 \item 123 \item 123\rlap{\rule{\textwidth}{.4pt}} \end{itemize} \end{itemize} \end{enumerate} \end{itemize} } \subsection*{Before} \test \newpage \makeatletter \xpatchcmd\enit@preset {\labelindent\z@skip} {\@nameuse{enit@#1\romannumeral\the\@listdepth @defaults}% \labelindent\z@skip} {}{\PatchFailed} \makeatother \subsection*{After} \test \end{document} ```

image

tobiasBora commented 10 months ago

Thanks a lot! Would be great to have a key to enable this reset!

muzimuzhi commented 10 months ago

You could try it yourself first.

jbezos commented 10 months ago

The behavior of labelsep is explained in the manual. I have no plans to add new features in this regard, sorry.