1
\newenvironment{Group}[1]{
 \newcounter{#1}
}

but if I want to increase the counter, I have to specify the counter.

\begin{Group}{derp}
  \arabic{derp}
  \stepcounter{derp}
  \arabic{derp}
\end{Group}

I want to be able to do:

\being{Group}{foo}
  \IncAndShowCounter % keep a counter for this group only
\end{Group}
1
  • 1
    Welcome to TeX SE! I don't totally understand what you're trying to do. Do you want a single counter that's accessible inside each instance of your environment or do you want a counter per instance? Please add a minimal working example (MWE) that illustrates your problem.
    – TH.
    Jun 23, 2017 at 4:34

1 Answer 1

3

As I said in the comment, I'm not sure exactly what you want to do, so here are two possibilities. In both cases, there's a \IncAndShowCounter macro that does what I think you want. But it's done differently for each case.

One counter per environment, starting at 0.

Here, I allocate a single counter named Group and set it to 0 at \begin{Group}.

\documentclass{article}
\newcounter{Group}
\newenvironment{Group}{%
    \setcounter{Group}{0}%
}{}

\newcommand*\IncAndShowCounter{%
    \stepcounter{Group}%
    \arabic{Group}%
}

\begin{document}

\begin{Group}
\IncAndShowCounter\ first

\IncAndShowCounter\ second

\IncAndShowCounter\ third
\end{Group}

\begin{Group}
\IncAndShowCounter\ A

\IncAndShowCounter\ B

\IncAndShowCounter\ C
\end{Group}
\end{document}

enter image description here

You can see that the counter resets inside each environment.

One shared counter per type of Group.

Here, \begin{Group}{foo} will create a counter foo that will be accessible inside all such environments. \begin{Group}{bar} will have an unrelated counter.

\documentclass{article}
\newenvironment{Group}[1]{%
    \expandafter\ifx\csname c@#1\endcsname\relax
        \newcounter{#1}%
    \fi
    \newcommand*\IncAndShowCounter{%
        \stepcounter{#1}%
        \arabic{#1}%
    }%
}{}


\begin{document}

\begin{Group}{foo}
\IncAndShowCounter\ first

\IncAndShowCounter\ second

\IncAndShowCounter\ third
\end{Group}

\begin{Group}{bar}
\IncAndShowCounter\ x

\IncAndShowCounter\ y

\IncAndShowCounter\ z
\end{Group}

\begin{Group}{foo}
\IncAndShowCounter\ A

\IncAndShowCounter\ B

\IncAndShowCounter\ C
\end{Group}

\end{document}

enter image description here

Notice how the counter for the second \begin{Group}{foo} continues from the first.

3
  • Still have a bit of difficulty tho. I want to have each entry on the menu be prefixed with a counter, and a letter. So maybe entres would be E1... E6 and soups S1...S6 sharelatex.com/project/594cbdf1097fd8116d21b99e
    – Daniel
    Jun 25, 2017 at 21:21
  • Have the group environment take a second parameter that's the letter that you want and change the \Entry macro to display the letter and the counter value.
    – TH.
    Jun 26, 2017 at 2:53
  • That's what I ended up doing. thank you much
    – Daniel
    Jul 3, 2017 at 16:33

You must log in to answer this question.

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