`enumdepth` 和 `ifthenelse`

`enumdepth` 和 `ifthenelse`

我想根据我当前的枚举深度生成文本。经过一番搜索,我发现。这似乎ifthenelse没有得到enumdepth适当的尊重。我做错了什么吗?

工作示例:

\documentclass{article}
\usepackage{ifthen}
\newcommand{\curdepth}[1] %
               { 
                  \ifthenelse{\equal{\@enumdepth}{1}}
                  {\small Current counter is \theenumi}
                  {\small Current counter is \theenumi~(\theenumii)}
               }

\begin{document}
\begin{enumerate}
\item This is the first item in the list. \curdepth

\item Sample
  \begin{enumerate}
  \item This is the first item in the list. \curdepth

  \end{enumerate}
\end{enumerate}
\end{document}

输出为:

1. This is the first item in the list. Current counter is 1 ()
2. Sample
(a) This is the first item in the list. Current counter is 2 (a)

预期输出为:

1. This is the first item in the list. Current counter is 1
2. Sample
(a) This is the first item in the list. Current counter is 2 (a)

-- 迈克

答案1

由于\@enumdepth等于 TeX 计数,因此您应该添加\the以使其工作(当然,还要添加...)。

\documentclass{article}
\usepackage{ifthen}
\makeatletter
\newcommand{\curdepth}[1]{ 
                  \ifthenelse{\equal{\the\@enumdepth}{1}}
                  {\small Current counter is \theenumi}
                  {\small Current counter is \theenumi~(\theenumii)}
               }
\makeatother

\begin{document}
\begin{enumerate}
\item This is the first item in the list. \curdepth

\item Sample
  \begin{enumerate}
  \item This is the first item in the list. \curdepth

  \end{enumerate}
\end{enumerate}
\end{document}

答案2

\documentclass{article}
\makeatletter
\newcommand\curdepth[1]{\small Current counter is \theenumi%
    \ifnum\@enumdepth>\@ne ~(\theenumii)\fi}
\makeatother

答案3

默认情况下,\ifthenelse执行数字测试,所以\@enumdepth=1就足够了;\equal测试是关于字符串的。

您还需要用 和 括住\newcommand\makeatletter因为您需要使用名称中\makeatother包含 的宏。@

\documentclass{article}
\usepackage{ifthen}

\makeatletter
\newcommand{\curdepth}[1]{% <-- don't forget
  \ifthenelse{\@enumdepth=1}
    {\small Current counter is \theenumi}
    {\small Current counter is \theenumi~(\theenumii)}% <-- don't forget
}
\makeatother

\begin{document}
\begin{enumerate}
\item This is the first item in the list. \curdepth

\item Sample
  \begin{enumerate}
  \item This is the first item in the list. \curdepth

  \end{enumerate}
\end{enumerate}
\end{document}

相关内容