1

I have trouble defining a custom environment for a tcolorbox equation environment. When I define an environment for my tcolorbox with ams align Option I get the error:

LaTeX Error: \begin{tcolorbox} on input line 494 ended by \end{myenv}. (\end{myenv} 

With the answer from LaTeX Error: \begin{tcolorbox} on input line x ended by \end{myenv} when using new environment I thought I could solve it. But while the example from this question works, mine doesn't.

So it works to use this environment

 \newenvironment{myenv}
 {\tcolorbox[hbox]}
   {\endtcolorbox}

but when I use this:

\newenvironment{myenv}
{\tcolorbox[ams align]}
  {\endtcolorbox}

I get the above error. What is wrong and how can I solve this?

4
  • 1
    Why not use \newtcolorbox{myenv}{ams align} to define the new environment?
    – leandriis
    Apr 20 at 12:38
  • I need to define an alternative environment to the tcolorbox-environment, for a case where I cannot compile it while loading the tcolorbox package... But I guess that would work. Still the question remains, why this occurs in the first place
    – Philipp
    Apr 20 at 12:50
  • The trouble is that even using (correctly!) the macro form \tcolorbox the variable \@currenvir containing the name of the current environment is overwritten.
    – campa
    Apr 20 at 13:00
  • But it works with option 'hbox'?
    – Philipp
    Apr 20 at 13:05

1 Answer 1

2

When starting an environment foo by writing \begin{foo} a couple of things happen; among these, the current environment's name is stored in the macro \@currenvir, and then the macro \foo is expanded. That's the reason why when defining wrappers for environments it's always a good idea to use the internal macros \foo and \endfoo: in this way you (usually) get clearer error messages.

However, tcolorbox does things differently, and the macro \tcolorbox (more exactly, some other macro a couple of expansions later) also sets \@currenvir:

\documentclass[twocolumn]{article}% twocol for smaller snapshot

\usepackage{tcolorbox}

\begin{document}

\csname@currenvir\endcsname
\begin{center}
\csname@currenvir\endcsname
\end{center}
\csname@currenvir\endcsname
\begin{tcolorbox}
\csname@currenvir\endcsname
\end{tcolorbox}

\end{document}

enter image description here

I don't quite know the rationale behind this choice, but I guess that's (also) why a tcolorbox version of \newenvironment is provided, namely \newtcolorbox:

\documentclass{article}

\usepackage{tcolorbox}
\tcbuselibrary{theorems}

\newtcolorbox{myenv}{ams align}

\begin{document}

\begin{myenv}
a &= b & c&= d \\
e &= f
\end{myenv}

\end{document}

enter image description here

2
  • Thanks for this detailed explanation. But does it explain why it works with one option but not with the other?
    – Philipp
    Apr 21 at 15:50
  • 1
    @Philipp The mechanism behind hbox is completely different. \hbox isn't looking for \end{something}, so the issue does not present itself in this case.
    – campa
    Apr 21 at 16:02

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.