87

I'm doing homework from a particular book, and each question is numbered after the page number and the question number. For example 52.2, 52.9, 53.13, etc. (Note that they are not sequential.)

How do I do something that will work like the enumerate environment, but instead of 1. 2. 3., etc. at each \item, I put a custom number?

Tobi
  • 56,353

6 Answers6

123

You could simply use the optional Argument of \item

\documentclass{scrartcl}

\begin{document}
\begin{itemize}
    \item [2.9] Foo
    \item [53.2] Bar
    \item [69.11]
\end{itemize}
\end{document}

I hope that’s what you wanted to do …

Tobi
  • 56,353
  • 1
    Thanks. Strangely enough, I wasn't aware of the itemize function :O ... useful! – iDontKnowBetter Sep 29 '11 at 00:43
  • 3
    That's what I usually do, but the alignment works the other way I expect it to and I often have to fix it by defining my own lists. Any other means to do it? – François G. Dorais Sep 29 '11 at 01:12
  • 1
    @Francois: Please post a separate question with a MWE that shows the problem. – Peter Grill Sep 29 '11 at 01:31
  • 7
    You can also do this using the enumerate environment (that is, in this instance, itemize and enumerate can be interchanged). Also, from a terminology perspective, these are environments. – Werner Sep 29 '11 at 01:40
  • If I do this with something long like [Question], the allignment gets messed up. However this is fixed if I change the environment to "description" – geo909 Jan 24 '15 at 21:23
  • 2
    @geo909: This is because the labels, which is “Question” in your case, have a fixed width. If you have such list {description} seems to be more appropriate. You can also increase the label width e.g. by using the enumitem package. – Tobi Jan 25 '15 at 11:34
  • But this way, the \ref command does not show the correct label. How should we fix this? – user2178095 Oct 07 '21 at 11:32
  • I guess this is impossible since {itemize} doesn't save any labels by default … maybe you ask a new question referring to this on ;-) – Tobi Oct 11 '21 at 22:02
  • Is it possible to label them with the numbers inside? – alper Jan 18 '22 at 15:21
  • What do you mean with “numbers inside“. I suggest you post a follow up question and link to this one. – Tobi Feb 05 '22 at 23:02
32

The genuinely insane way to do this is as follows:

\documentclass{article}
\usepackage{enumitem}
\makeatletter
\newcommand*{\wackyenum}[1]{%
  \expandafter\@wackyenum\csname c@#1\endcsname%
}

\newcommand*{\@wackyenum}[1]{%
  $\ifcase#1\or52.2\or52.9\or53.13\or42%
    \else\@ctrerr\fi$%
}
\AddEnumerateCounter{\wackyenum}{\@wackyenum}{53.13}
\makeatother


\begin{document}
\begin{enumerate}[label=\wackyenum*]
\item One
\item Two
\item Three
\end{enumerate}
\end{document}

This allows you to define an arbitrary list of numbers that the the enumeration will cycle through. This has the (very minor) advantage of having the spacing behave a little better. This is basically how a lot of things are done in the moreenum package. The documentation explains the procedure.

Seamus
  • 73,242
18

Depending on how much "non-sequential" your numbers are, and how much you need a quick & dirty solution instead of something with polish and automatism, you might get along with \addtocounter and some ad-hoc-ery involving the enumitem package:

\documentclass{article}

\usepackage{enumitem}

\begin{document}

\begin{enumerate}[label=52.\arabic*,start=2]
\item Answer % 52.2
\addtocounter{enumi}{6}
\item Answer % 52.9
\end{enumerate}

\begin{enumerate}[label=53.\arabic*,start=13]
\item Answer % 53.13
\end{enumerate}

\end{document}

With a bit more work, you could make the page number (52, 53) into a counter of its own and wrap it all into a re-useable environment definition, but I think this gives you the idea of it all.

DevSolar
  • 7,857
6

Building up on Seamus's insane answer, here is how to do the same in ConTeXt.

\defineconversion[insane][52.2, 52.9, 53.13,42]

\starttext
\startitemize[insane]
  \item One
  \item Two
  \item Three
\stopitemize
\stoptext
Aditya
  • 62,301
4

To get automatically the page number you can do as follows

\documentclass[a4paper]{article}
\usepackage{enumitem,refcount}
\newcounter{qcount}
\newenvironment{questions}
  {\refstepcounter{qcount}\label{qcount\theqcount}%
   \begin{itemize}[label=\protect\getpagerefnumber{qcount\theqcount}.\protect\qcountnum]}
  {\end{itemize}}
\newcommand{\qitem}[1]{\def\qcountnum{#1}\item}

\begin{document}

\begin{questions}
\qitem{2} A
\qitem{25} B
\end{questions}

\newpage

\begin{questions}
\qitem{3} C
\qitem{7} D
\end{questions}

\end{document}

At each questions environment a new label is generated, so we can use it for numbering the questions. The \qitem command wants as argument the question number. Since the mechanism exploits the \label-\ref system, two runs of latex are needed.

egreg
  • 1,121,712
3

In modification of egreg's solution you may use package perpages to have enumeration per page:

\documentclass{article}
\usepackage{perpage}
\MakePerPage{enumi}
\renewcommand*{\theenumi}{\theperpage.\arabic{enumi}}
\newcommand*{\nitem}[1][]{%
  \ifx\relax#1\relax\else\setcounter{enumi}{\numexpr #1-1\relax}\fi
  \item
}
\usepackage{lipsum}

\begin{document}

\makeatletter\@whilenum\value{page}<100\do{% demonstration of 99 pages
  \lipsum[\thepage]
  \begin{enumerate}
  \nitem A
  \nitem[7] B
  \nitem[19] C
  \nitem \lipsum[\thepage]
  \nitem D
  \end{enumerate}
  \newpage
}
\end{document}

Instead of redefining theenumi you may redefine \labelenumi:

\renewcommand*{\labelenumi}{\theperpage.\theenumi}

The difference is that \label/\ref will reference the number without page.

Schweinebacke
  • 26,336