Text Formattingadvanced use of enumerate

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
ptoche
Posts: 49
Joined: Thu Apr 12, 2007 10:41 am

advanced use of enumerate

Post by ptoche »

I'd like to produce an _enumerate_ list with a custom number of stars placed to the left of the enumerate label, like this:

* 1. This is an easy question
** 2. This is a difficult question
*** 3. This is an impossible question

I know how to place one star everywhere (or indeed two or three stars), using the _enumerate_ package, but I do not know how to alternate the number of stars:

Code: Select all

\usepackage{enumerate}
\begin{enumerate}[*]
\item This is an easy question and gets 1 star
\item This is a difficult question and should get 2 stars, but only gets 1 as it is...
\item This is an impossible question and should get 3 stars, but only gets 1 as it is...
\end{enumerate}
Thanks for your help!

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

advanced use of enumerate

Post by gmedina »

Hi,

the enumitem package offers you a solution:

Code: Select all

\documentclass{article}
\usepackage{enumitem}

\makeatletter
  \def\cstar#1{\expandafter\@cstar\csname c@#1\endcsname}
  \def\@cstar#1{\ifcase#1\or $\ast$\or $\ast\ast$\or $\ast\ast\ast$\fi}
  \AddEnumerateCounter{\cstar}{\@cstar}{$\ast\ast\ast$}
\makeatother

\begin{document}

\begin{enumerate}[label=\cstar*\,\arabic*.]
  \item first item.
  \item second item.
  \item third item.
\end{enumerate}

\end{document}
Please refer to the package documentation for further information.
1,1,2,3,5,8,13,21,34,55,89,144,233,...
ptoche
Posts: 49
Joined: Thu Apr 12, 2007 10:41 am

advanced use of enumerate

Post by ptoche »

thanks a lot for your suggestion, it's pretty close. Let me clarify what I'm looking for.

I want to have an _enumerate_ list where some items will be preceded by one star, others by two stars, etc. but I need to retain the ability to specify how many stars to be assigned to each individual item -- the 1 2 3 pattern apparent in my example was a fluke. How can I do something like:

* 1. This is an easy question
*** 2. This is an impossible question
* 3. This is an easy question
** 4. This is a difficult question
*** 5. This is an impossible question
** 6. This is a difficult question
** 7. This is a difficult question
* 8. This is an easy question

I guess I would need to have a _label_ inside each _item_ to specify how many stars are to be assigned to it, then the _enumerate_ environment would need to be told how to print the content of the _label_ next to the standard _enumerate_ _label_, perhaps something along the lines of:

Code: Select all

\begin{enumerate}[\label \labelenumi]
\item \label{**} This is a difficult question 
\item \label{*} This is an easy question
\item \label{***} This is an impossible question
\end{enumerate}
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

advanced use of enumerate

Post by localghost »

Indeed you should have been more precise. Try the following.

Code: Select all

\documentclass [11pt,a4paper,english]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{babel}
\usepackage{tabularx}
\usepackage{blindtext}

\newcounter{lineno}

\begin{document}
  \begin{tabularx}{\linewidth}{@{}r>{\stepcounter{lineno}\thelineno.}rX@{}}
    $\ast\ast$ & & \blindtext \\ \\
    $\ast\ast\ast$ & & \blindtext \\ \\
    $\ast\ast$ & & \blindtext \\ \\
    $\ast$ & & \blindtext
  \end{tabularx}
\end{document}

Best regards
Thorsten
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

advanced use of enumerate

Post by gmedina »

Here's another option, defining commands \itemone, \itemtwo, and \itemthree to add one, two, and three stars, respectively, before the correponding number:

Code: Select all

\documentclass{article}

\newcommand\itemone{\stepcounter{enumi}\item[$\ast$\, \theenumi.]}
\newcommand\itemtwo{\stepcounter{enumi}\item[$\ast\ast$\, \theenumi.]}
\newcommand\itemthree{\stepcounter{enumi}\item[$\ast\ast\ast$\, \theenumi.]}

\begin{document}

\begin{enumerate}
  \itemone first item.
  \item second item.
  \itemone third item.
  \itemtwo fourth item.
  \itemthree fifth item.
  \itemone sixth item.
\end{enumerate}

\end{document}
1,1,2,3,5,8,13,21,34,55,89,144,233,...
ptoche
Posts: 49
Joined: Thu Apr 12, 2007 10:41 am

Re: advanced use of enumerate

Post by ptoche »

thanks a lot for your suggestion Thorsten, but I *am* looking for an _enumerate_ solution...
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

advanced use of enumerate

Post by gmedina »

ptoche wrote:thanks a lot for your suggestion Thorsten, but I *am* looking for an _enumerate_ solution...
Perhaps you didn´t notice my previous reply?
1,1,2,3,5,8,13,21,34,55,89,144,233,...
ptoche
Posts: 49
Joined: Thu Apr 12, 2007 10:41 am

Re: advanced use of enumerate

Post by ptoche »

Dear gmedina,

I hadn't seen your message! According to my clock, your message was posted at 10:08am, while mine was posted at 10:09am!

Your code is *exactly* what I was looking for. I am ever so grateful for your help. I can see ways to apply the idea to other uses too, great stuff. The first piece of code you posted could come in handy one day too, always useful to have.

Thank you very much,
itatitl
Posts: 13
Joined: Thu Mar 24, 2011 8:20 am

advanced use of enumerate

Post by itatitl »

Is it possible to modify this so that it works for various labelling styles?
For example, the current one fails when we try

Code: Select all

\begin{enumerate}[(i)]
\item One
\item Two
\itemone Three
\end{enumerate}
Last edited by cgnieder on Wed Jan 29, 2014 12:34 pm, edited 1 time in total.
alainremillard
Posts: 45
Joined: Fri Mar 16, 2012 8:22 pm

advanced use of enumerate

Post by alainremillard »

itatitl wrote:Is it possible to modify this so that it works for various labelling styles?
For example, the current one fails when we try

Code: Select all

\begin{enumerate}[(i)]
\item One
\item Two
\itemone Three
\end{enumerate}
Have a look at the enumitem package. It allows customization of enumerate environment.

Code: Select all

\documentclass{article}

\usepackage{enumitem}

\newcommand\itemone{\stepcounter{enumi}\item[$\ast$\, \theenumi.]}
\newcommand\itemtwo{\stepcounter{enumi}\item[$\ast\ast$\, \theenumi.]}
\newcommand\itemthree{\stepcounter{enumi}\item[$\ast\ast\ast$\, \theenumi.]}

\begin{document}

\begin{enumerate}[label=(\roman*)]
  \itemone first item.
  \item second item.
  \itemone third item.
  \itemtwo fourth item.
  \itemthree fifth item.
  \itemone sixth item.
\end{enumerate}

\end{document}
Post Reply