14

I would like to enumerate a list, but instead of the standard numbering

 1. bar
 2. bar
 3. bar
 4. bar
 5. bar

I would like the labels to be powers of 2 (automatically generated):

 1. bar
 2. bar
 4. bar
 8. bar
16. bar

etc. Can this be done with enumitem or pgf?

2
  • 4
    I'm curious, why do you need this?
    – suvayu
    Oct 25 '17 at 23:25
  • somes questions requiere as answer the sum of the correct options, witch are given in powers of two Oct 28 '17 at 17:54
20

Examples with xfp or pgfmath are shown below. Note that the enumi counter is for the outermost level, if this is a nested list you need enumii/enumiii/enumiv, depending on the level of nesting.

output of code

\documentclass{article}
\usepackage{enumitem}
\usepackage{xfp}
\usepackage{pgfmath}
\begin{document}
\begin{enumerate}[label={\protect\fpeval{2^(\value{enumi}-1)}.}]
\item
foo
\item
bar
\item
baz
\item
foo
\item
bar
\item
baz
\item
foo
\item
bar
\item
baz
\end{enumerate}
\begin{enumerate}[label={\protect\pgfmathparse{int(2^(\value{enumi}-1))}\protect\pgfmathresult.}]
\item
foo
\item
bar
\item
baz
\item
foo
\item
bar
\item
baz
\item
foo
\item
bar
\item
baz
\end{enumerate}
\end{document}
2
  • It returned the error "Font \TU/lmr/m/n/12=[lmroman12-regular]:+tlig; at 12pt not loadable: metric data not found or bad. \normalsize". Is there a way to do these calculations with pgf? Oct 25 '17 at 13:31
  • @geekformoney Yes (see updated answer), I don't know why this would trigger a font error though. Oct 25 '17 at 13:41
12

No other package used, only enumitem's way of adding enumerate counter and evaluating with \numexpr, defining a new list in order to keep the regular enumerate list clean.

\documentclass{article}
\usepackage{enumitem}

\newcounter{powercntr}
\makeatletter
\DeclareRobustCommand{\poweroftwocalc}[1]{%
  \ifnum1<\value{#1}%
  \setcounter{powercntr}{\numexpr\c@powercntr*2}
  \fi
}
\def\poweroftwo#1{\poweroftwocalc{#1}\expandafter\@poweroftwo\csname the#1\endcsname}
\def\@poweroftwo#1{\thepowercntr}

\AddEnumerateCounter{\poweroftwo}{\@poweroftwo}{4096}

\newlist{powerlist}{enumerate}{1}
\setlist[powerlist,1]{before=\setcounter{powercntr}{1}}

\makeatother



\begin{document}
\begin{powerlist}[label={\poweroftwo*}]
\item And
\item Now
\item For
\item Something
\item Completely 
\item Different
\item Was
\item Said
\item Very
\item Frequently
\item In
\item Monty
\item Python's
\item Flying
\item Circus
\end{powerlist}
\end{document}

enter image description here

10

No other packages used, no e-TeX needed. Basically a rip-off of @ChristianHupfer's idea.

\documentclass{article}
\usepackage{enumitem}

\newcounter{powercntr}

\DeclareRobustCommand{\updatepowercntr}[1]%
  {\arabic{powercntr}\addtocounter{powercntr}{\value{powercntr}}}

\newlist{powerlist}{enumerate}{1}
\setlist[powerlist,1]{before=\setcounter{powercntr}{1}}

\begin{document}
\begin{powerlist}[label={\updatepowercntr*}]
\item And
\item Now
\item For
\item Something
\item Completely 
\item Different
\item Was
\item Said
\item Very
\item Frequently
\item In
\item Monty
\item Python's
\item Flying
\item Circus
\end{powerlist}
\end{document}

enter image description here

9

Here is a version demonstrating \AddEnumerateCounter and using xintcore for the arithmetic. This way you can start at any (positive) power of 2 you choose and nesting is no problem.

Sample output

\documentclass{article}

\usepackage{enumitem,xintcore}

\AddEnumerateCounter*{\poweroftwo}{\internalpoweroftwoname}{1}
\newcommand{\poweroftwo}[1]{\xintiiPow{2}{\value{#1}}}

\begin{document}

\begin{enumerate}[label={\poweroftwo*},start={19}]
\item A
\item list
\item enumerated
\item with
\item powers
\item of
\item two
\item starting
\item at
\item the
\item initial
\item value
\item 5
\item 2
\item 4
\item 2
\item 8
\item 8
\end{enumerate}

\end{document}

You probably want to use this with leftmargin=*,widest*=38.

I originally used the bigintcalc package but @jfbu kindly pointed out that xintcore is significantly faster. For bigintcalc the syntax

\newcommand{\poweroftwo}[1]{\bigintcalcPow{2}{\number\value{#1}}}

with \number is needed.

Note I have simplified the syntax from the enumitem manual. We no longer need a helper macro, and can avoid @ in names, so do not have to encase in \makeatletter...\makeatother. The second argument of \AddEnumerateCounter looks like a macro, but is actually stripped of the leading \ to produce a character string to build internal names in enumitem.

8
  • +1 syntax with package xintcore would be \newcommand{\@poweroftwo}[1]{\xintiiPow 2 {#1}}
    – user4686
    Oct 26 '17 at 8:03
  • @jfbu I have now simplified this further, so your example (after \usepackage{xintcore}) would now be \newcommand{\poweroftwo}[1]{\xintiiPow 2 {\number\value{#1}}} Oct 27 '17 at 7:43
  • +2 ;-) I am obedient LaTeX citizen hence the official recommended way is \xintiiPow{2}{\number\value{#1}} :)
    – user4686
    Oct 27 '17 at 7:51
  • 1
    @jfbu Good syntax point - I'll update my corresponding use to add brackets around the 2 etc. Oct 27 '17 at 7:56
  • @jfbu Impressive speed up - I have now changed my answer to use xintcore Oct 27 '17 at 8:34

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.