T-F-S / tcolorbox

A LaTeX package to create highly customizable colored boxes.
http://www.ctan.org/pkg/tcolorbox
LaTeX Project Public License v1.3c
213 stars 15 forks source link

More meaningful error message in case of `\Declare(d)TColorBox`(es) with same name and both with `auto counter` #276

Open dbitouze opened 3 months ago

dbitouze commented 3 months ago

The following MCE:

\documentclass{article}
\usepackage{tcolorbox}
\DeclareTColorBox[auto counter]{pabox}{}{}
\DeclareTColorBox[auto counter]{pabox}{}{}
\begin{document}
\end{document}

fails to compile with the following cryptic message:

! LaTeX Error: Command \c@tcb@cnt@pabox already defined. Or name \end... illegal, see p.192 of the manual.

Maybe it is worth to provide a more meaningful message in such a case.

muzimuzhi commented 3 months ago

The error was raised by \newcounter{<conter name>} on an existing <counter name>. Not sure if it's feasible for LaTeX2e to make that general message more specific for counters.

Possible patch on tcolorbox side

\documentclass{article}
\usepackage{tcolorbox}

\makeatletter
\def\tcb@proc@counter@auto#1{%
  %% begin patch
  \ifcsdef{c@tcb@cnt@#1}
    {\tcb@error{Auto counter for environment '#1' already defined}{}}
    {}%
  %% end patch
  \newcounter{tcb@cnt@#1}%
  \csxdef{tcb@cnt@#1}{tcb@cnt@#1}%
  \tcb@proc@counter@autoanduse{#1}%
}
\makeatother

\DeclareTColorBox[auto counter]{pabox}{}{}
\DeclareTColorBox[auto counter]{pabox}{}{}
\begin{document}
\end{document}

Now compile it will throw two errors

! Package tcolorbox Error: Auto counter for environment 'pabox' already defined
.

! LaTeX Error: Command \c@tcb@cnt@pabox already defined.
               Or name \end... illegal, see p.192 of the manual.
T-F-S commented 3 months ago

I think this is a useful patch and I will add it to the next version. Thank you.