[Tex/LaTex] Defining a custom environment with per-section numbering

environmentsnumberingsectioning

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}
    

    Best Answer

    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

    Related Question