Is there an easy way of putting an equation inside a dashed box?
Ideally similar to \boxed
from amsmath. The output should look something like this:
boxesequations
Is there an easy way of putting an equation inside a dashed box?
Ideally similar to \boxed
from amsmath. The output should look something like this:
You can use tikzmark
as from this answer by Andrew Stacey to mark the endpoints where you want the box. The \MyBox
macro as defined below accepts an optional first parameter which allows you to get fancy boxes.
This won't work if the text crosses line boundaries. If this is an issue and you are willing to consider highlighting instead of a box you should refer to Cool Text Highlighting in LaTeX.
This version will not adjust properly for text that adjusts the vertical spacing (ex. \dfrac{1}{2}
from the amsmath
package). However, an updated version of this that adjusts to the correct height is provided at A \boxed alternative with minimal spacing?
\documentclass[border=5pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,shapes}
\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node (#1) {};}
\newcommand{\DrawBox}[1][red]{%
\tikz[overlay,remember picture]{
\draw[#1]
($(bl)+(-0.2em,0.9em)$) rectangle
($(br)+(0.2em,-0.3em)$);}
}
\newcommand{\MyBox}[2][red]{\tikzmark{bl}#2\tikzmark{br}\DrawBox[#1]}
\begin{document}
Lorem ipsum dolor sit amet. \MyBox{Lorem ipsum} dolor sit amet. Lorem ipsum dolor sit amet.
Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem
ipsum \MyBox[blue]{$3.29 \times 10^{29}$} dolor sit amet. Lorem ipsum dolor sit amet. Lorem
ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem
\MyBox[draw=red,fill=yellow!20,,opacity=0.3]{$3.29 \times 10^{29}$} ipsum dolor sit amet.
Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet.
\end{document}
1st version: general box with mdframed
.
You don't need to combine
both packages, just load mdframed
with framemethod=TikZ
and look at the example 5 in mdframed-example-tikz.pdf
. I still don't completely understand how it works, but after some trial and error test I produced next code.
\documentclass{article}
\usepackage{tikz}
%\usetikzlibrary{shapes}
\usepackage{amsmath,amssymb}
\usepackage{lipsum}
\usepackage[framemethod=TikZ]{mdframed}
\mdfsetup{skipabove=\topskip,skipbelow=\topskip}
\tikzstyle{titregris} =
[draw=red, thick, fill=red,%
text=white, rectangle,
right,minimum height=.7cm]
\pgfdeclarehorizontalshading{exercisebackground}{100bp}{color(0bp)=(green!40);color(100bp)=(black!5)}
\newcounter{exercise}
\renewcommand*\theexercise{Exercise~n\arabic{exercise}}
\makeatletter
\def\mdf@@exercisepoints{}
\define@key{mdf}{exercisepoints}{
\def\mdf@exercisepoints{#1}
}
\def\mdf@@myboxedtitle{}
\define@key{mdf}{myboxedtitle}{
\def\mdf@myboxedtitle{#1}
}
\mdfdefinestyle{exercisestyle}{%
outerlinewidth=1em,%
outerlinecolor=white,%
leftmargin=-1em,%
rightmargin=-1em,%
middlelinewidth=1.2pt,%
roundcorner=5pt,%
linecolor=red,%
apptotikzsetting={\tikzset{mdfbackground/.append style={blue!30}}},
innertopmargin=1.2\baselineskip,
skipabove={\dimexpr0.5\baselineskip+\topskip\relax},
skipbelow={-1em},
needspace=3\baselineskip,
frametitlefont=\sffamily\bfseries,
settings={\global\stepcounter{exercise}},
singleextra={%
\node[titregris,xshift=1cm] at (P-|O) %
{~\mdf@frametitlefont{\mdf@myboxedtitle}~};
},%
firstextra={%
\node[titregris,xshift=1cm] at (P-|O) %
{~\mdf@frametitlefont{\mdf@myboxedtitle}~};
},
}
\makeatother
\begin{document}
\lipsum[1-4]
\begin{mdframed}[style=exercisestyle,myboxedtitle={Hello}]
To calculate the horizontal position the kinematic differential
equations are needed:
\begin{align}
\dot{n} &= u\cos\psi -v\sin\psi \\
\dot{e} &= u\sin\psi + v\cos\psi
\end{align}
For small angles the following approximation can be used:
\begin{align}
\dot{n} &= u -v\delta_\psi \\
\dot{e} &= u\delta_\psi + v
\end{align}
\end{mdframed}
\end{document}
The result is:
2nd version: exercise box with mdframed
.
As I've told, previous code was taken from an example at mdframed
documentation. This example is supposed to be used for exercises and I didn't change original names. Now, if you want to type exercises with this format we can come back to the original code, clean it a little bit and obtain:
\documentclass{article}
\usepackage{amsmath,amssymb}
\usepackage[framemethod=TikZ]{mdframed}
\tikzstyle{titlered} =
[draw=red, thick, fill=red,%
text=white, rectangle,
right, minimum height=.7cm]
\newcounter{exercise}
\renewcommand*\theexercise{Exercise~\arabic{exercise}}
\makeatletter
\mdfdefinestyle{exercisestyle}{%
outerlinewidth=1em,%
outerlinecolor=white,%
leftmargin=-1em,%
rightmargin=-1em,%
middlelinewidth=1.2pt,%
roundcorner=5pt,%
linecolor=red,%
backgroundcolor=blue!20,
innertopmargin=1.2\baselineskip,
skipabove={\dimexpr0.5\baselineskip+\topskip\relax},
skipbelow={-1em},
needspace=3\baselineskip,
frametitlefont=\sffamily\bfseries,
settings={\global\stepcounter{exercise}},
singleextra={%
\node[titlered,xshift=1cm] at (P-|O) %
{~\mdf@frametitlefont{\theexercise}~};},%
firstextra={%
\node[titlered,xshift=1cm] at (P-|O) %
{~\mdf@frametitlefont{\theexercise}~};},
}
\makeatother
\newcommand{\SomeText}{%
To calculate the horizontal position the kinematic differential
equations are needed:
\begin{align}
\dot{n} &= u\cos\psi -v\sin\psi \\
\dot{e} &= u\sin\psi + v\cos\psi
\end{align}
For small angles the following approximation can be used:
\begin{align}
\dot{n} &= u -v\delta_\psi \\
\dot{e} &= u\delta_\psi + v
\end{align}
}
\newenvironment{MyExercise}%
{\begin{mdframed}[style=exercisestyle]}{\end{mdframed}}
\begin{document}
\begin{MyExercise}
\SomeText
\SomeText
\end{MyExercise}
\begin{MyExercise}
\SomeText
\SomeText
\SomeText
\end{MyExercise}
\end{document}
3rd version: general and exercise box with tcolorbox
.
Just for completion, I'd like to add a solution made with tcolorbox
package. With recent versions it's possible to make breakable
framed boxes with it.
This package provides several libraries to modify it's default behavior, we will need three of them:
breakable
: to allow boxes to automatically break between pages.skins
: to add some overlays over regular boxes.hooks
: to be able of using some special hooks in our code.Next code includes some comments to explain how to use it.
\documentclass{article}
\usepackage{tikz}
\usepackage{etoolbox}
\usepackage{amsmath,amssymb}
\usepackage{lipsum}
\usepackage[skins,breakable,hooks]{tcolorbox}
\newcounter{myexercise}
% Definition of some styles and parameters for
% tcolorboxes
\tcbset{%
% Title boxes will be added with overlays
fancytitle/.code={%
\ifstrempty{#1}{}{\pgfkeysalso{%
top=\baselineskip,
overlay unbroken and first app = {%
\node[rectangle, draw=red, fill=red,
anchor=west,font=\color{white}]
at ([xshift=5mm]frame.north west)%
{\strut#1};},
enlarge top by = \baselineskip,
enlarge top at break by= 0mm,
}}},
% This is our boxes style
fancybox/.style 2 args = {%
breakable, enhanced, colback=blue!20,
colframe=red, width=#1,
fancytitle={#2},pad at break=2mm},
fancybox/.default = {.5\textwidth}{}%
}
% This command creates a new environment called
% FancyBox which uses previously defined fancybox
% style
\newtcolorbox{FancyBox}[2][.5\textwidth]{%
fancybox={#1}{#2}}
% This command creates a new environment called
% FancyExercise which uses previously defined
% fancybox style with an automatic enumerated
% title.
\newtcolorbox{FancyExercise}[1][.5\textwidth]{%
code={\stepcounter{myexercise}},
fancybox={#1}{Exercise~\arabic{myexercise}}}
\newcommand{\SomeText}{To calculate the horizontal position the kinematic differential
equations are needed:
\begin{align}
\dot{n} &= u\cos\psi -v\sin\psi \\
\dot{e} &= u\sin\psi + v\cos\psi
\end{align}
For small angles the following approximation can be used:
\begin{align}
\dot{n} &= u -v\delta_\psi \\
\dot{e} &= u\delta_\psi + v
\end{align}
}
\begin{document}
\lipsum[1-2]
% Example of a FancyExercise
\begin{FancyExercise}[.75\textwidth]
\SomeText
\end{FancyExercise}
% Example of a FancyExercise with default width
\begin{FancyExercise}
\SomeText
\end{FancyExercise}
% Example of a FancyBox with a mandatory title
\begin{FancyBox}[\textwidth]{This is a tcolorbox}
\SomeText
\end{FancyBox}
\end{document}
Best Answer
You can use the small
dashbox
package: