0

On the question Missing number, treated as zero when renewing enumerate \item and using hyperref I learned I have to define my custom commands from the question How to recognize the enumerate shortlabels in my custom environment? before the package hyperref to be loaded.

However, my thesis should use the class abntex2 and the abntex2 class got the brilliant and extraordinary idea of including the package hyperref on their source code, without any practical reason.

Now, I got no idea how I can include the package hyperref after defining my code. So far I tried using \RequirePackage{enumitem} and defining my command before loading the abntex2 class. However, LaTeX threw the error

enumitem.sty:1587: LaTeX Error: Environment description undefined.

\PassOptionsToPackage{shortlabels}{enumitem}
\RequirePackage{enumitem}
\let\itemenumerateoptional\item
\newcounter{enumerateoptionalcount}

\def\renewenumerateoptionalitem{%
  \let\itemenumerateoptional\item
  \renewcommand{\item}[1][]{%
      \refstepcounter{enumerateoptionali}% increment the counter
      \itemenumerateoptional[\bfseries##1~\theenumerateoptionali]%
  }%
}
\newlist{enumerateoptional}{enumerate}{1}
\setlist[enumerateoptional]{
    before=\renewenumerateoptionalitem,
    label=\arabic*,
    nosep,
    labelindent=20mm,
    leftmargin=*,
    after=\let\item\itemenumerateoptional,
}
\documentclass{abntex2}

\begin{document}
\begin{enumerateoptional}[1.]
   \item[Some first] item one
   \item[Some second] item two
\end{enumerateoptional}
\end{document}
Babel <3.12> and hyphenation patterns for 76 language(s) loaded.
(D:\latex\enumitem.sty

D:\latex\enumitem.sty:1587: LaTeX Error: Environment description undefined.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help
D:\latex\enumitem.sty:1587:  ==> Fatal error occurred, no output PDF file produced!

Related questions

  1. Using later-defined commands in an own document class
  2. How to override command on my preamble, after its redefinition by my class with renewcommand?
  3. Hyperref is loaded by the class, and I need to load packages that are supposed to be loaded before
  4. when using hyperref package undefined control sequence \chapter error
  5. Which packages should be loaded after hyperref instead of before?
  6. Using later-defined commands in an own document class
2

Redefine \H@item instead of \item. That should work even if hyperref is loaded too early:

\documentclass[12pt]{article}
\errorcontextlines 10000
\usepackage{hyperref}

\usepackage[shortlabels]{enumitem}

\makeatletter
\def\renewenumerateoptionalitem{%
  \let\itemenumerateoptional\H@item
  \renewcommand{\H@item}[1][]{%
      \refstepcounter{enumerateoptionali}% increment the counter
      \itemenumerateoptional[\bfseries##1~\theenumerateoptionali]%
  }%
}
\makeatother
\newlist{enumerateoptional}{enumerate}{1}
\setlist[enumerateoptional]{
    before=\renewenumerateoptionalitem,
    label=\arabic*,
    nosep,
    labelindent=20mm,
    leftmargin=*,
}

\begin{document}
\begin{enumerateoptional}[1.]
   \item[Some first] item one \label{test}
   \item[Some second] item two
   \item abc
\end{enumerateoptional}
\ref{test}
\end{document}
  • Everything works under MikTex on Windows, but when compiling this with Texlive 2017 on Linux, latex says \ref{test}: Undefined control sequence. – user Oct 21 '19 at 1:40

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.