For the frame around theorem-like structures, one possibility is to use mdframed
and amsthm
; for a simple frame around equations, empheq
-style, it's enough to say something like:
\newcommand*\mymathbox[1]{%
\fcolorbox{ocre}{mygray}{\hspace{1em}#1\hspace{1em}}}
and then use box=\mymathbox
in the optional argument of empheq
. A complete example:
\documentclass{book}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{empheq}
\usepackage{bookman}
\usepackage[framemethod=tikz]{mdframed}
\definecolor{ocre}{RGB}{243,102,25}
\definecolor{mygray}{RGB}{243,243,244}
\newcommand*\mymathbox[1]{%
\fcolorbox{ocre}{mygray}{\hspace{1em}#1\hspace{1em}}}
\newtheoremstyle{mystyle}
{\topsep}
{\topsep}
{\normalfont}
{}
{\sffamily\bfseries}
{.}
{.5em}
{{\color{ocre}\thmname{#1}~\thmnumber{#2}}\thmnote{\,--\,#3}}%
\theoremstyle{mystyle}
\newmdtheoremenv[
backgroundcolor=mygray,
linecolor=ocre,
leftmargin=20pt,
innerleftmargin=0pt,
innerrightmargin=0pt,
]{theo}{Theorem}[section]
\begin{document}
\chapter{Test chapter}
\section{Test section}
\begin{theo}[Name of the theorem]
In $E=\mathbb{R}^n$ all norms are equivalent.
\begin{align}
a &= b\\
E &= mc^2 + \int_a^a x\, \mathrm{d}x
\end{align}
\end{theo}
\begin{empheq}[box=\mymathbox]{align}
a&=b\\
E&=mc^2 + \int_a^a x\, \mathrm{d}x
\end{empheq}
\end{document}
In a comment, it has been requested to have a framed box similar for those for theorems in my previous solution, but for math expressions; one possibility here is to use the tcolorbox
package:
\documentclass{book}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{bookman}
\usepackage[most]{tcolorbox}
\definecolor{ocre}{RGB}{243,102,25}
\definecolor{mygray}{RGB}{243,243,244}
\tcbset{myformula/.style={
arc=0pt,
outer arc=0pt,
colback=mygray,
colframe=ocre,
boxrule=0.4pt,
left=2pt,
right=2pt,
highlight math style={
arc=0pt,
outer arc=0pt,
colback=mygray,
colframe=red.
}
}
}
\begin{document}
\chapter{Test chapter}
\section{Test section}
\begin{tcolorbox}[ams equation,myformula]
E = mc^2 + \int_a^a x\, \mathrm{d}x
\end{tcolorbox}
\begin{tcolorbox}[ams align,myformula]
a &= b \\
E &= mc^2 + \int_a^a x\, \mathrm{d}x
\end{tcolorbox}
\end{document}
Or, using mdframed
again,
\documentclass{book}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{bookman}
\usepackage[framemethod=tikz]{mdframed}
\definecolor{ocre}{RGB}{243,102,25}
\definecolor{mygray}{RGB}{243,243,244}
\newmdenv[
innertopmargin=0pt,
backgroundcolor=mygray,
linecolor=ocre,
innerleftmargin=0pt,
innerrightmargin=0pt,
leftmargin=10pt
]{mymath}
\begin{document}
\chapter{Test chapter}
\section{Test section}
\begin{mymath}
\begin{align}
a &= b\\
E &= mc^2 + \int_a^a x\, \mathrm{d}x
\end{align}
\end{mymath}
\end{document}
Best Answer
Until it is Claudio Fiandrino’s (the author of
hf-tikz
) turn, let me propose four solutions.Solution 0
Of course, there is the solution that brought you to this: Just use one node that gets drawn and filled:
Solution 1 and 2
Solution 1 and 2 are very similar, they only differ on how they catch the math content:
uses an optional last argument that is delimited by
{ }
.If there is a last argument (
#5
in the code) it is used to determine its height and depth. Those are added to the rectangle.uses the ending
\tikzmarkend
to find the math content. The same procedure follows always (height, depth, …). This will break heavily if you nesthf-tikz
s.Which brings me to the advantages of the „uselessness“ of
hf-tikz
: It works across&
alignments and line-breaks of theamsmath
environments, and it could even be nested.Solution 3
A compromise: Solution works like the original but instead of having to specify the amount of height and depth, it determines those from math content given as optional arguments.
Codes/Outputs
Code 1 (optional
{ }
argument)Output 1
Code 2 (catches anything until
\tikzmarkend
)Output 2
Code 3 (optional argument with math content)
Output 3