3

I am using an article class. I am using a description list to have a itemized list with custom labels instead of bullets. In my case the values of the labels are in dollars (see example below).

Here is an example:

\begin{description}
\item[\$550] 
boo boo
\item[\$500] 
boo boo boo
\end{description}

I have hundreds of entries in this custom list, which makes it very hard to change everything. I would like to have two enhancements:

  1. Add another line just above this list which sums up the values of the custom labels. In this case it would read \$1050 summary. So basically if I will add a third item to the list, this will sum it up automatically. Say the third item said \item[\$100] boo the summary line changes to \$1,150.
  2. I would like all the labels to have the small font. So I would like the label to be \item[{\small \$550}] . Of course I don't want to do this for all the hundreds of items. How should I make this as a default?

UPDATE: spacing in the list is defined with \setlist[description]{font=\normalfont\itshape\space,nolistsep,labelwidth=6.5em,leftmargin =\dimexpr\labelwidth+\labelsep\relax} and I'd like it to be the same

2

First solution: redefining \item.

Here's a first solution which should fit better your requirements, since it should work without needing much changes to your document.

It creates a customlist environment from the enumerate environment, using the enumitem package. In this customlist environment, the labels of the items are in small font, and displayed with a dollar sign in front of them. Also, once in the customlist (and only in this environment), the \item command is redefined to be able to sum up the amounts listed, while an \olditem command is defined to be the standard \item command. A \summaryitem command is also defined, and can be used to insert an item in the list in which the total amount is displayed.

\documentclass{article}
\usepackage{enumitem}
\usepackage{etoolbox}
\newlist{customlist}{enumerate}{1}
\setlist[customlist]{%
    label={\small\$\arabic*}
}
\newcounter{customlist}
\newcounter{total}

\makeatletter
\newcommand{\summaryitem}{%
    \@ifundefined{totalofcustomlist\romannumeral\the\value{customlist}}
        {}
        {\setcounter{customlisti}{\csname totalofcustomlist\romannumeral\the\value{customlist}\endcsname}}
    \addtocounter{customlisti}{-1}
    \olditem
}
\newcommand{\amount}[1]{\@cdr #1\@nil}
\AtBeginEnvironment{customlist}{%
    \setcounter{total}{0}
    \stepcounter{customlist}
    \let\olditem\item
    \def\item[#1]{%
        \setcounter{customlisti}{\amount{#1}}
        \addtocounter{customlisti}{-1}
        \addtocounter{total}{\amount{#1}}
        \olditem
    }
}
\AtEndEnvironment{customlist}{%
    \immediate\write\@mainaux{\string\gdef\string\totalofcustomlist\romannumeral\the\value{customlist}{\the\value{total}}}
    \let\item\olditem
}
\makeatother

\begin{document}

First custom list:
\begin{customlist}
    \summaryitem Summary of first list.
    \item[\$500] First item
    \item[\$200] Second one
    \item[\$550] Third one
\end{customlist}

Second one:
\begin{customlist}
    \summaryitem Summary of second list.
    \item[\$250] First item
    \item[\$550] Second one
\end{customlist}

\end{document}

The above code returns the expected output:

I think this approach has an important flaw: it redefines the \item command, so this customlist environment has to be used carefully. For example, if an itemize environment is nested in the customlist, the \item command cannot be used to create items; one would have to use \olditem instead. However, it has the advantage that if you only want to format this huge list that you already created with items labelled like \item[\$100], it would work directly by changing the description environment to customlist.

Second solution: not redefining \item.

Here's another (better) solution.

It creates a similar customlist environment, again from the enumerate environment and using the enumitem package. A similar \summaryitem command is defined, and the labels of the items in the list are also displayed in small font with a dollar sign in front of the amount displayed. However, in this solution, the \item command is not redefined: an \amountitem command is defined to write items in the list while handling the counting of the amounts. Its usage is quite the same as \item, but it requires a number as an argument (which should be the amount of the corresponding item in the list).

The following code gives the exact same output as the one from the first solution.

\documentclass{article}
\usepackage{enumitem}
\usepackage{etoolbox}
\newlist{customlist}{enumerate}{1}
\setlist[customlist]{%
    label={\small\$\arabic*}
}
\newcounter{customlist}
\newcounter{total}
\newcommand{\amountitem}[1]{%
    \setcounter{customlisti}{#1}
    \addtocounter{customlisti}{-1}
    \addtocounter{total}{#1}
    \item
}
\makeatletter
\newcommand{\summaryitem}{%
    \@ifundefined{totalofcustomlist\romannumeral\the\value{customlist}}
        {}
        {\setcounter{customlisti}{\csname totalofcustomlist\romannumeral\the\value{customlist}\endcsname}}
    \addtocounter{customlisti}{-1}
    \item
}
\AtBeginEnvironment{customlist}{%
    \setcounter{total}{0}
    \stepcounter{customlist}
}
\AtEndEnvironment{customlist}{%
    \immediate\write\@mainaux{\string\gdef\string\totalofcustomlist\romannumeral\the\value{customlist}{\the\value{total}}}
}
\makeatother
\begin{document}

First custom list:
\begin{customlist}
    \summaryitem Summary of first list.
    \amountitem{500} First item
    \amountitem{200} Second one
    \amountitem{550} Third one
\end{customlist}

Second one:
\begin{customlist}
    \summaryitem Summary of second list.
    \amountitem{250} First item
    \amountitem{550} Second one
\end{customlist}

\end{document}
2
  • Thanks this is a nice answer - I am looking at the second answer. To make it perfect for my example, I also need the "customlist" environment to have the spacing I have set on my other description environments. SPecifically I need the same code as yours with the addition that the new list you create matches this: \setlist[description]{font=\normalfont\itshape\space,nolistsep,labelwidth=6.5em,leftmargin =\dimexpr\labelwidth+\labelsep\relax}
    – bissi
    Feb 26 '20 at 10:10
  • Also, I think it would help for the general audience if you remove the inbuilt $ sign - that way we can add labels in GBP, Euros etc. as well. And, one issue I am having with your code is that a label of "2500" works but a label of "2,500" does not work. I want the latter.
    – bissi
    Feb 26 '20 at 10:12

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.