4

I have to define a new environment doing something like that.

\documentclass[12pt]{article}
\usepackage{tcolorbox,empheq}
\begin{document}
\begin{tcolorbox}
\begin{empheq}{align}
a=b
\end{empheq}
\end{tcolorbox}
\end{document}

screenshot

This code works fine, but if I try something like testa or testb, I get errors.

\documentclass[12pt]{article}
\usepackage{tcolorbox,empheq}

\newenvironment{testa}{\tcolorbox\empheq{align}}{\endempheq\endtcolorbox}
\newenvironment{testb}{\tcolorbox\setkeys{EmphEqEnv}{align}\EmphEqMainEnv}{\endEmphEqMainEnv\endtcolorbox}

\begin{document}
\begin{testa}
a=b
\end{testa}
\end{document}

Is there a solution ?

MB4E
  • 1,084

2 Answers2

4

I understand you want to combine tcolorbox with empheq. Instead of a tcolorbox which contains an empheq environment, you should declare an empheq environment with an optional parameter which defines the colorbox aspect. This combination can be declared as a newenvironment

\documentclass{article}

\usepackage{amsmath}
\usepackage{empheq}
\usepackage[most]{tcolorbox}

\newtcbox{\mymath}[1][]{%
    nobeforeafter, math upper, tcbox raise base,
    enhanced, colframe=blue!30!black,
    colback=blue!30, boxrule=1pt,
    #1}

\newenvironment{testa}[1]{%
    \empheq[box={#1}]{align}}{\endempheq}

\begin{document}
\begin{empheq}[box=\mymath]{align}
    c_i &= \langle\psi|\phi\rangle \\
    d_i &= \sqrt{x^2-1}
\end{empheq}

\begin{testa}{\mymath[colback=red!30]}
    c_i &= \langle\psi|\phi\rangle \\
    d_i &= \sqrt{x^2-1}
\end{testa}

\end{document}

enter image description here

Some other examples in Attractive Boxed Equations

Ignasi
  • 136,588
  • Yes, I know this method but I really have to do things the other way. – MB4E Mar 08 '18 at 18:14
  • @MB4E Could you explain it better? What's the result you want to obtain? – Ignasi Mar 08 '18 at 18:16
  • Just the same result as in my first code example. – MB4E Mar 08 '18 at 18:42
  • I just add a screenshot. – MB4E Mar 08 '18 at 18:48
  • Something like this? \begin{tcolorbox}[ams align]a=b\end{tcolorbox} – Ignasi Mar 08 '18 at 19:00
  • @MB4E I understand that empheq is used to place equation numbers outside boxes, but you want them inside. Then I don't know why do you need empheq. tcolorbox with ams ... options can produce boxes with multiline equations with numbers inside the box and without empheq. – Ignasi Mar 08 '18 at 19:03
  • You right, but this is just a MCV example of a more complex problem. Thanks for help, perhaps I have to find another way without empheq. – MB4E Mar 08 '18 at 19:10
3

I suggest to use a temporary wrapper environment for empheq and then 'wrap' \tcolorboxenvironment around it:

\documentclass[12pt]{article}
\usepackage[most]{tcolorbox}
\usepackage{empheq}

\newenvironment{testa}{%
  \empheq{align}
}{\endempheq}

\tcolorboxenvironment{testa}{colback=yellow!40!white}


\begin{document}


\begin{testa}
a=b
\end{testa}
\end{document}
  • Yes, this is working, but in fact I need something like \begin{testa}{gather*}a=b\end{testa} so the empheq parameter has to be new environment parameter. The method using \tcolorboxenvironment does not seem to work for that. – MB4E Mar 08 '18 at 21:25
  • @MB4E: Did you ask for that argument in your question? No, you didn't. By your comment you are changing the initial question... –  Mar 08 '18 at 21:29
  • I know, it was an MCV example and your answer is working in this case. I was hoping to adapt the solution to a more general situation but unfortunately it does not seem possible like that. But you right, the initial question is solved. – MB4E Mar 08 '18 at 21:37
  • Nice! and if you define testa* and put * on align*, it works without numbering. – PatrickT Jun 25 '20 at 21:03