6

There's a type of environment (shown below) which I'm using very frequently. I would like to define a new environment to begin it and end it with single commands.

\begin{equation}\boxed{\boxed{
\begin{gathered}
    i(t)=\frac{V_s}{R}\left[1-e^{-\frac{R}{L}t}\right]\\
    \textnormal{Time Constant (} \tau_L \textnormal{)} =\frac{L}{R}
\end{gathered}
}}\end{equation}

However, it seems *\boxed{}* cannot be split between the beginning-and-ending braces of \newenvironment{} \newenvironment{envname}{ *\boxed{* }{ *}* } (the parts enclosed in the asterisks have bad syntax).

Splitting two instances of "\boxed{}" is even worse syntactically.

For reference, this is the output I want: enter image description here

I have to type out these formatted equations very frequently, and any way to condense the necessary commands is appreciated... Is there a way to get around the syntactical issues and define a \newenvironment{name}{beginning}{ending} properly? If possible, I would like to avoid using extra packages.

Sorry if any part of this post seems stupid. I'm new to StackExchange...

3
  • 1
    Welcome to TeX SX! Should the environment include equation and gathered?
    – Bernard
    Aug 27 at 9:01
  • Thank you! Yes, I want the environment to include both of those things
    – Sisyphus
    Aug 27 at 9:42
  • You can use if you prefer also tcolorbox package.
    – Sebastiano
    Aug 27 at 12:14
4

Of course you cannot have unbalanced braces in the begin/end code. A way out is to use \boxes:

\documentclass[twocolumn]{article}

\usepackage{amsmath}

\newenvironment{boxedeq}{%
   \equation\setbox0=\hbox\bgroup$\gathered
  }{%
   \endgathered$\egroup
   \fbox{\fbox{\box0}}%
   \endequation
}

\begin{document}

\begin{boxedeq}
i(t)=\frac{V_s}{R}\left[1-e^{-\frac{R}{L}t}\right] \\
\textnormal{Time Constant ($\tau_L$)} =\frac{L}{R}
\end{boxedeq}

\end{document}

enter image description here

5
  • This seems to work too, thanks! What do the % signs denote though?
    – Sisyphus
    Aug 27 at 9:50
  • @Sisyphus The % are comment signs to hide the line endings. They are not strictly necessary here but they can be crucial in other situations, or you find spurious spaces.
    – campa
    Aug 27 at 10:14
  • Ohh yeah, those. I thought perhaps they had a different purpose here. It makes sense now. Thanks
    – Sisyphus
    Aug 27 at 12:17
  • Beware that this may not be color safe.
    – egreg
    Aug 27 at 20:05
  • @egrer Uhm, how so? The $ adds a group, and gathered adds further three (a box, a \ialign and a further math group). Or am I misunderstanding something?
    – campa
    Aug 28 at 10:18
6

With a “save box”:

\documentclass{article}
\usepackage{amsmath}

\newsavebox{\doubleboxedbox}
\newenvironment{doubleboxed}
 {\begin{lrbox}{\doubleboxedbox}$\displaystyle}
 {$\end{lrbox}\boxed{\boxed{\usebox{\doubleboxedbox}}}}

\begin{document}

\begin{equation}
\begin{doubleboxed}
  \begin{gathered}
    i(t)=\frac{V_s}{R}\left[1-e^{-\frac{R}{L}t}\right]\\
    \text{Time Constant ($\tau_L$)}=\frac{L}{R}
  \end{gathered}
\end{doubleboxed}
\end{equation}

\end{document}

(I simplified the input for the second line).

enter image description here

With expl3 facilities:

\documentclass{article}
\usepackage{amsmath}

\NewDocumentEnvironment{doubleboxed}{b}{\boxed{\boxed{#1}}}{}

\begin{document}

\begin{equation}
\begin{doubleboxed}
  \begin{gathered}
    i(t)=\frac{V_s}{R}\left[1-e^{-\frac{R}{L}t}\right]\\
    \text{Time Constant ($\tau_L$)}=\frac{L}{R}
  \end{gathered}
\end{doubleboxed}
\end{equation}

\end{document}
1
  • This seems to work, and thank you for simplifying the input!
    – Sisyphus
    Aug 27 at 9:48
3

Another solution, using a patched version of the \doublebox command from fancybox:

\documentclass{article}
\usepackage{amsmath} 
\usepackage{fancybox} 
\usepackage{xpatch}
\makeatletter
\xpatchcmd{\@doublebox}{\advance\fboxsep .5pt}{\advance\fboxsep 2.5pt}{}{}
\newenvironment{boxedequation}{\equation\doublebox\bgroup$\gathered}
{\endgathered$\egroup\endequation}
\makeatother

\begin{document}

\begin{boxedequation}
    i(t)=\frac{V_s}{R}\left[1-e^{-\frac{R}{L}t}\right]\\
    \textnormal{Time Constant (} \tau_L \textnormal{)} =\frac{L}{R}
\end{boxedequation}

\end{document} 

enter image description here

2
  • A bit too complicated for me right now, but it works otherwise. Might use it in the future. Thanks
    – Sisyphus
    Aug 27 at 12:22
  • I think it looks complicated because of the patch to adapt the distance between the frames to the image posted. Try it without the patch and see what the default \doublebox produces.
    – Bernard
    Aug 27 at 12:27

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.