2

I would like to generate text based on my current depth of the enumeration. After a little search, I found this. It seems ifthenelse is not respecting enumdepth properly. Am I doing something wrong?

Working example:

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

The output is:

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)

The expected output is:

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)

-- Mike

  • 2
    Can you provide a complete, compiling, working example? – Steven B. Segletes Sep 18 '17 at 11:52
  • 2
    you should always provide a full example, for example have you used this code at a point where @ may be used in command names (eg inside a package file) – David Carlisle Sep 18 '17 at 11:54
  • Edited the original post and added working example! – Mike V.D.C. Sep 18 '17 at 12:20
  • 1
    I agree with both above comments. I just adding that if you use this code in your preamble you possibly need to put the command using it inside \makeatletter and \makeatother – koleygr Sep 18 '17 at 12:21
  • @koleygr, modifying the line to \makeatletter\@enumdepth\makeatother is not making change! Do I need to use them (these two commands) some-where-else? Adding it at the point where \curdepth is called also doesnt help! Sorry if I am doing something stupid! – Mike V.D.C. Sep 18 '17 at 12:27
2

As \@enumdepth is equal to a TeX count, you should add \the to make it work (and, of course, makeat...).

\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}
3
\documentclass{article}
\makeatletter
\newcommand\curdepth[1]{\small Current counter is \theenumi%
    \ifnum\@enumdepth>\@ne ~(\theenumii)\fi}
\makeatother
  • Way too simple... ;) – TeXnician Sep 18 '17 at 12:56
1

By default, \ifthenelse performs numeric tests, so \@enumdepth=1 is sufficient; the \equal test is about strings.

You also need to surround the \newcommand with \makeatletter and \makeatother, because you need to use a macro with @ in its name.

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

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.