定义一个等于 \@enumdepth 的计数器

定义一个等于 \@enumdepth 的计数器

我需要一个返回枚举级别的计数器。以下代码有效。但有两个问题。首先,我希望它适用于该类beamer,但当我取消注释第一行并注释掉第二行时,代码会引发错误。其次,我想要一个计数器,而不是一个值,这就是为什么我\setcounter{depthCtr}{\depthLevel}在使用检索级别后有行命令depthLevel。显然,我想避免这种迂回的方法并构造一个直接返回计数器的宏。但我注释掉的构造\depthMacro会引发错误。有人可以解释如何\depthMacro正确构造以及如何使其与beamer类一起工作吗?

%\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

看起来您正在寻找的只是 LaTeX 自己的\@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}

在此处输入图片描述

如果你真的想要一个 LaTeX 计数器,只需使用

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

或者更确切地说

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

这样您就可以真正地将其用作mylistdepth完整的 LaTeX 计数器。

答案2

在此处输入图片描述

\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}

相关内容