6

Some old tests around here had questions that consisted in analyzing several items (each with a different number associated with it) and adding up the numbers of all the correct ones. To avoid having multiple combinations giving the same answers like 1+3 = 4 and 4=4, the numbers are all powers of 2, so:

01. This is the first item (2⁰)
02. This is the second item (2¹)
04. This is the third item (2²)
08. This is the forth item (2³)
16. This is the fifth item (2⁴)

So my goal is to recreate this kind of enumerating list, that follows the powers of 2 (0 -- 1 -- 2 -- 4 -- 8 -- 16 -- 32 and so on). Is there a package that can do it or a rather simple way around it? In the document I currently am working on I am using the enumitem package, already.

3
  • Are the trailing 0 important?
    – user31729
    Commented Apr 18, 2016 at 15:51
  • I'd like them to keep everything with 2 digits, although they could be dropped if the solution would get too complicated Commented Apr 18, 2016 at 15:53
  • I think, this can be achieved!
    – user31729
    Commented Apr 18, 2016 at 15:55

4 Answers 4

2

An 'awful' mix with enumitem and expl3 features, defining a new counter formater named baseenum -- I've to test with deeper level nesting however.

\documentclass{article}

\usepackage{enumitem}
\usepackage{xparse}
\makeatletter


\def\basetwoenum#1{\expandafter\@basetwoenum\csname c@#1\endcsname}

\ExplSyntaxOn
\def\@basetwoenum#1{%
  \int_set:Nn \l_tmpa_int \c_one
  \int_step_inline:nnnn {2}{1}{\number#1}{%
    \int_set:Nn \l_tmpb_int { 2 * \l_tmpa_int }
    \int_set_eq:NN \l_tmpa_int \l_tmpb_int
  }
  \int_use:N \l_tmpa_int
}
\ExplSyntaxOff
\AddEnumerateCounter{\basetwoenum}{\@basetwoenum}{100}
\makeatother 

\begin{document}

\begin{enumerate}[label={\basetwoenum*}]
  \item First
  \item Second
  \item Third
  \item Fourth
  \item Fifth
\end{enumerate}

Resume it

\begin{enumerate}[label={\basetwoenum*},resume]
  \item First
  \item Second
  \item Third
  \item Fourth
  \item Fifth
\end{enumerate}


\end{document}

enter image description here

4
  • In your answer, I see the line \int_set:Nn \l_tmpb_int { 2 * \l_tmpa_int }. Is this what defines "power of 2" to be used? Switching that 2 * to a n * would allow me to use whatever power of n I chose? Commented Apr 18, 2016 at 17:26
  • @GuilhermeVargas: Yes, but in this case I would suggest some other setup, to make it more user - friendly
    – user31729
    Commented Apr 18, 2016 at 17:27
  • Something like in the other answer that is a variant to your solution? Just curious Commented Apr 18, 2016 at 17:30
  • @GuilhermeVargas: Well, egreg did provide for expansion -- if you need \label or \ref his solution is better, of course
    – user31729
    Commented Apr 18, 2016 at 17:31
3

A variant of Christian Hupfer's solution, but fully expandable:

\documentclass{article}

\usepackage{enumitem}
\usepackage{xparse}

\ExplSyntaxOn
\DeclareExpandableDocumentCommand{\basetwoenum}{m}
 {
  \basetwoenum_main:n { #1 }
 }
\cs_new:Nn \basetwoenum_main:n
 {
  \exp_args:Nc \basetwoenum_eval:n { c@#1 }
 }
\cs_new:Nn \basetwoenum_eval:n
 {
  \fp_eval:n { 2 ** (#1-1) }
 }
%% for keeping enumitem happy
\cs_set_eq:cN { @basetwoenum } \basetwoenum_main:n
\ExplSyntaxOff

\makeatletter
\AddEnumerateCounter{\basetwoenum}{\@basetwoenum}{000}
\makeatother 

\begin{document}

\begin{enumerate}[label={\basetwoenum*}]
  \item First
  \item Second
  \item Third
  \item Fourth
  \item Fifth
\end{enumerate}

Resume it

\begin{enumerate}[label={\basetwoenum*},resume]
  \item First
  \item Second
  \item Third
  \item Fourth
  \item Fifth
\end{enumerate}

\end{document}

This gives correct results up to 253; of course, if you plan to use more than ten items you have to accordingly increase the number of zeros in the final argument to \AddEnumerateCounter.

enter image description here

3
  • Is an expandable solution really an issue here? If the O.P. requires values to be used in other macros )or calculation, I see the point, of course, but for mere typesetting?
    – user31729
    Commented Apr 18, 2016 at 16:38
  • @ChristianHupfer \label and \ref?
    – egreg
    Commented Apr 18, 2016 at 16:39
  • Agreed!!!!!!!!!!!!!
    – user31729
    Commented Apr 18, 2016 at 16:40
3
\documentclass{article}

\usepackage{tikz}

\renewcommand*{\labelenumi}{\pgfmathparse{int(2^(\theenumi-1))} \pgfmathresult}


\begin{document}
    \begin{enumerate}
        \item One
        \item Two
        \item Three
        \item more
        \item more
        \item more
        \item more
        \item more
        \item more
        \item more
        \item more
        \item more
        \item more
    \end{enumerate}
\end{document}

enter image description here

1
  • Please add a couple of descriptive sentences that explain the main aspects of your solution.
    – Mico
    Commented Apr 18, 2016 at 21:42
3

Here's a LuaLaTeX-based solution, which works with the enumitem package. It defines an enumerated environment called powertwoenum, in which consecutive items are numbered as 1, 2, 4, 8, 32, etc. Items in a powertwoenum list may be cross-referenced via the usual \label-\ref mechanism.

enter image description here

% !TEX TS-program = lualatex
\documentclass{article}
\newcommand\powertwo{\directlua{tex.sprint(2^(\arabic{powertwoenumi}-1))}}
\usepackage{enumitem}
\newlist{powertwoenum}{enumerate}{1}
\setlist[powertwoenum]{label=\protect\powertwo.,ref=\protect\powertwo}


\begin{document}
\begin{powertwoenum}
  \item First
  \item Second
  \item Third
  \item Fourth
  \item Fifth \label{item:five}
\end{powertwoenum}

A cross-reference to item \ref{item:five}.

Resume the enumerated list.

\begin{powertwoenum}[resume]
  \item First
  \item Second
  \item Third
  \item Fourth
  \item Fifth
\end{powertwoenum}
\end{document}
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .