LaTeX Box Commands: \mbox
, \makebox
, \fbox
, \framebox
and \parbox
\mbox
and \makebox
LaTeX \mbox
and \makebox
command1:
1
2
3
4
\mbox{text}
\makebox{text}
\makebox[width]{text}
\makebox[width][position]{text}
Here are some examples:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
\documentclass[a4paper,11pt]{article}
\usepackage[margin=1in]{geometry}
\usepackage[x11names]{xcolor}
\pagecolor{AntiqueWhite1}
\setlength\parindent{0pt}
\begin{document}
\verb|\mbox| command:
\vspace{1em}
\mbox{text}
\vspace{5em}
\verb|\makebox| command:
\vspace{1em}
\makebox[10em][c]{left center right}\\
\makebox[10em][l]{left center right}\\
\makebox[10em][r]{left center right}\\
\makebox[10em][s]{left center right}\\
\makebox[\linewidth][c]{left center right}\\
\makebox[\linewidth][l]{left center right}\\
\makebox[\linewidth][r]{left center right}\\
\makebox[\linewidth][s]{left center right}\\
\makebox[0pt][l]{Left}Right\\
\newcommand{\pts}[1]{\makebox[0em][r]{#1 points\hspace*{1em}}}
\pts{10}What is the air-speed velocity of an unladen swallow?\\
\pts{90}An African or European swallow?\\
\end{document}
Besides, we can also use \widthof{<text>}
, provided by calc
package, to set the box width, which is a very convenient way when we want to create some text boxes which have the same width as a certain piece of text2:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
\documentclass[a4paper,11pt]{article}
\usepackage[margin=1in]{geometry}
\usepackage[x11names]{xcolor}
\pagecolor{AntiqueWhite1}
\setlength\parindent{0pt}
\usepackage{calc}
\begin{document}
\makebox[\widthof{long long text}][l]{text}\\
\makebox[\widthof{long long text}][l]{long text}\\
\makebox[\widthof{long long text}][l]{long long text}\\
\makebox[\widthof{long long text}][c]{text}\\
\makebox[\widthof{long long text}][c]{long text}\\
\makebox[\widthof{long long text}][c]{long long text}\\
\makebox[\widthof{long long text}][r]{text}\\
\makebox[\widthof{long long text}][r]{long text}\\
\makebox[\widthof{long long text}][r]{long long text}\\
\end{document}
\fbox
and \framebox
LaTeX \fbox
and \framebox
command3:
1
2
3
4
\fbox{text}
\framebox{text}
\framebox[width]{text}
\framebox[width][position]{text}
Here are some examples:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
\documentclass[a4paper,11pt]{article}
\usepackage[margin=1in]{geometry}
\usepackage[x11names]{xcolor}
\pagecolor{AntiqueWhite1}
\setlength\parindent{0pt}
\begin{document}
\verb|\fbox| command:
\vspace{1em}
\fbox{text}
{\setlength{\fboxsep}{0em}
{\fbox{left center right}}}\\
{\setlength{\fboxsep}{1em}
{\fbox{left center right}}}\\
{\setlength{\fboxsep}{0em}\setlength{\fboxrule}{1pt}
{\fbox{left center right}}}\\
{\setlength{\fboxsep}{1em}\setlength{\fboxrule}{1pt}
{\fbox{left center right}}}\\
\vspace{5em}
\verb|\framebox| command:
\vspace{1em}
\framebox[10em][c]{left center right}\\
\framebox[10em][l]{left center right}\\
\framebox[10em][r]{left center right}\\
\framebox[10em][s]{left center right}\\
\framebox[\linewidth][c]{left center right}\\
\framebox[\linewidth][l]{left center right}\\
\framebox[\linewidth][r]{left center right}\\
\framebox[\linewidth][s]{left center right}\\
{\setlength{\fboxsep}{0em}
{\framebox{left center right}}}\\
{\setlength{\fboxsep}{1em}
{\framebox{left center right}}}\\
{\setlength{\fboxsep}{0em}\setlength{\fboxrule}{1pt}
{\framebox{left center right}}}\\
{\setlength{\fboxsep}{1em}\setlength{\fboxrule}{1pt}
{\framebox{left center right}}}\\
\end{document}
where3:
\fboxrule
: The thickness of the rules around the enclosed box. The default is 0.2 pt.\fboxsep
: The distance from the frame to the enclosed box. The default is 3 pt.- and syntax like
\setlength{\fboxsep}{0em}
and\setlength{\fboxrule}{1pt}
is wrapped in extra curly braces to keep the specification work locally.
\parbox
1
2
3
4
\parbox{width}{contents}
\parbox[position]{width}{contents}
\parbox[position][height]{width}{contents}
\parbox[position][height][inner-pos]{width}{contents}
Here are some examples:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
\documentclass[a4paper,11pt]{article}
\usepackage[margin=1in]{geometry}
\usepackage[x11names]{xcolor}
\pagecolor{AntiqueWhite1}
\usepackage{lipsum}
\setlength\parindent{0pt}
\newcommand{\mytext}{\lipsum[1][1-2]}
\begin{document}
\mytext\parbox{20em}{\textcolor{blue}{\mytext}}\\
\mytext\parbox[t]{20em}{\textcolor{blue}{\mytext}}\\
\mytext\parbox[c]{20em}{\textcolor{blue}{\mytext}}\\
\mytext\parbox[b]{20em}{\textcolor{blue}{\mytext}}\\
\mytext\parbox[s]{20em}{\textcolor{blue}{\mytext}}\\
\mytext\parbox[t][15em]{20em}{\textcolor{blue}{\mytext}}\\
\mytext\parbox[t][15em][c]{20em}{\textcolor{blue}{\mytext}}\\
\mytext
\end{document}
By the way, for those more complicated cases, minipage
environment is more suitable than simple \parbox
command5:
A \parbox
command is used for a parbox containing a small piece of text, with nothing fancy inside. In particular, you shouldn’t use any of the paragraph-making environments inside a \parbox
argument. For larger pieces of text, including ones containing a paragraph-making environment, you should use a minipage
environment See minipage.
Display modes
For \mbox
, \makebox
, \fbox
, and \framebox
, the text in these commands is typeset in LR mode13, meaning that the text is not broken into lines7:
LR mode (for left-to-right mode; in plain TeX this is called restricted horizontal mode) is in effect when LaTeX starts making a box with an \mbox
command. As in paragraph mode, LaTeX’s output is a string of words with spaces between them. Unlike in paragraph mode, in LR mode LaTeX never starts a new line, it just keeps going from left to right. (Although LaTeX will not complain that the LR box is too long, when it is finished and next tries to put that box into a line, it might well complain that the finished LR box won’t fit there.)
while the text in the \parbox
command is in text mode4, which means that LaTeX will break lines to make a paragraph7:
Paragraph mode (in plain TeX this is called horizontal mode) is what LaTeX is in when processing ordinary text. It breaks the input text into lines and finds the positions of line breaks, so that in vertical mode page breaks can be done. This is the mode LaTeX is in most of the time.
For example,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
\documentclass[a4paper,11pt]{article}
\usepackage[margin=1in]{geometry}
\usepackage[x11names]{xcolor}
\pagecolor{AntiqueWhite1}
\usepackage{lipsum}
\setlength\parindent{0pt}
\newcommand{\mytext}{\lipsum[1][1-2]}
\begin{document}
\mbox{\mytext}
\makebox[10em][l]{\mytext}
\fbox{\mytext}
\framebox[10em][c]{\mytext}
\mytext\parbox{20em}{\textcolor{blue}{\mytext}}
\end{document}
References