...create a table?

The tabular environment of $latex \LaTeX$ is good for aligning content, such as problems and tables. Your content can either be aligned left (l), center aligned (c), aligned to the right (r), or fixed columns with word wrapping. Here's an example:
\begin{tabular}{l|r|c|l}
\hline
Steps & Number of Squares & Length of each square & Total Area\\
\hline
1 & 1 & 1 & 1\\
2 & 8 & $\frac{1}{3}$ & $8\left(\frac{1}{3}\right)^2$\\
\hline
$\vdots$ & $\vdots$ & $\vdots$ & $\vdots$\\
\hline        $n$ & $8^{n-1}$ & $\left(\frac{1}{3}\right)^{n-1}$ & $8^{n-1}\left(\left(\frac{1}{3}\right)^{n-1}\right)^2$   \\
\hline
\end{tabular}

After declaring \begin{tabular} the format of the table is determined here: {l|r|c|l}

There are 4 letters, so the table will have 4 columns: column 1 and 4 are aligned left, column 2 is aligned right, and column 3 is center aligned. The other symbols, the |, indicate where the vertical lines of the table will be. This table will have 3 vertical lines which separate the first, second, and third.

After setting up the format of the table you're ready to create the table; use  \hline to draw a horizontal line. The ampersand, &, is used to separate the columns. Since there are 4 columns in the table, 3 ampersands are needed. The line

Steps & Number of Squares & Length of each square & Total Area\\

tells us the first column will have the word "Steps", the second column "Number of Square", the 3rd column "Length of each square" and the 4th column "Total Area". The \\ tells $latex \LaTeX$ to go to the next line. After drawing a horizontal line with \hline, next line has all 4 columns containing 1. The \\ symbol tells us to go to the next line and since there is no \hline command, there will be no horizontal line. After filling out the information in the remaining table, \end{tabular} tells us to end the tabular environment.

In the first example, notice that the width of the columns wasn't determined by you, $latex \LaTeX$ set each width to make it look nice. The horizontal lines went the entire width of the table, too.

Example 2:

\begin{tabular}{|c|c|p{1cm}|c|c|}
\cline{1-2}
\cline{4-5}
$x$ & $y=\sin(x)$ & & $x$ & $\arcsin(x)$ \\
\cline{1-2}
\cline{4-5}
$-\frac{\pi}{2}$  & $-1$ & & $-1$  & $-\frac{\pi}{2}$  \\
\cline{1-2}
\cline{4-5}
$-\frac{\pi}{4}$ & $-\frac{\sqrt{2}}{2}$ & & $-\frac{\sqrt{2}}{2}$ & $-\frac{\pi}{4}$\\
\cline{1-2}
\cline{4-5}
$-\frac{\pi}{6}$ & $-\frac{1}{2}$ & & $-\frac{1}{2}$ & $-\frac{\pi}{6}$\\
\cline{1-2}
\cline{4-5}
\end{tabular}

Once again, after \begin{tabular} set up the format of the table: {|c|c|p{1cm}|c|c|}. There are 5 columns in this table. Columns 1, 2, 4, and 5 are centered and $latex \LaTeX is going to figure out how wide each column is. Column 3, however, says p{1cm} which tells us the column will be 1 centimeter wide. Content for this column will be left aligned and will wrap to the next line rather than overflow the column. The table begins with \cline{1-2}
\cline{4-5} which tells us a horizontal line will appear over the 1st and 2nd columns and the 3rd and 4th columns. The first line of content in the table says:

$x$ & $y=\sin(x)$ & & $x$ & $\arcsin(x)$ \\

This tells us an x (in math mode) will be in column 1 followed by $y=\sin(x)$ in column 2. Nothing is in column 3 followed by $x$ in column 4 and $\arcsin(x)$ in column 5 followed by ending the line with \\. The rest of the table is completed in similar fashion. Here's the tex file: CreateTable and a screenshot of the output.

CreateTable1

If you click on the image to get a closer view you'll notice, unfortunately, that the tables aren't as nice as you'd expect because the fractions end up taking too much vertical space.  Now that might not be a problem that you need to worry about with your table but you can easily see a potential problem in creating a table.

There are various ways to get around the problem; one way is to create extra space at the top and bottom of a cell to make the spacing better. Another way is to follow good design principles in creating your table. The booktabs documentation insists that you should never use vertical lines to separate columns. We'll see 2 methods for correcting the problem. One fix introduces space at the top and/or bottom. The second fix has space at the top and/or bottom and removes most of the vertical and horizontal lines. You can compare the approaches for yourself.

In order to introduce vertical more vertical space we'll create a new command. TeX Stackexchange to the rescue! This post tells us how: create a strut. Struts helps to ensure that something (in this case our row) has a minimum height. The new command is

\def\mystrut(#1,#2){\vrule height #1pt depth #2pt width 0pt}

\def defines the command mystrut which has 2 arguments. Of course, you can call it something else which has more meaning; say \ExtraSpace. The first argument of mystrut will be used to set the space above. It's the distance from the baseline to the top. The second argument will set the depth (the length from the baseline to the bottom) space below. By adjusting the numbers you'll create the space that gives you the look you want. Here's the tex file: CreateTable2 PDF output: CreateTable2, and screenshot of the result.

CreateTable2

You now have the knowledge needed to make a good looking, basic table. If you need more features and/or control over your table then there are packages to help you create professional publication quality tables. Look into the booktabs and tabu packages.

Leave a Reply

Your email address will not be published. Required fields are marked *

AlphaOmega Captcha Classica  –  Enter Security Code