1

I need a counter that returns my enumerate level. The following code works. But there are two problems. First I want it to work for the beamer class, but when I uncomment the first line and comment out the second line, the code throws an error. Second I want a counter, not a value, which is why I have the line \setcounter{depthCtr}{\depthLevel} command after I've retrieved the level using depthLevel. Obviously, I want to avoid this circuitous method and construct a macro that returns the counter directly. But my commented out construction \depthMacro throws an error. Could somebody please explain how to construct \depthMacro correctly, and how to make it work with the beamer class?

%\documentclass{beamer}
\documentclass{article}
\newcounter{depthCtr}
\makeatletter
\newcommand{\depthLevel}{\the\@enumdepth}
\makeatother

%\makeatletter
%\newcommand{depthMacro}{
%   \setcounter{depthCtr}{\the\@enumdepth}
%   \thedepthCtr}
%\makeatother

\begin{document}
This is the base level \depthLevel
\begin{enumerate}
    \item This is the level  \depthLevel
    \begin{enumerate}
        \item This is the level  \depthLevel
        \begin{enumerate}
            \item This is the level  \depthLevel
            \setcounter{depthCtr}{\depthLevel}
            \item This is the level \thedepthCtr
%           \item This is the level  \depthMacro
        \end{enumerate}
    \end{enumerate}
\end{enumerate}
\end{document}
  • 1
    You're approach works if you use \begin{frame}...\end{frame}, i.e. use the enumerate environment in a frame environment – user31729 Sep 22 '17 at 20:26
  • Can you give a bit more background information? For what purpose do you need the enumerate level? – user36296 Sep 22 '17 at 21:21
  • Please see my response to @David_Carlisle below. He asked the same question. – Leo Simon Sep 23 '17 at 17:02
2

It appears what you are looking for is simply LaTeX's own \@listdepth.

\documentclass{beamer}

\newcounter{depthCtr}
\makeatletter
\newcommand{\depthLevel}{\the\@enumdepth}
\let\beamer@cramped\relax% just because
\let\c@depthCtr\@enumdepth % make a latex counter version

% HERE!
\newcommand{\mybetterdepthLevel}{\the\@listdepth}
\makeatother



\begin{document}
This is the base level \depthLevel

This is the base level \thedepthCtr

This is the base level \mybetterdepthLevel

\begin{enumerate}
    \item This is the level  \depthLevel
    \item This is the level  \thedepthCtr
    \item Yes, this is the level  \mybetterdepthLevel
    \begin{itemize}
        \item This is the level  \depthLevel
        \item This is the level  \thedepthCtr
        \item No! this is in fact the level  \mybetterdepthLevel
        \begin{enumerate}
            \item This is the level  \depthLevel
            \item This is the level  \thedepthCtr
            \item No! this is in fact the level  \mybetterdepthLevel
            \item Can't go deeper :-( (too deeply nested error)
        \end{enumerate}
    \end{itemize}
\end{enumerate}
\end{document}

enter image description here

If you really really want a LaTeX counter, just use

% HERE!
\let\c@mylistdepth\@listdepth
\newcommand{\mybetterdepthLevel}{\arabic{mylistdepth}}

or perhaps rather

\let\c@mylistdepth\@listdepth
\newcommand{\themylistdepth}{\the\value{mylistdepth}}
\newcommand{\mybetterdepthLevel}{\themylistdepth}

so that you can really use mylistdepth as a full LaTeX counter.

3

enter image description here

\documentclass{beamer}

\newcounter{depthCtr}
\makeatletter
\newcommand{\depthLevel}{\the\@enumdepth}
\let\beamer@cramped\relax% just because
\let\c@depthCtr\@enumdepth % make a latex counter version
\makeatother



\begin{document}
This is the base level \depthLevel

This is the base level \thedepthCtr

\begin{enumerate}
    \item This is the level  \depthLevel
    \item This is the level  \thedepthCtr
    \begin{enumerate}
        \item This is the level  \depthLevel
        \item This is the level  \thedepthCtr
        \begin{enumerate}
            \item This is the level  \depthLevel
            \item This is the level  \thedepthCtr
        \end{enumerate}
    \end{enumerate}
\end{enumerate}
\end{document}
  • Thanks very much @David_Carlisle. I should have generalized my MWE the first time around. I want the counter to work with a mixture of itemize and enumerate levels, so somehow I have to add \@enumdepth to \@itemdepth to get the right answer. But I can't seem to manage integer arithmetic. Could you tell me how I would do that? Sorry for not asking the first time around. – Leo Simon Sep 23 '17 at 1:08
  • @LeoSimon it's not at all clear why you need a latex counter version, as opposed to say \makeatletter\def\zzz{\numexpr\@enumdepth+\@itemdepth\relax} then use \the\zzz to print the value ir \ifnum\zzz=6 I'm getting too deep\fi in the document – David Carlisle Sep 23 '17 at 7:54
  • my original intent was to pass the counter as an optional <> argument to \hypertarget identifying the level that I was at, but this turned out to be misguided. I'm now using an ugly \ifthenelse construction which most likely didn't need a counter. But it was a learning experience! – Leo Simon Sep 23 '17 at 17:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.