Table environment and Tabular environment in Latex

Table of contents

1. The difference between Table and Tabular

 2. A simple Table environment example:

3. Latex's "auto wrap" function

Four, Latex multi-line and multi-column

5. Use tablesgenerator to quickly convert excel tables into tex codes

Sixth, set the width of the table to be consistent with the page width


Description: Contents 1 to 4 come from Latex using Table (table) and common parameter explanation_\begin{tabular}_iioSnail's blog-Programmer Secret-Programmer Secret

Five to six are from the original.

1. The difference between Table and Tabular

code:

\documentclass{article}

\begin{document}


\begin{table}[h!]
\centering
\begin{tabular}{||c c c c||} 
 \hline
 Col1 & Col2 & Col2 & Col3 \\ [0.5ex] 
 \hline\hline
 1 & 6 & 87837 & 787 \\ 
 2 & 7 & 78 & 5415 \\
 3 & 545 & 778 & 7507 \\
 4 & 545 & 18744 & 7560 \\
 5 & 88 & 788 & 6344 \\ [1ex] 
 \hline
\end{tabular}
\caption{Table to test captions and labels.}
\label{table:1}
\end{table}



\end{document}

Note: If you don't understand the code, you can use Chatgpt to let it explain the meaning of each line of code. For specific operations, select all the codes, click on the browser chatgpt plug-in, such as Sider, and explain.

Code result:

 2. A simple Table environment example:

code:

\documentclass{article}
\begin{document}
\begin{tabular}{l|c}
Name&Salary\\\hline
Mark&$\$250,000$\\
Carly&$\$80,000$\\
Carter&$\$25,000$\\
Sam&$\$50,000$
\end{tabular}
\end{document}

Code result:

Code explanation:

  • {l|c} : Change the parameter to indicate the number of columns and their corresponding typesetting. lmeans left alignment , cmeans center alignment , and r means right alignment. | Indicates the separator between two columns. When not added |, there will be no vertical bar Namebetween Salary the two columns.
  • & : This symbol represents the separator between two cells in a row. For example, if this has two columns, each line has a &symbol. If there are n columns, each row should have n-1  & symbols
  • \\: Newline character, which means the end of the line and the next line
  • hline: horizontal line, indicating that a horizontal line should be added below the line. For example, in the above picture, there is a horizontal line under the first line, but there is no line behind it.

3. Latex's "auto wrap" function

Latex does not automatically wrap, when the text is too large, it will just keep writing backwards, even beyond the page

The code for the two tables is as follows:

Of the two output tables, the upper one is unformatted and the lower one is formatted.

  • p{width} : Can be used  p{width}to specify the column width. For example, the first one means that no column width is specified ( c|lll, that is, column 1 is centered and columns 2-4 are left-aligned). The second table specifies column widths ( c|lp{2in}p{1in}ie centered column 1, left aligned column 2, 2 feet for column 3, 1 foot for column 4). The units that can be used for width are cm, pt, in, emetc.

Here is an example of LaTeX code to create two tables. The first table uses the "lll" parameter, which means that there are three columns, and each column is left-aligned. The first row of the first table is the header, followed by four rows of data. The data in the first table includes name, salary, hobbies, and children information.

The second table uses the "lp{2in}p{1in}" parameter, indicating that there are four columns, the first column is left-aligned, the second column is paragraph-aligned, with a width of 2 inches, and the third column is paragraph-aligned , with a width of 1 inch. The second table has the same data as the first table.

This code can be used in a LaTeX environment to generate two tables with different formatting.

Four, Latex multi-line and multi-column

\documentclass{article}
\usepackage{multirow}
\begin{document}
\begin{table}
\begin{tabular}{c|lp{1.4in}lp{1.1in}}
Name&Salary&Likes&Children&Desires\\\hline
\multirow{3}{*}{Mark}&\multirow{3}{*}{$\$250,000$}&windsurfing&Amy&\multirow{3}{*}{own
	a cheese shop}\\
~&~&jumping on&John&~\\
~&~&a trampoline&Ray&~\\\hline
\multirow{3}{*}{Carly}&$\multirow{3}{*}{\$80,000}$&heavy metal music&\multirow{3}{*}{Tyra}
&tattoo a president\\
~&~&Paris&~&\multirow{2}{*}{of the united states}\\
~&~&dancing in the rain&~&~\\\hline
\multirow{3}{*}{Carter}&$\multirow{3}{*}{\$25,000}$&candy&\multirow{3}{*}{None}&play the
	Ukelele\\
~&~&fast cars that he cannot afford&~&\multirow{2}{*}{ travel the world}\\
~&~&Ramen&~&~\\\hline
\multirow{3}{*}{Sam}&$\multirow{3}{*}{\$50,000}$&paintingit&Kyle&\multirow{3}{*}{be in the
	circus}\\
~&~&motorcycles&\multirow{2}{*}{Sam Jr.}&~\\
~&~&Reddit&~&~
\end{tabular}
\end{table}
\end{document}

Code explanation:

Here is a LaTeX snippet to create a table. The code uses the `multirow` macro package to merge cells.

The table has 5 columns, namely Name (name), Salary (salary), Likes (hobbies), Children (children), and Desires (wish). Each row represents a person, and the information of each person is filled in the corresponding column.

The `\multirow` command is used to merge cells in a row. In this table, there are multiple rows using the `\multirow` command. For example, "Mark", "$\$250,000$", and "own a cheese shop" in the first line all use the `\multirow` command to combine the three lines.

The `\hline` command is used to draw horizontal lines to separate the table into different lines. In this form, there are four horizontal lines.

The LaTeX code can be generated by a compiler to generate a PDF document containing tables. Tables will appear in the generated documentation with the specified format and content.

5. Use tablesgenerator to quickly convert excel tables into tex codes

Multiple rows and multiple columns of the table, that is, the requirement of merging cells, we can design the table style we want in excel, copy and paste the table into the new tab page (tablesgenerator.com)  , and then click to generate tex code , you can copy and paste it into our tex file.

Or upload the csv file on the above website to achieve the same result as above. Note that when the table is very long, when complex merged cells are involved, uploading a csv file works better.

Sixth, set the width of the table to be consistent with the page width

Next, the problem is how to set the size of the table.

The specific operation is as follows:

code:

\documentclass{article}
\begin{document}
\begin{table}
\begin{tabular}{c|lll}
Name&Salary&Likes&Children\\\hline
Mark&$\$250,000$&windsurfing and jumping on trampolines&Amy, John, and Ray\\
Carly&$\$80,000$&heavy metal music, Paris, and dancing in the rain&Tyra\\
Carter&$\$25,000$&candy, fast cars that he cannot afford and Ramen&None\\
Sam&$\$50,000$&painting, motorcycles, and Reddit&Kyle and Sam Jr.
\end{tabular}
\end{table}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{table}
\begin{tabular*}{\hsize}{@{}@{\extracolsep{\fill}}c|lll@{}}
Name&Salary&Likes&Children\\\hline
Mark&$\$250,000$&windsurfing and jumping on trampolines&Amy, John, and Ray\\
Carly&$\$80,000$&heavy metal music, Paris, and dancing in the rain&Tyra\\
Carter&$\$25,000$&candy, fast cars that he cannot afford and Ramen&None\\
Sam&$\$50,000$&painting, motorcycles, and Reddit&Kyle and Sam Jr.
\end{tabular*}
\end{table}
\end{document}

operation result:

 Note: This example may not be very good. The essence is to make the width of the table in Chapter 2 the same as that of the entire page. The width of the page refers to the area where the margin is removed. If you want to achieve this goal, you only need to use the tabular environment Add the series of codes above: \begin{tabular*}{\hsize}{@{}@{\extracolsep{\fill}} c|lll @{}} Just replace c|lll with your own form The required column division and typesetting are enough. For example, I have a table with 7 columns, and I want it to be left-aligned. I only need to replace c|lll with 7 l, that is, lllllll. The overall modification is as follows: \begin{tabular*}{\hsize}{@{}@{\extracolsep{\fill}} lllllll @{}}.

This code is used to create two tables. The first table uses the tabular environment, which contains four columns, namely Name, Salary, Likes and Children. A horizontal line is used in the table to separate the header and the content of the table, the content of each row is separated by an & symbol, and the end of each row is represented by a \ symbol. The content of the form includes the information of four people, including name, salary, hobbies and children.

The second table uses a tabular* environment, compared to the first table, its width is the width of the entire page , set by the \hsize command. Other settings are the same as the first form.

 

reference:

Using Table (table) and common parameters in Latex to explain _\begin{tabular}_iioSnail's Blog - Programmer's Secret - Programmer's Secret (cxymm.net)

Guess you like

Origin blog.csdn.net/u011375991/article/details/131343330