3

I am using the enumitem package, and I would like to redefine the optional argument of the \item command. By default, the optional argument accepts text to replace the default label. I never exercise this option. Instead, I would like the optional argument to accept a number, say 10, which is then printed at the beginning of the item paragraph in the form (10 points). The label itself is not effected and still registers the label counter. I'm aware that this behavior is implemented by the Exam Document Class, but I prefer to customize my own lists via enumitem. Thanks in advance.

5

You can use the following implementation:

enter image description here

\documentclass{article}

\usepackage{enumitem,xparse}

\makeatletter
% enumitem-related constructions
\newlist{pointslist@}{enumerate}{1}
\setlist[pointslist@]{label=\arabic*.,ref=\arabic*}
% Create list emulator
\newenvironment{pointslist}[1][]
  {\begin{pointslist@}[#1]% \begin{pointslist}[<opts>]
     \let\olditem\item% Store \item in \olditem
     \RenewDocumentCommand{\item}{o}{% Redefine \item to
       \olditem\leavevmode% ... call \olditem...
       \IfNoValueF{##1}{(##1~points)}% ...and possibly (.. points)
     }}
  {\end{pointslist@}}% \end{pointslist}
\makeatother

\begin{document}

\begin{pointslist}
  \item This is some item
  \item[10] This is item is worth something
  \item Just another item
\end{pointslist}

\end{document}

In principle, we create an enumitem-defined list - pointslist@ - and set everything accordingly. Then we create a new environment that would emulate the list, only to redefine \item to suit your needs.

The redefinition of \item is such that it always sets the original \item (\olditem in the redefinition) before checking whether you supplied your optional argument. If there was an optional argument specified, it sets the appropriate message.


A rudimentary LaTeX2e implementation for the xparse command redefinition might resemble

\newenvironment{pointslist}[1][]
  {\begin{pointslist@}[#1]%
     \let\olditem\item
     \renewcommand{\item}[1][\relax]{%
       \olditem\leavevmode
       \ifx\relax##1\relax\else(##1~points)\fi%
     }}
  {\end{pointslist@}}

The only test would be to see whether the user supplied an optional argument or not. Options for checking this is available in Different command definitions with and without optional argument.

  • This looks very nice, Werner, although I'll need to study it. Where does the xparse come in? – steven_nevets Jan 28 '15 at 22:58
  • @steven_nevets: xparse provides an easy-to-use interface for checking the use/existence of optional arguments. There are pure LaTeX2e equivalents, but xparse is the future... :) – Werner Jan 28 '15 at 23:01
  • Thanks, Werner, for the incorporated documentation and LaTeX2e equivalents. That is very much appreciated. – steven_nevets Jan 29 '15 at 18:58
  • how to provide an optional argument for pointslist? e.g. \begin{pointslist}[(2)] \item ... \end{pointslist} to start from 2 instead of 1. – user19832 Sep 21 '16 at 12:42
  • 1
    @user19832: Use \begin{pointslist}[start=2] \item ... \end{pointslist}. This is thanks to enumitem. – Werner Sep 21 '16 at 16:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.