Closed syvshc closed 2 years ago
This seems to be an X-Y problem. To your original requirement, try this:
\documentclass{article}
\usepackage{enumitem}
\newlist{myenum}{enumerate}{3}
\setlist[myenum,1]{label=(\arabic*)}
\setlist[myenum,2]{label=(\roman*)}
\makeatletter
% #1: label prefix typeset inside \llap
% #2: one-arg styling command
\newcommand{\uselistlabel}[2]{%
\stepcounter{\@enumctr}%
\llap{#1}%
% see https://github.com/jbezos/enumitem/blob/1bdcad0987823b3716c86b126b3863f895ea9c4a/enumitem.sty#L1371
#2{\@nameuse{label\@enumctr}}%
}
\makeatother
\begin{document}
\begin{myenum}
\item
\item[\uselistlabel{*}{\textbf}] \leavevmode
\begin{myenum}
\item
\item[\uselistlabel{\textsuperscript{+}}{\textbf}]
\end{myenum}
\end{myenum}
\end{document}
PS: Personally I recommend always specifying the language of a markdown code block. It makes the code block highlighted hence more readable, see https://github.github.com/gfm/#example-112.
This seems to be an X-Y problem. To your original requirement,
Thanks for your reply, I found that I can't \ref
the \uselistlabel
ed \item.
Sorry for not clearify that at begining
To the attempt you shown:
\@listdepth
and \@enumdepth
holds the current enumeration nesting depth. But it seems enumitem
only updates \@listdepth
(and \@enumdepth
is always 0
, hence \expandafter\let\expandafter\originallabel\csname labelexer\romannumeral\@enumdepth\endcsname
is letting \originallabel
to \relax
). See how \@listdepth
is used in \@toodeep
.\expandafter\bfseries\expandafter{\expandafter\originallabel}
, the \expandafter
before \originallabel
is superfluous.Note generally, a patch working with the LaTeX2e itself will not continue to work when a package extending the same area is loaded. There're always some internals changed.
Adapting your attempt,
\documentclass{article}
\usepackage{enumitem}
% https://tex.stackexchange.com/a/52718/180617
\makeatletter
\def\exeritem{%
\expandafter\let\expandafter\originallabel\csname labelexer\romannumeral\@listdepth\endcsname
\expandafter\def\csname labelexer\romannumeral\@listdepth\expandafter\endcsname\expandafter{%
\expandafter\bfseries\expandafter{\originallabel}}%
\item
\expandafter\let\csname labelexer\romannumeral\@listdepth\endcsname\originallabel
}
\makeatother
\newlist{exer}{enumerate}{2}
\setlist[exer, 1]{leftmargin=*, label=\arabic*., widest=000, ref=\arabic*}
\begin{document}
\begin{exer}
\item
\exeritem \labelexeri
\end{exer}
\end{document}
Also you may find utility macros from etoolbox
helpful in reducing number of \expandafter
s used:
\usepackage{etoolbox}
% https://tex.stackexchange.com/a/52718/180617
\makeatletter
\def\exeritem{%
\letcs{\originallabel}{labelexer\romannumeral\@listdepth}%
% the \edef variant of \@namedef
\csedef{labelexer\romannumeral\@listdepth}%
{\noexpand\textbf{\expandonce\originallabel}}%
\item
\cslet{labelexer\romannumeral\@listdepth}\originallabel
}
\makeatother
I found that in enumitem
, it uses \enit@depth
to control depth
https://github.com/jbezos/enumitem/blob/1bdcad0987823b3716c86b126b3863f895ea9c4a/enumitem.sty#L514
so if I change \@enumdepth
in exer
to enit@depth
, problem solved.
Thanks @muzimuzhi 's trick of etoolbox
's macro.
sorry for reopen this issue, I tested the code below
\documentclass{article}
\usepackage{enumitem}
\usepackage{etoolbox}
% https://tex.stackexchange.com/a/52718/180617
\makeatletter
\def\exeritem{%
\letcs{\originallabel}{labelexer\romannumeral\enit@depth}%
% the \edef variant of \@namedef
\csdef{labelexer\romannumeral\enit@depth}%
{\textbf{\originallabel}}%
\item
\cslet{labelexer\romannumeral\enit@depth}\originallabel
}
\makeatother
\newlist{exer}{enumerate}{2}
\setlist[exer, 1]{leftmargin=*, label=\arabic*., widest=000, ref=\arabic*}
\begin{document}
\begin{exer}
\item
\exeritem \labelexeri
\item
\end{exer}
\end{document}
I found that \csedef
with expanding control works the same as \csdef
without expanding control, so I want to ask why @muzimuzhi used \csedef
?
I found that
\csedef
with expanding control works the same as\csdef
without expanding control, so I want to ask why @muzimuzhi used\csedef
?
I guess it's because in your original example (see snippet below), i thought you wanted to use the replacement text of \originallabel
in defining control sequence labelexer\romannumeral\@enumdepth
. I spotted the surplus \expandafter
just before \originallabel
which caused problem, and didn't check if any other \expandafter
s were all necessary.
\expandafter\def\csname labelexer\romannumeral\@enumdepth\expandafter\endcsname\expandafter{% \expandafter\bfseries\expandafter{\expandafter\originallabel}}%
I see, thanks
I want to change several appearence of item in a new enumerate-like environment
exer
. I wrote thisAnd the output is :
We can notice that the definition of
\bitem
and\exeritem
are totally the same except the label commandlabelexer
andlabelenum
. However the\exeritem
didn't work. But when I wrote the command directly inexer
environment (the second environment), it worked. why would that happen?