4

I'm trying to insert a numbered equation inside a yellow box. The problem is that the yellow box extends across the entire line. How can I avoid it?

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[most]{tcolorbox}

\newtcolorbox{mybox}{
enhanced,
boxrule=0pt,frame hidden,
colback=yellow!40!white,
sharp corners
}

\begin{document}
\section{Introduction}
\begin{mybox}
\begin{equation}
a = b
\end{equation}
\end{mybox}

\end{document}

3 Answers 3

3

If all you need to achieve is to place a formula in a colored box, employing the machinery of the tcolorbox may be overkill. For sure, using the \colorbox macro of the xcolor package would require far less overhead.

enter image description here

\documentclass{article}
\usepackage{xcolor} % for '\colorbox' macro
\begin{document}
\section{Introduction}

\begin{equation}
\colorbox{yellow!40!white}{$\displaystyle ~a = b~\vphantom{\int}$}
\end{equation}

\end{document}

(I trust you can figure out what \vphantom{\int} and ~ are doing. If not, please let me know and I'll provide an explanation.)

3
  • 1
    Hi @Mico elegant solution. Can you tell me the meaning of \vphantom{\int} and ~ please? Commented Oct 9, 2021 at 9:57
  • 3
    @GennaroArguzzi - \vphantom{\int} inserts a (typographic) strut with the height and depth of a (displaystyle-size) integral symbol; this serves to create some vertical "padding" for the material that's enclosed by \colorbox. The ~ symbols insert a bit of horizontal whitespace; thus, inserting ~ at both the start and end of the formula serves to create a bit of horizontal padding for what's enclosed by \colorbox.
    – Mico
    Commented Oct 9, 2021 at 10:03
  • 2
    Thank you very much for the explanation @Mico Commented Oct 9, 2021 at 10:04
5

With empheq package:

\documentclass{article}
\usepackage{empheq}
\usepackage{xcolor}


\begin{document}
{\setlength\fboxsep{3ex}
\begin{empheq}[box=\colorbox{yellow}]{equation}
a = b 
\end{empheq}
}
\end{document}

enter image description here

Note: Size of yellow box is determined by border around equation. It is defined by fboxsep. You can determine it locally as is done in above MWE or globally, if you move \setlength\fboxsep{3ex} to the document preamble. You may find other fboxsep size more appropriate, for example 2ex, which gives:

enter image description here

3
  • great alternative @Zarko Commented Oct 9, 2021 at 10:15
  • @Mico, done. Thank you very much for the comment!
    – Zarko
    Commented Oct 9, 2021 at 10:33
  • +1 for the update
    – Mico
    Commented Oct 9, 2021 at 10:56
4

Probably the output is closer to the expected one, when using \tcbhighmath:

enter image description here

\documentclass{article}
\usepackage[most]{tcolorbox}

\tcbset{highlight math/.append style={boxrule=0pt,
                                      frame hidden,
                                      colback=yellow!40!white,
                                      sharp corners}}
\begin{document}
\section{Introduction}
\begin{equation}
\tcbhighmath{a=b}
\end{equation}
\end{document}
1
  • Hello @leandriis thank you for your useful solution Commented Oct 9, 2021 at 9:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .