# How to specify height and width of \fbox{}

I am trying to have my text read like this inside a box:

        If A and B are two events that are not mutually exclusive then:

$P(A \cup B) = P(A) + P(B) - P(A \cap B)$


Al I have is this, can someone help guide me of hidden commands that I don't know about?

\fbox{If A and B are two events that are not mutually exclusive then $P(A \cup B) = P(A) + P(B) - P(A \cap B)$}

• Maybe is this what you are trying to achieve? \documentclass{article} \begin{document} If A and B are two events that are not mutually exclusive then: $P(A \cup B) = P(A) + P(B) - P(A \cap B)$ \end{document}. Mar 16, 2013 at 2:36
• I want to somehow have a box command code around this: \begin{center} If A and B are two events that are not mutually exclusive then: \end{center} \begin{center} $P(A \cup B) = P(A) + P(B) - P(A \cap B)$ \end{center} Mar 16, 2013 at 2:50
• Off topic, but P(AB) = P(A) + P(B) − P(AB) even if A and B are two events that are mutually exclusive, because in that case AB is ∅ and P(∅) = 0 by axiom. Feb 21, 2019 at 12:11

You can use \fbox and a minipage (or a \parbox) of the desired (fixed) width; another option would be to use the varwidth environment from the varwidth package, so the resulting width is the natural width of the contents. A little example showing both approaches:

\documentclass{article}
\usepackage{varwidth}

\begin{document}

\noindent\fbox{\begin{minipage}{\dimexpr\textwidth-2\fboxsep-2\fboxrule\relax}
\centering
If A and B are two events that are not mutually exclusive then:
$P(A \cup B) = P(A) + P(B) - P(A \cap B)$
\end{minipage}}

\begin{center}
\fbox{\begin{varwidth}{\dimexpr\textwidth-2\fboxsep-2\fboxrule\relax}
If A and B are two events that are not mutually exclusive then:
$P(A \cup B) = P(A) + P(B) - P(A \cap B)$
\end{varwidth}}
\end{center}

\end{document}


Using the optional arguments for minipage you can control other attributes of the used box; in particular, the second optional argument allows you to specify the height:

\documentclass{article}

\begin{document}

\noindent\fbox{\begin{minipage}[t][3\height][c]{\dimexpr\textwidth-2\fboxsep-2\fboxrule\relax}
\centering
If A and B are two events that are not mutually exclusive then:
$P(A \cup B) = P(A) + P(B) - P(A \cap B)$
\end{minipage}}

\end{document}

• \relax can be omitted in this case, I think. Mar 16, 2013 at 3:17
• i'm sorry i cant figure out where you are setting the width and height in inches in your example, i just see the height as 3, which is inches? Oct 19, 2019 at 11:46

There is another posibility with tcolorbox package.

A MWE:

\documentclass{article}
\usepackage[skins]{tcolorbox}
\begin{document}
\begin{tcolorbox}[skin=widget,
boxrule=1mm,
coltitle=black,
colframe=blue!45!white,
colback=blue!15!white,
width=(.9\linewidth),before=\hfill,after=\hfill,
If A and B are two events that are not mutually exclusive then:
\tcblower
$P(A \cup B) = P(A) + P(B) - P(A \cap B)$
\end{tcolorbox}
\end{document}

• Could you add some example of usage? Otherwise this is more of a comment than an answer. Alternative ways of doing something are always welcome. Mar 16, 2013 at 10:56

Gonzalo Medina's is the actual answer to the problem given, but here is another option: Instead of using an \fbox you could also the mdframed package which in basic usage

\begin{mdframed}
If A and B are two events that are not mutually exclusive then:
$P(A \cup B) = P(A) + P(B) - P(A \cap B)$
\end{mdframed}


produces a nice box:

but also allows you to get fancy should you so desire:

## Notes:

• The style used here is only slightly modified from one of the examples in the documentation, but am sure you could pick better color choices. This one was just meant to illustrate a few of the options, but there are numerous others.

## Code:

\documentclass{article}
\usepackage{xcolor}
\usepackage[framemethod=tikz]{mdframed}

\begin{document}
\begin{mdframed}
If A and B are two events that are not mutually exclusive then:
$P(A \cup B) = P(A) + P(B) - P(A \cap B)$
\end{mdframed}

\bigskip
\begin{mdframed}[
linecolor=red,linewidth=2pt,%
frametitlerule=true,%
apptotikzsetting={\tikzset{mdfframetitlebackground/.append style={%
frametitlerulecolor=blue,
frametitlerulewidth=1pt, innertopmargin=\topskip,
frametitle={Non Mutually Exclusive Events},
outerlinewidth=1.25pt
]
% ----------
If A and B are two events that are not mutually exclusive then:
$P(A \cup B) = P(A) + P(B) - P(A \cap B)$
\end{mdframed}
\end{document}

• The package framed works in the same way \begin{framed} Here your text \end{framed} I don't know the differences with mdframed Sep 20, 2017 at 13:36
• @loved.by.Jesus: I think framed is an older package: last updated in 2012. mdframed is a bit newer and allows for use of tikz. Having said that, mdframed is also geting old and the latest similar pacakge that I know of is tcolorbox. Sep 21, 2017 at 22:15

What about framebox ?

\framebox(300,100){some text (or keep it empty)}


where 300 is the wanted width and 100 the height of the box, in \unitlength (pt by default).

(Credits to David Carlisle)

• If you credit someone, you could post the answer as Community Wiki question to not "steal" it ;-) Oct 13, 2015 at 13:15
• @RomainPicot : I don't consider answering to an old question with input from a reply to another question, moreso linking to the original answer, is stealing. Actually it would even only be strictly applying the CC-BY-SA rules, provided the initial answer was original enough — which it is indubitably not… Anyway, since it's asked nicely. Oct 13, 2015 at 13:54
• +1 for simple answer! how do you arrange the text conf so it doesn't go out of box at sides? Aug 23, 2016 at 14:36
• @nilon Sorry, I don't know and don't have the ability to search currently. Aug 24, 2016 at 9:24
• Amazingly enough it doesn't work when I try to specify a different unit for length, like \framebox(3cm,2cm){text}. I get an error. Does one have to use \unitlength? Sep 10, 2020 at 7:39