10

I have an enumerate environment that I want to refer to later in my document using \ref and \label, but enumerate doesn't get numbered by section (the way, say, figure would). How can I add section numbers to enumerate?

I do not want individual, enumerated items to be numbered. Rather, I want the enumeration as a whole to be numbered.

What I am really looking for is something like this:


Algorithm 1.2

  • 1. Walk to store.
    2. Buy Milk
    3. Walk Home
  • ... /snip/ ...

    As described in Algorithm 1.2, it's easy to buy milk.


    I don't want to use a figure because I can't have it moving around the document. I don't want to use the theorem/definition/proof environments because of all the styling. The algorithm environments use a lot of styling too. I'm basically looking for a way to slap a title on my enumerate environment and give it a number that I can refer to.

    Edit:

    This is what I finally ended up with, based on Mico's post:

    \usepackage{amsmath}
    \newcounter{algcounter}
    \newenvironment{alg}{
    \bigskip\noindent
    \refstepcounter{algcounter}
    \textbf{Algorithm \thealgcounter}
    \newline
    \begin{enumerate}
      \setlength{\itemsep}{1pt}
      \setlength{\parskip}{0pt}
      \setlength{\parsep}{0pt}
    }{\end{enumerate}}
    \numberwithin{algcounter}{section}
    
    9
    • 4
      Next time, please state your full requirements at the start. That'll save potential helpers some work.
      – lockstep
      Feb 5, 2012 at 15:39
    • Can you elaborate why you don't want to use a theorem environment. I think what you are asking is a specific type of a theorem. It is relatively easy to change the style of theorem, for example, by using the ntheorem package.
      – Aditya
      Feb 5, 2012 at 15:44
    • 1
      @Jake: Do you want to use the algorightm-environment. Please provide a minimal example. Feb 5, 2012 at 16:06
    • 2
      @MarcoDaniel: I see your first name is being (mis)spelled as "Macro". Welcome to the club -- my first name frequently gets misspelled "Micro".
      – Mico
      Feb 5, 2012 at 18:38
    • 1
      @Mico: We have difficult names ;-) Feb 5, 2012 at 19:03

    2 Answers 2

    12

    If you're willing to load the amsmath package (you may be doing so already anyway...), you can just issue the command

    \numberwithin{enumi}{section}
    

    to have level-1 enumerations be subordinated to the section.

    Addendum. From your follow-up remarks, it would appear that you're not trying to change the appearance of LaTeX's own enumerate environment but, instead, want to create a custom environment that will be numbered within sections. (I.e., each time a \section command is encountered, numbering should start over at "1"; however, the section number needs to be prefixed to the environment number.) The following MWE illustrates how this may be achieved.

    \documentclass{article}
    \usepackage{amsmath} % needed for its \numberwithin command
    \newcounter{myalgctr}
    
    \newenvironment{myalg}{%      define a custom environment
       \bigskip\noindent%         create a vertical offset to previous material
       \refstepcounter{myalgctr}% increment the environment's counter
       \textsc{Algorithm \themyalgctr}% or \textbf, \textit, ...
       \newline%
       }{\par\bigskip}  %          create a vertical offset to following material
    \numberwithin{myalgctr}{section}
    
    \begin{document}
    \section{First section}
    
    Some text before the first algorithm environment \ldots
    
    \begin{myalg}
    Some thoughts\ldots \label{alg:first}
    \end{myalg}
    
    \begin{myalg}
    More thoughts\ldots \label{alg:second}
    \end{myalg}
    
    Some text after the second algorithm environment \ldots
    
    \section{Second section}
    
    \begin{myalg}
    Further thoughts\ldots \label{alg:third}
    \end{myalg}
    
    \section{Third section}
    
    As we showed in algorithms \ref{alg:first}, \ref{alg:second}, 
    and \ref{alg:third}, \ldots 
    \end{document}
    

    enter image description here

    4
    • Similarly to @Macro-Daniel's answer, I need numbering for the environment as a whole, not the individual items.
      – Jake
      Feb 5, 2012 at 15:37
    • @Jake: I've provided an addendum to my answer. I now interpret your question as pertaining to a custom environment that is to be numbered within sections.
      – Mico
      Feb 5, 2012 at 18:30
    • How does one make \newenvironment play well with fancyhdr? Right now it doesn't pick up the section mark at all from environments defined this way. May 1, 2020 at 15:48
    • @level1807 - I'm very sorry, but my mind-reading skills are absolutely worthless. I also have no idea how your query relates to anything in the answer above, which I wrote more than 8 years ago. May I suggest that you post a new query, in which you would lay out which issues you're encountering and what you've tried so far? Ideally, your query would be accompanied by a [Minimum (Non-)Working Example]((meta.tex.stackexchange.com/q/228/5001)). A new query has the very important benefit of being seen by far more people than a comment below an 8-year-old posting.
      – Mico
      May 1, 2020 at 15:59
    9

    EDIT:

    Based on the new informations of the comments I provide a new answer.

    To type an algorithm you can work the the package algorihtm. By using the command numberwithin (provided by amsmath) you can set the counter:

    \documentclass[a4paper,11pt]{article}
    \usepackage{algorithmicx}
    \usepackage{algorithm}
    \usepackage{algpseudocode}
    \usepackage{amsmath}
    \numberwithin{algorithm}{section}
    \begin{document}
    \section{foo}
    \begin{algorithm}
    \caption{Algorithm}\label{algo}
    
    \begin{algorithmic}[1]
    \Require $x\ge5$
    \Ensure $x\le-5$
    \Statex
    \While{$x>-5$}
    \State $x\gets x-1$
    \EndWhile
    \end{algorithmic}
    \end{algorithm}
    \ref{algo}
    \end{document}
    

    enter image description here

    To manipulate list environments like enumerate I really recommend the package enumitem. The package provides the key ref to manipulate the format of references.

    Here an example:

    \documentclass{article}
    
    \usepackage{enumitem}
    \setlist[enumerate,1]{ref=\thesection.\theenumi}
    
    \begin{document}
    \section{foo}
    Text
    \begin{enumerate}
    \item \label{enum1} foo
    \item \label{enum2} bar
    \end{enumerate}
    
    \ref{enum1} and \ref{enum2}
    \end{document}
    

    enter image description here

    2
    • Please consider to add a variant using label and resume, so I can delete my answer.
      – lockstep
      Feb 5, 2012 at 15:22
    • This is close but not what I need...I edited my question to be a bit clearer
      – Jake
      Feb 5, 2012 at 15:33

    You must log in to answer this question.

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