Different Kinds of Boxes in Latex

Different Kinds of Boxes in Latex

A box is an object which is treated as a character in LaTex. A box can never be broken or split into multiple pages or lines. We have three kinds of boxes in LaTex, which are as follows:

  • LR – The contents of this box are rendered from left to right (LR = Left-Right).
  • Par – These kinds of boxes contain several lines that are typeset in paragraph mode like the normal text. Paragraphs can be put one on top of the other. We can control the width of the paragraph.
  • Rule – A rule is generally a thick or thin line that separates different rows or columns. It is also used to separate the title from the main text.

LR Boxes

We have four types of boxes under this category. These four varieties of boxes are generated by the following commands.

\fbox{texts}

\mbox{texts}

\framebox[width][pos]{texts}

\makebox[width][pos]{texts}

The commands that start with letter ‘f’ will render a rectangular frame inside which the texts will reside. This rectangular frame will be missing in the commands starting with another letter. The following example illustrates the same.

\documentclass{article}
\usepackage[utf8]{inputenc}
\begin{document}
\fbox{inside the fbox}\\[7pt]
\mbox{inside the mbox}\\[7pt]
\makebox[5cm][c]{inside the make box}\\[7pt]
\framebox[5cm][l]{inside the frame box}
\end{document}

Output:

The options provided in square brackets for the \framebox and \makebox commands are optional. Apart from ‘l’ (left) and ‘c’ (centre), we can also use ‘r’ (right) or ‘s’ (stretch). Option ‘r’ will put the text on the right side of the box and the ‘s’ option will stretch the texts from left to right side of the box. Must try these options to understand their effect. The default option is ‘c’.

Please note that the \makebox or \mbox commands also render boxes. The only difference is they do not render the rectangular frame. The command \fbox puts a rectangular frame around the texts. \mbox also does the same thing, but the frame will be missing.

The first optional argument width can have four-length commands: \width, \depth, \height, and \totalheight.

The \depth and \height commands control the length below and above the baseline. The \totalheight command is the sum of \depth and \height. The \width command deals with the width of the box. The following snapshot will make the concept even more clear.

Different Kinds of Boxes in Latex

The dotted horizontal line is the baseline.

\documentclass{article}
\usepackage[utf8]{inputenc}
\begin{document}
\framebox[2\width]{inside the frame box}
\end{document}

Output:

Different Kinds of Boxes in Latex

We can also control the appearance of the frames.

The \fboxrule parameter sets the width of the lines which comprise the frame. The default value is 0.4pt for all the classes.

Another parameter is \fboxsep. This command controls the space between contents and the edge of the frame. The default value is 3pt.

It is quite obvious that these commands can only come into the picture when we are using commands which render a frame (\fbox or \framebox).

Let us understand the functionality of the \fboxrule and \fboxsep parameters.

\documentclass{article}
\usepackage[utf8]{inputenc}
\begin{document}
\setlength\fboxrule{6pt}\setlength\fboxsep{6mm}
\fbox{We are learning about various commands to deal with the frame design.}
\end{document}

Output:

Different Kinds of Boxes in Latex

We have used \setlength to set the values for \fboxrule and \fboxsep.

Not only this, we can even lower or raise the box. To do so, we use the \raisebox command. The general syntax of the command is:

\raisebox{lift}[depth][height]{texts}

The second and the third arguments are optional. The lift parameter pushes the text above or below the baseline as per the value given by us. Consider the following code.

\documentclass{article}
\usepackage[utf8]{inputenc}
\begin{document}
\raisebox{1.5ex}{Up} baseline
\raisebox{-1.5ex}{down} baseline
\raisebox{1.5ex}{Up} baseline
\raisebox{-1.5ex}{down} baseline
\end{document}

Output:

Different Kinds of Boxes in Latex

The value 1.5ex does the lifting in the upward direction whereas, the negative value deals with the downward direction. We can also use length commands (\dept, \height, \width, \totalheight) in the \raisebox command (observe the optional arguments).

We can even do the rotation of boxes. The demonstration is given below.

\documentclass{article}
\usepackage{graphicx}
\begin{document}
$a_1$ \rotatebox{90}{\fbox{The vertical one}}
$a_2$ \rotatebox{45}{\raisebox{\height}{\fbox{Positioned at $45^\circ$}}}
$a_3$ \rotatebox{45}{\raisebox{-\height}{\fbox{This positioning is weird.}}}
$a_4$
\end{document}

Output:

Different Kinds of Boxes in Latex

For the rotation we use \rotatebox{}{} command. The first argument of the command tells about the rotation angle. The first box is rotated by 90 degrees. Therefore, we see a vertical box. The remaining two boxes are rotated by 45 degrees. The second argument takes the text to be rotated. We have also used the \raisebox command to change the positioning of the box. Observe the base position of the second and the third box. One is above the baseline, while the other is below the baseline. This is due to the usage of the \height and -\height commands in the arguments of the \raisebox command.

Note: It is mandatory to include the graphicx package. Otherwise, the \rotatebox{}{} command will not work.

Paragraph Boxes

We use the \parbox command to construct a paragraph box. The general syntax of the \parbox command is:

\parbox[pos]{width}{texts}

The pos argument is optional. The pos argument is used for positioning the text. Let us write a code using the \parbox command.

\documentclass{article}
\begin{document}
\parbox{0.5\linewidth}{This paragraph is typeset using the parbox command.}
\end{document}

Output:

Different Kinds of Boxes in Latex

We can see there is a line break at the word ‘parbox’ in the output. This is due to the width is set to half of the line-width. Just change the 0.5\linewidth to \linewidth, and then we will get the output as follows.

Different Kinds of Boxes in Latex

\linewidth contains the width of the line. We can also achieve the above output using the minipage environment. Run the following code and observe the output.

\documentclass{article}
\begin{document}
\begin{minipage}{\linewidth}{This paragraph is typeset using the parbox command.}
\end{minipage}
\end{document}

The general syntax for the minipage environment will be:

\begin{minipage}[pos]{width} … texts … \end{minipage}

Here also, the pos argument is optional.

Now, we can talk about the positioning of texts. The pos argument can take the following values:

t – To place the texts at the top of the page.

b – To place the texts at the bottom.

c – This option shows the text at the center of the page (vertically centralized). This is also the default value.

s – Letter ‘s’ stands for stretch. This option will stretch the text. Therefore, the gap between the words will increase.

All these above options do the vertical positioning with respect to the text baseline.

Now, consider the following code:

\documentclass{article}
\begin{document}
\begin{minipage}[t]{0.5\linewidth}
This is the first paragraph and its width is half of the line-width. This minipage environment is quite essential in positioning of texts.
\end{minipage}
\begin{minipage}[b]{0.5\linewidth}
This is the second paragraph and its width is half of the line-width. We must understand the usage of the different position values.
\end{minipage}
\end{document}

Output:

Different Kinds of Boxes in Latex

Note: The above output can also be achieved by using the \parbox command.

Boxes With Specific Heights

We can even extend the minipage environment or the \parbox command to include two more arguments. Thus, the general syntax of the \parbox command will be:

\parbox[pos][height][inner-pos]{width}{texts}

Similarly, for the minipage environment we have

\begin{minipage}[pos][height][inner-pos]{width} … texts … \end{minipage}.

Let us take an example to understand it better.

\documentclass{article}
\begin{document}
\begin{minipage}[b][4cm][t]{2cm}
The height of this minipage is 4~cm.
\end{minipage}\hfill
\begin{minipage}[b][4cm][c]{2cm}
The height of this minipage is also 4~cm. However, it is vertically centered.
\end{minipage}\hfill
\begin{minipage}[b][4cm][b]{2cm}
This minipage has the same height. But, it is positioned at the bottom of the page.
\end{minipage}\hfill
\begin{minipage}[b][4cm][s]{2cm}
\baselineskip 8pt plus 1pt minus 1pt
This is the last minibox of the same height. The texts in this box are stretched.
\end{minipage}
\end{document}

Output:

Different Kinds of Boxes in Latex

The height and width of each box are 4cm and 2cm, respectively. The \hfill command provides space between the boxes. This space is the rubber length, which can shrink or stretch horizontally as per need. In the last minibox, we have used the command \baselineskip. This command shows spaces between lines (observe the interline spaces in the last minipage of the output.). In our case, the interline space can reach up to as high as 9pt and as low as 7pt. Do try to generate the above output using \parbox command.

Nested Box

We can do any level of nesting for the boxes. Consider the following.

\documentclass{article}
\begin{document}
\fbox{\fbox { \parbox { .5\linewidth} {We are inside the second box. The line-width is half of the total line-width.}}}
\end{document}

Output:

Different Kinds of Boxes in Latex

Since we have nested two \fbox commands, we are getting two frames.

Rule Boxes

A rule box is a rectangle filled with black color. We use the \rule command to achieve the solid rectangle. The general syntax for the \rule command is:

\rule[lift]{width}{height}

The first argument, which is also the optional one, tells us about how much amount we need to raise the rectangle from the baseline. The second one and the third one is self-explanatory. The following code does illustrate the usage of the \rule command.

\documentclass{article}
\begin{document}
\rule{7mm}{10mm}
\rule[5mm]{10mm}{7mm}
\end{document}

Output:

Different Kinds of Boxes in Latex

The second rectangle, in the output, has been lifted by 5mm from the baseline. To place the rectangle below the baseline, use a negative value. We can also draw a dark horizontal line using the \rule command. The following code does the same.

\documentclass{article}
\begin{document}
\rule{5in}{.2mm}
\end{document}

Output:

Different Kinds of Boxes in Latex

In the code, we have increased the width a lot as compared to the height. Hence, we got the above result.