# trivlist list environment \item command

My document class uses paralist and redefines \begin{enumerate}[(a)][abc] etc... environments to issue \label for each \item, given a second option (e.g., \abc{a}) that becomes a 1-parameter macro issuing \ref. For this both \@item and \pl@item are "overloaded".

However, there is a problem with, e.g., ntheorem environment nested inside enumerate:

\begin{enumerate}[(a)][abc] % \ref{...} is the body of "\gdef\abc" in refined
\item one
\begin{theorem}[second]
... and so it follows.
\end{theorem}
\end{enumerate}

As in the point \abc*{1} above... % starred wraps around parentheses


I found ntheorem (and many other environments) use trivlist for style; so the example above is troublesome because there is an extra \item issued, without \@listdepth increased.

I have actually to switch between \let\@item\old@item and \let@item\new@item accordingly with my redefined \begin|\end{enumerate}, because from "new" I also generate \label commands, for each \item issued.

\let\old@item\@item
\gdef\@item[#1]{\old@item[#1] ...}
\let\new@item\@item
\let\old@pl@item\pl@item
\def\pl@item[#1]{\old@pl@item[#1] ...}
\let\new@pl@item\pl@item


But the \trivlist from ntheorem gives "multiple defined labels" because of the "spurious" \item coming with \trivlist.

Next try was to redefine \@trivlist (also \endtrivlist),

\def\@trivlist{\if@newlist ... \fi}


where the dots in \@trivlist keep track of whether

\def\endtrivlist{\if@newlist ... \fi}


the dots in \endtrivlist should restore either the old or the new \@item and \pl@item (re)definitions.

There is, though, never the case that \@newlist conditional is true, and \trivlist is heavily used everywhere: thus, I fail to discern the case of list environmens (that enumerate are too), from... \everypar?

Initially, my intent was to intercept \@thm and \@endtheorem, but I tried widening the possibilities of environments nested in redefined \begin{enumerate}, and this is how I ran over \trivlist.

The problem is that \@trivlist occurs many times not paired by \endtrivlist, which makes my task impossible (since I employ a push/pop technique). And trying using \if@newlist, supposedly suggesting that a list (always paired) is involved, the code is skipped, the flag is false. That not enough, \@trivlist also tests the flag \if@inlabel, having something to do with just after \item, while I must take care of old/new trick before \item.

Appreciate any help, Thank s.

-
Please, add a complete minimal example. – egreg Jan 6 '13 at 20:29
In general it's a bad idea to globally change the meaning of \item – egreg Jan 6 '13 at 20:57
Ok, it would seem I should add an ME. – sjb Jan 7 '13 at 11:15

## 3 Answers

\endtrivlist should be paired with \trivlist (rather than \@trivlist) despite the name including the letters l i s t the trivlist mechanism should be seen as the basic display environment mechanism in LaTeX. It is used for all kinds of display environment that would not normally be considered lists, such as center, verbatim, tabbing. You really don't want to mess with \trivlist unless you want to redefine everything.

-
Yes, it should, not by my code, though, I don't use, I only try to keep track of alternating nested hierarchies of "enumerate" environments from paralist however mixed with other list environments, e.g., ntheorem. My question concerned what can I do with unexpected behavior of \trivlist, since I depend on \@trivlist/\endtrivlist to come in pair, I do not want to change them, I hook them. – sjb Jan 7 '13 at 11:23

Based upon the snippets of a possible MWE you've provided, I'm thinking the following might be along the lines of what you want. Instead of redefining LaTeX commands, I've created a new environment myenum and a pseudo \item that I call with \myitem. This way there should be no conflicts with other definitions relying upon LaTeX's definitions.

I use xparse to create an environment with two optional arguments (since your code seems to indicate that that's what you want) and their default behaviors. Though if you omit the second and then later omit both, LaTeX will be unhappy about multiply defined labels. There seem to be two reasonable choices here (1) change the order of the arguments (2) force one of them to be mandatory.

You also seem to want your second argument to define a command to call the particular reference and a starred version to apply parentheses around the reference. Here's a MWE that does that while also making the second argument mandatory:

\documentclass{article}
\pagestyle{empty}
\usepackage{xparse}
\usepackage{paralist}
\usepackage{amsthm}
\newtheorem{theorem}{theorem}
%%
\makeatletter
%% create a command to assign an enum-based prefix for each label
\newcommand{\my@label@prefix}{unused}
%% allows a starred version for the reference to have parentheses.
\newcommand{\my@ref}[2]{\ref{#1:#2}}
\newcommand{\my@@ref}[2]{(\my@ref{#1}{#2})}
\NewDocumentEnvironment{myenum}{ O{(a)} m }
{\renewcommand{\my@label@prefix}{#2}%
%% check whether second argument corresponds to pre-existing command!
\expandafter\@ifdefinable\csname #2\endcsname%
{\global\@namedef{#2}{\@ifstar{\my@@ref{#2}}{\my@ref{#2}}}}%
\begin{enumerate}[#1]}
{\end{enumerate}}
%% This next line uses paralist's internal
%% \pl@the to pass the correct label mark.
\newcommand{\myitem}{\item\label{\my@label@prefix:\pl@the}}
%% \newcommand{\myitem}{\item\label{\my@label@prefix:\alph{enumi}}}
\makeatother
\begin{document}
\noindent%
First:
\begin{myenum}[(a)]{abc}
\myitem one
\begin{theorem}\label{thm:first}
... and so it follows.
\end{theorem}
\myitem two
\end{myenum}
Second (\abc{a}):
\begin{myenum}[(A)]{xyz}
\myitem one
\begin{theorem}\label{thm:second}
... and so it follows.
\end{theorem}
\myitem two
\end{myenum}
Third:
\begin{myenum}[(i)]{getit}
\myitem one
\myitem two
\myitem three
\myitem four
\myitem five
\end{myenum}

\textbf{From first:} As in the point \ref{abc:a} and \abc{b} above...

\textbf{From second:} As in the point \ref{xyz:A} and \xyz*{B} above...

\textbf{From third:} As in the point \getit*{iv} and \getit{iii} above...

\textbf{Theorems:} The first theorem is \ref{thm:first} and the second is \ref{thm:second}
\end{document}


It seems a bit contrary to LaTeX to define labels in a manner that depends upon a particular order. But also, that seemed to be what you wanted and I can imagine situations where that might be desirable (just not many).

-
Thanks for your viable answer. I already have implemented all four \begin{asparaenum}, \begin{inparaenum}, \begin{compactenum}, \begin{enumerate}: myenum and \myitem are out of the question; \label is issued (hierarchically, with possible lists of item refs or "numbers") for all five representations: alph, Alph, roman, Roman, arabic, so that users make no mistake however they like to reference \items. Users must only use known LaTeX. My question is not about what I have already coded. Avoiding \item by \myitem is unacceptable. It was really fast your solution, though. – sjb Jan 7 '13 at 11:10

Since \begin{enumerate} vs \end{enumerate} always come in pair, and the only obvious connection with other "list" environments are the \begin vs \end "commands", hooking them by redefinition, employing only one redefinition of \@item and \pl@item throughout the document but using a \boolean flag to condition the actually needed side-effect upon issuing \item (i.e., generate \label commands), together at last with a stack-like "\GetTokens vs \@removeelement" implemented list... seems the best-fit trick there is.

The \trivlist vs \endtrivlist proved indeed an impossible MWE trial&error direction.

The missing MWE would have taken too much "moving out of the context" effort; instead, i failed with an "unbalanced" attempt like basing on the first \item occurrence (following the idea from \if@newlist) for "push" paired with \endtrivlist for "pop".

\let\my@begin\begin
\def\begin{%\my@push{either true or false}{into the stack} depending on flag
%set flag false
\my@begin}

\let\my@end\end
\def\begin{%\my@pop{into \my@temp}{from the stack} and set flag to \my@temp
\my@end}

%\let\my@enum\---macro missing---
%redefined \begin{enumerate}: invoke \my@enum; set flag true; ...

\let\my@item\@item
\gdef\@item[#1]{\my@item[#1]%
%trigger generate \label only if flag and ...%
}

\let\my@pl@item\pl@item
\def\pl@item[#1]{\my@pl@item[#1]%
%trigger generate \label only if flag and ...%
}


s.

-