Select Page

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.

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.

The dotted horizontal line is the baseline.

Output:

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.

Output:

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.

Output:

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.

Output:

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.

Output:

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.

\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.

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:

Output:

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.

Output:

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.

Output:

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.

Output:

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.

Output:

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