7

I would like to enlarge vertically a box around a mathematical formula. I am looking for a generic way to do do, so that I could define a command to enlarge a box.

I would like to define commands \boxedB{} and \boxedBB{} that would add space in the box.

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\[
\boxed{ \
\left |\int_0^1 f(x)g(x)dx\right | \leq \sqrt{\int_0^1|f(x)|^2 dx}\cdot\sqrt{\int_0^1|g(x)|^2 dx}
\ }
\]
\end{document}

enter image description here

I would like to have more space above the \sqrt{} and below.


PS: It is not a problem if the command adds also space horizontally.

9

You can control the inner separation using \fboxsep. Unfortunately, that’s for vertical and horizontal distance, but apparently you also wanted more distance to left and right.

\documentclass{article}
\usepackage{amsmath}

\newcommand\boxedB[1]{{\setlength\fboxsep{10pt}\boxed{#1}}}

\begin{document}
\[
\boxedB{
  \left |\int_0^1 f(x)g(x)dx\right | \leq \sqrt{\int_0^1|f(x)|^2 dx}\cdot\sqrt{\int_0^1|g(x)|^2 dx}
}
\]
\end{document}

enter image description here


The definition of \boxed is not super-sophisticated, so we could also simpy extend it to take an optional argument which is the desired distance.

\documentclass{article}
\usepackage{amsmath}

\makeatletter
\renewcommand\boxed[2][\fboxsep]{% by default take current \fboxsep
  {%
    \setlength\fboxsep{#1}%
    \fbox{\m@th$\displaystyle#2$}% <-- original \boxed
  }%
}
\makeatother

\begin{document}
\[
\boxed[10pt]{
  \left |\int_0^1 f(x)g(x)dx\right | \leq \sqrt{\int_0^1|f(x)|^2 dx}\cdot\sqrt{\int_0^1|g(x)|^2 dx}
}
\]
\end{document}
  • Thanks :) Can I change this parameter locally? – Colas Jan 13 '17 at 13:22
  • (see edits to my question) – Colas Jan 13 '17 at 13:24
  • 1
    @Colas See updated answer. – Henri Menke Jan 13 '17 at 13:26
6

You can set separately the padding at the sides and at top/bottom:

\documentclass{article}
\usepackage{amsmath}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\xboxed}{O{}m}
 {
  \group_begin:
  \keys_set:nn { colas/xboxed } { #1 }
  \dim_set_eq:NN \fboxsep \l_colas_xboxed_tb_dim
  \boxed
   {
    \dim_compare:nF { \l_colas_xboxed_lr_dim = 0pt }
     {
      \hspace{-\fboxsep}\hspace{\l_colas_xboxed_lr_dim}
     }
    #2
    \dim_compare:nF { \l_colas_xboxed_lr_dim = 0pt }
     {
      \hspace{-\fboxsep}\hspace{\l_colas_xboxed_lr_dim}
     }
   }
  \group_end:
 }
\NewDocumentCommand{\xboxedset}{m}
 {
  \keys_set:nn { colas/xboxed } { #1 }
 }
\keys_define:nn { colas/xboxed }
 {
  lr .dim_set:N = \l_colas_xboxed_lr_dim,
  tb .dim_set:N = \l_colas_xboxed_tb_dim,
 }
\ExplSyntaxOff

\begin{document}

\[
\xboxed[tb=9pt]{
  \left |\int_0^1 f(x)g(x)\,dx\right | \leq
  \sqrt{\int_0^1|f(x)|^2\,dx}\cdot\sqrt{\int_0^1|g(x)|^2\,dx}
}
\]
\[
\xboxed[lr=3pt,tb=9pt]{
  \left |\int_0^1 f(x)g(x)\,dx\right | \leq
  \sqrt{\int_0^1|f(x)|^2\,dx}\cdot\sqrt{\int_0^1|g(x)|^2\,dx}
}
\]
\xboxedset{tb=9pt}
\[
\xboxed{
  \left |\int_0^1 f(x)g(x)\,dx\right | \leq
  \sqrt{\int_0^1|f(x)|^2\,dx}\cdot\sqrt{\int_0^1|g(x)|^2\,dx}
}
\]
\[
\xboxed[lr=3pt]{
  \left |\int_0^1 f(x)g(x)\,dx\right | \leq
  \sqrt{\int_0^1|f(x)|^2\,dx}\cdot\sqrt{\int_0^1|g(x)|^2\,dx}
}
\]
\end{document}

If lr is not given a value, the same padding as top/bottom is used.

The setting is local for the particular \xboxed (and nested ones, of course, unless countermanded) or it can be set for the current group (or even document-wise in the preamble) with \xboxedset.

enter image description here

  • Wow! Your solution is very impressive! – Colas Jan 13 '17 at 16:40
3

A non-framed version can be obtained by storing the content in a box, \mybox, and then adding a bar with extra height and depth in front of it. In this example \mybox is defined with \ensuremath{\displaystyle #2} since it is supposed to be used in displayed math. The same approach can be used together with \boxed to not add horizontal space, or in other other boxes without the math declarations.

\documentclass{article}
\usepackage{amsmath}
\usepackage{calc}
\newdimen\myextraHT
\myextraHT=5mm
\newsavebox\mybox%
\newcommand\HTbox[2][\myextraHT]{%
  \savebox\mybox{\ensuremath{\displaystyle #2}}%
  \raisebox{-\dp\mybox-#1}{\rule{0pt}{\ht\mybox+\dp\mybox+#1+#1}}%
  \usebox\mybox
}
\begin{document}
A row of text to show where the box horisontal start and end
\begin{displaymath}
  \left |\int_0^1 f(x)g(x)dx\right | \leq \sqrt{\int_0^1|f(x)|^2 dx}\cdot\sqrt{\int_0^1|g(x)|^2 dx} 
\end{displaymath}
A row of text to show where the box horisontal start and end
\begin{displaymath}
  \HTbox{\left |\int_0^1 f(x)g(x)dx\right | \leq \sqrt{\int_0^1|f(x)|^2 dx}\cdot\sqrt{\int_0^1|g(x)|^2 dx}}
\end{displaymath}
A row of text to show where the box horisontal start and end
\begin{displaymath}
  \HTbox[20mm]{\left |\int_0^1 f(x)g(x)dx\right | \leq \sqrt{\int_0^1|f(x)|^2 dx}\cdot\sqrt{\int_0^1|g(x)|^2 dx}}
\end{displaymath}
A row of text to show where the box horisontal start and end
\end{document}

enter image description here

To get a framed version, as requested in the question, an \fbox can be added to the command:

\newcommand\HTbox[2][\myextraHT]{%
  \savebox\mybox{\ensuremath{\displaystyle #2}}%
  \fbox{\raisebox{-\dp\mybox-#1}{\rule{0pt}{\ht\mybox+\dp\mybox+#1+#1}}%
    \usebox\mybox}
}

Then the framed equations becomes:

enter image description here

  • Simply put \fbox{...} around \usebox\mybox and you have the frame as well. – Henri Menke Jan 13 '17 at 19:32
  • @HenriMenke Yes, at some point I had something similar, but the \rule must also be included. I guess I wanted to show it in a more general way, and the frame looks a bit odd when space is added only vertically. Maybe I should have read the question one more time. I have edited the answer. – StefanH Jan 13 '17 at 20:47

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.