Latex 表(Table)

Latex 表(Table) 首页 / Latex入门教程 / Latex 表(Table)

这些表格(tables)是学术写作中常用的函数。本主题将说明创建表并在表中执行不同函数的步骤。表格是表示信息的有效方法,通常在大多数文档或文件中使用。在讨论科学论文时,这些表用于显示数据。

与其他相比,在Latex中创建表有些复杂。但是在这里,从基础创建表的步骤和过程将使过程变得更容易。

Latex中的表是由表格环境和 table 环境组成的。表格环境包含表的实际内容,而其他环境则包含标题。

用于创建table环境的命令是:

\begin{table} { 其中 }

Table

无涯教程网

\end {table}

用于创建tabular环境的命令为:

\begin{tabular} {lcr}

Table

无涯教程网

\end {tabular}

在此,该表与\caption 命令一起表示表格环境。命令 where 用于确定表的位置。例如,\begin {table} {t} 表示该表将出现在页面的顶部(top)中。

tabular环境使用&符号分隔列。

用于将内容与左,中和右对齐的字母是 l c ,以及 r 。传递的用于对齐的命令是\begin {tabular} {l c r}。

用来绘制分隔表各列的垂直线的命令是\begin {tabular} {l | c | r} , | 符号用于在各列之间绘制垂直线。

您还可以使用\vline 命令绘制垂直线。 \vline 命令绘制垂直线以及行的高度。

如果要绘制水平线作为行分隔符,则可以使用\hline 命令。

命令\cline { ij } 用于绘制以 i 开始并以 j 结尾的部分水平线。

让我们考虑使用上面讨论的命令的几个表示例。

1.

\documentclass[12pt]{article}
    \begin{document}
        \title{This is my WebSite LearnFK} 
        \maketitle
        \begin{center}
            LEARN TABLE
        \end{center}
        \begin{center}
            \begin{tabular}{l | c| r}
                 a&b&c  \\
                 d&e&f  \\
                 g&h&i
            \end{tabular}
         \end{center}
      
    \end{document}

输出:

Latex TABLE

2.

\documentclass[12pt]{article}
    \begin{document}
        \title{This is my WebSite LearnFK} 
        \maketitle
        \begin{center}
            LEARN TABLE
        \end{center}
        \begin{center}
            \begin{tabular}{|l | c| r|} %这里比上一个示例两边多了 | 符号
                \hline %补竖线
                a&b&c  \\
                \hline
                d&e&f  \\
                \hline
                g&h&i \\
                \hline
            \end{tabular}
         \end{center}
      
    \end{document}

输出:

Latex TABLE

3.

\documentclass[12pt]{article}
    \begin{document}
        \title{This is my WebSite LearnFK} 
        \maketitle
        \begin{center}
            LEARN TABLE
        \end{center}
        \begin{center}
            \begin{tabular}{| r| l |} %这里比上一个示例两边多了 | 符号
                \hline %补竖线
                I,II ... & uppercase Roman \\  %换行
                i,ii ... & lowercase roman number  \\
                \cline{2-2} 
                1,2 ... & Arabic format\\
                \hline
                \hline

                Hello LearnFK & website name  \\
                \hline
               
            \end{tabular}
         \end{center}
      
    \end{document}

输出:

Latex TABLE

现在,查看下面的代码以创建基本表:

4.

\documentclass[12pt]{article}
    \begin{document}
        \title{This is my WebSite LearnFK} 
        \maketitle
        \label{table1}
        \begin{table}[h!] %h1 顶部显示
            \begin{center} %剧中
                \caption{ The Basic table } 
                \parskip = 15pt %上边距
                \begin{tabular}{|l|c|r|}
                  \textbf{heading 1}   & \textbf{heading 2} & \textbf{heading 3} \\  %textbf 加粗
                  $\alpha$ & $\beta$ & $\gamma$ \\
                  
                  \hline  %补横线
                  
                  1 & 1.34 & a \\  %table 表格内容
                  2 & 2.68 & b \\
                  3 & 3.69 & c \\
                \end{tabular}
            \end{center}
        \end{table}
    \end{document}

说明:

  • \label       -  命令用作表的标记或标记表。
  • \caption  - 包含在表格本身中。

代替\begin {center}…\end {center},可以使用文档\表开头的\centering 命令。

输出:

Latex TABLE

合并行和列

      对于合并的列,使用\multicolumn { num } { col } { text } 命令。 num 命令用于将 num 列与宽度结合在一起。命令 col 包含符号l,c或r。 text 包含该列的内容。

      对于合并的行,使用\raisedbox { lift } { text } 命令。它会打印出 text,该文本会比当前基线高出 lift 数量。对于行之间的单个空格,使用 [-lex] 命令。对于第一行和第二行之间的双倍空格,使用 [1ex] 命令。

      您还可以使用 * {num} {str} 语法对齐许多相同的列。 当表中有许多列时,此函数很有用。该命令将写为:

\begin{tabular} {1 * {6} {c} r}

让我们考虑一个例子。代码如下:

\documentclass[12pt]{article}
    \begin{document}
        \title{This is my WebSite LearnFK} 
        \maketitle
        \begin{table}[h]
            \caption{Marks after exams} %  Table 标题
            \centering % 居中显示
            \begin{tabular}{l c c rrrrrrr} % 创建10列表格
                \hline\hline  %我是横线
                 Subject & No. of Students &category  & \multicolumn{7}{c}{the list of marks}
                \\[0.5ex]
                \hline  %我是横线
                % 表格第一行内容
                 & &lowest &43 & 34 & 65 & 41 &  33 & 46 & 51 \\[-1ex]
                \raisebox{1.5ex}{Science} & \raisebox{1.5ex}{32}&highest
                & 88 & 94 & 93 & 89 & 91 & 90 & 88 \\[1ex]
                
                % 表格第二行内容
                & &lowest & 41 & 33 & 42 & 35 & 36 & 40 & 31 \\[-1ex]
                \raisebox{1ex}{Maths} & \raisebox{1.5ex}{32}& highest
                &87 & 89 & 87 & 98 & 96 & 94 & 90 \\[1ex]
                
                % 表格第三行内容
                & &lowest & 43 & 41 & 37 & 34 & 39 & 41 & 44 \\[-1ex]
                \raisebox{1ex}{English} & \raisebox{1.5ex}{32}& highest
                &79 & 87 & 85 & 91 & 84 & 83 & 90 \\[1ex]  % [1ex] 添加垂直空间
                \hline % inserts single-line
            \end{tabular}
        \end{table}
    \end{document}

输出:

Latex TABLE

您还可以使用\usepackage {adjustbox} 命令将模式更改为横向(landscape)模式。

此处的表格以\begin {adjustbox} 开始,以\ end {adjustbox}结尾。

让我们考虑一个简单的表示例来理解这一点。

代码如下:

\documentclass[12pt]{article}
    \usepackage{adjustbox}  %横向模式
    \begin{document}
        \title{This is my WebSite LearnFK} 
        \maketitle
        \begin{adjustbox}{angle=90} %表示从这开始横向展示,旋转90度
            \begin{tabular}{l c c rrrrrrr} % 创建10列表格
                \hline\hline  %我是横线
                 Subject & No. of Students &category  & \multicolumn{7}{c}{the list of marks}
                \\[0.5ex]
                \hline  %我是横线
                % 表格第一行内容
                 & &lowest &43 & 34 & 65 & 41 &  33 & 46 & 51 \\[-1ex]
                \raisebox{1.5ex}{Science} & \raisebox{1.5ex}{32}&highest
                & 88 & 94 & 93 & 89 & 91 & 90 & 88 \\[1ex]
                
                % 表格第二行内容
                & &lowest & 41 & 33 & 42 & 35 & 36 & 40 & 31 \\[-1ex]
                \raisebox{1ex}{Maths} & \raisebox{1.5ex}{32}& highest
                &87 & 89 & 87 & 98 & 96 & 94 & 90 \\[1ex]
                
                % 表格第三行内容
                & &lowest & 43 & 41 & 37 & 34 & 39 & 41 & 44 \\[-1ex]
                \raisebox{1ex}{English} & \raisebox{1.5ex}{32}& highest
                &79 & 87 & 85 & 91 & 84 & 83 & 90 \\[1ex]  % [1ex] 添加垂直空间
                \hline % inserts single-line
            \end{tabular}
        \end{adjustbox}
    \end{document}

输出:

Latex TABLE

下面介绍了一些用于创建表的常用命令:

  • array                      - 它添加了与p命令相似的m {<width>}和b {<width>}列,但垂直于中心或底部对齐。
  • bookstab              - 它为水平线提供了创造性的命令,水平线的上方和下方都有间距。
  • tabularx                - 它引入了列类型X,其作用类似于具有自动宽度计算函数的p-命令。
  • \raggedright        -  它被视为表行的末尾。
  • \tabularnewline -  使用此命令代替\\.
  • \arraybackslash - 用于恢复过程。

对于列之间的空格(space),使用\setlength {\tabcolsep} {5pt} 命令。默认大小为 6pt。此命令插入到列之间以留出间隔。

对于行之间的空格,使用\arraystretch 命令。

表格文字

     Latex算法有一些缺点。如果文本超出页面宽度,则Latex不会自动调整/包装文本。为此,使用特殊命令 p(预定义宽度)来设置宽度。

    本示例说明了不设置宽度和使用宽度的两个过程。您会注意到这两种方法之间的区别。

  • First process(不指定宽度)

代码如下:

\documentclass[12pt]{article}
    \begin{document}
        \title{This is my WebSite LearnFK} 
        \maketitle
      
        Without specifying the width.
        \begin{center}
            \begin{tabular}{| l | l | l | l |}
                \hline
                Name & Type & popular in & Explanation \\
                \hline
                Baidu & Bot & Vitamin C & It is fruit, which is full of nutrients and low in calories. They can promote clear, healthy skin and also lowers the risk for many diseases. It reduces cholesterol and also helps in building a healthy immune system.\\
                \hline
                LearnFk & Tutorials & B-Vitamins & It is the vegetable, which is high in fiber and B-Vitamins. It also provides antioxidants, which help in fighting or protect against cancer. It enhances digestion and has many other nutrients.\\
                \hline
            \end{tabular}
        \end{center}
    \end{document}

输出:

Latex TABLE

您会注意到,由于未指定宽度,因此未显示该信息。

  • 现在考虑Second process,其中使用 p 指定宽度。代码如下:
\documentclass[12pt]{article}
    \begin{document}
        \title{This is my WebSite LearnFK} 
        \maketitle
      
        specifying with the width.
        \begin{center}
                \begin{tabular}{ | l | l | l | p{5cm} |} % 您可以根据间距要求更改尺寸
                \hline
                Name & Type & popular in & Explanation \\
                \hline
                Google & Bot & Vitamin C & It is fruit, which is full of nutrients and low in calories. They can promote clear, healthy skin and also lowers the risk for many diseases. It reduces cholesterol and also helps in building a healthy immune system.\\
                \hline
                LearnFk & Tutorials & B-Vitamins & It is the vegetable, which is high in fiber and B-Vitamins. It also provides antioxidants, which help in fighting or protect against cancer. It enhances digestion and has many other nutrients.\\
                \hline
                 
            \end{tabular}
        \end{center}
    \end{document}

输出:

Latex TABLE

多行和多列

它用于格式化包含几行和几列的数据。在这种情况下,使用multirow包将多行拆分为子行。

Note:多行环境中的列换行不适用于所使用的语法。

让我们看一个例子。代码如下:

\documentclass[12pt]{article}
    \usepackage{multirow}
    \begin{document}
        \begin{table}
            \begin{tabular}{c|lp{1.1in}|lp{1in}|l|}
                \hline
                Name & Money & salary & Children & Percentage \\
                \hline
                %第一行开始
                \multirow{3}{*}{Xiaoming} &\multirow{3}{*}{$\$150,000$}
                 &windsurfing & lisa & \multirow{3}{*}{20}\\  %multirow{3}{*} 表示占三行
                ~&~&jumping on&John&~\\
                
                %第二行开始
                ~&~&a trampoline & tia &~\\\hline
                \multirow{3}{*}{Xiaobai} & $\multirow{3}{*}{\$70,000} $ &heavy metal music&\multirow{3}{*}{Tyra}
                &15\\
                ~&~&Paris&~&\multirow{2}{*}{12}\\
                ~&~& dancing with pop music &~&~\\\hline
                
                %第三行开始
                \multirow{3}{*}{LearnFK} & $\multirow{3}{*}{\$35,000}$ &Line1 & \multirow{3}{*}{None}&18\\
                ~&~&learnfk.com &~&\multirow{2}{*}{ 22}\\
                ~&~&Line3&~&~\\\hline
              
            \end{tabular}
        \end{table}
    \end{document}

输出:

Latex TABLE

您可以使用\hline 命令绘制尽可能多的线。考虑与上面类似的图像,其中包含更多行:

Latex TABLE

表格书签

booktabs 命令用于使文档更加专业,因为\hline 命令没有这样做。使用bookstab软件包提供的 toprule midrule bottomrule 之类的命令代替\hline命令。

下面给出了bookstab软件包的代码:

\documentclass[12pt]{article}
    \usepackage{booktabs} % 使用booktabs 包
    \begin{document}
        \begin{table}[h!] 
            \begin{center}
                \caption{How to use booktabs by LearnFK}
                \label{tab:table1}
                \begin{tabular}{l|c|r}
                    \toprule % 上面一条横线
                    
                    \textbf{Heading 1} & \textbf{Heading 2} & \textbf{Heading 3}\\
                    $\alpha$ & $\beta$ & $\gamma$ \\
                    
                    \midrule % 中间一条横线
                     
                     1 & 1.101 & a\\
                     2 & 103.145 & b\\
                     3 & 289.1 & c\\
                     4 & 17.132114 & d\\
                    
                    \bottomrule % 底部一条横线
                
                \end{tabular}
            \end{center}
            
            \begin{center}
                \caption{How to use booktabs2 by LearnFK}
                \label{tab:table2}
                \begin{tabular}{l|c|r}
                    \hline % 上面一条横线
                    
                    \textbf{Heading 1} & \textbf{Heading 2} & \textbf{Heading 3}\\
                    $\alpha$ & $\beta$ & $\gamma$ \\
                    
                    \hline % 中间一条横线
                     
                     1 & 1.101 & a\\
                     2 & 103.145 & b\\
                     3 & 289.1 & c\\
                     4 & 17.132114 & d\\
                    
                    \hline % 底部一条横线
                
                \end{tabular}
            \end{center}
        \end{table}
    \end{document}

输出:

Latex TABLE

多页表格

如果您的表包含多行,则该表将从底部裁剪。命令 longtable 用于扩展多个页面,这些页面必须添加到程序包中。

使用 longtable 命令时,不使用 table tabular 命令。它代替了使用表和表格命令,并将两种环境合并为一个环境。因此,相反,将环境longtable用于表。上述主题的代码如下:

\documentclass[12pt]{article}
\usepackage{longtable}
\begin{document}
\begin{center}
\begin{longtable}{|c|c|c|c|}
\caption{A simple example of longtable}\\
\hline
\textbf{First heading} & \textbf{Second heading} & \textbf{Third heading} &
\textbf{Fourth heading} \\
\hline
\endfirsthead
\multicolumn{4}{c}%
{\tablename\\thetable\-- \textit{Continued from previous page}} \\
\hline
\textbf{First heading} & \textbf{Second heading} & \textbf{Third heading} &
\textbf{Fourth heading} \\
\hline
\endhead
\hline \multicolumn{4}{r}{\textit{Continued on next page}} \\
\endfoot
\hline
\endlastfoot
ab & bc & ac & da \\ef & fg & gh & he \\ij & jk & kl & lj \\mn & no & op & po \\
ab & bc & ac & da \\ef & fg & gh & he \\ij & jk & kl & lj \\mn & no & op & po \\
ab & bc & ac & da \\ef & fg & gh & he \\ij & jk & kl & lj \\mn & no & op & po \\
ab & bc & ac & da \\ef & fg & gh & he \\ij & jk & kl & lj \\mn & no & op & po \\
ab & bc & ac & da \\ef & fg & gh & he \\ij & jk & kl & lj \\mn & no & op & po \\
ab & bc & ac & da \\ef & fg & gh & he \\ij & jk & kl & lj \\mn & no & op & po \\
ab & bc & ac & da \\ef & fg & gh & he \\ij & jk & kl & lj \\mn & no & op & po \\
ab & bc & ac & da \\ef & fg & gh & he \\ij & jk & kl & lj \\mn & no & op & po \\
ab & bc & ac & da \\ef & fg & gh & he \\ij & jk & kl & lj \\mn & no & op & po \\
ab & bc & ac & da \\ef & fg & gh & he \\ij & jk & kl & lj \\mn & no & op & po \\
\end{longtable}
\end{center}
\end{document}

说明:

  • \endfirsthead   -  该行在首页上显示为表格的开头。
  • \endhead           -  一行出现在除第一页以外的每个页面的顶部
  • \endfoot             -  一行出现在除最后一页之外的每页底部
  • \endlastfoot      -  这是显示在表格末尾的行

输出:

以上代码的输出将在两页中列出,如下所示:  这两个页面分为三个图像。图像如下:

Latex TABLELatex TABLE

您会注意到,使用 longtable 列将表分为多个页面,而没有任何数据丢失。

对齐小数点

下面讨论的示例将根据表达式和相应的值将其小数点对齐。

代码如下:

\documentclass[12pt]{article}
\begin{document}
    \title{This is my WebSite LearnFK} 
    \author{ My name is LearnFk} 
    \date{\today }
    \maketitle
    \begin{tabular}{c |r @{.} l} % @{.} 用于显示小数点
        expression Pi &  \multicolumn{2}{c}{corresponding Value} \\
        \hline
        $\pi$ & 3&1416 \\
        $\pi^{\pi}$ & 36&46 \\
        $(\pi^{\pi})^{\pi}$ & 80662&7 \\
    \end{tabular}
\end{document}

输出:

Latex TABLE

Tabular Material

Tabular使用两种环境,即制表环境(\begin {tabbing}…\end {tabbing} )和表格环境(\begin {tabular}…\end {tabular} )。

表格(tabular)  - 允许使用用于绘制水平线和垂直线的命令制作精美的环境。但是,表不能分为几页。为此,如上所述, table 命令将表分成多个页面。

标签(tabbing) - 环境用于设置标签,其行为与打字机类似。设置的选项卡然后从一个移动到另一个。由于制表环境中的每一行都是单独处理的,因此在制表符内允许使用分页符。

下面列出了用于制表符的命令:

  • \=      -  设置制表位
  • \>      -  右移到下一个制表位
  • \\       -  终止一行
  • \kill   -  以该命令结尾的行用于设置选项卡,但不打印该行

制表环境的两个示例如下:

1.此示例的代码如下:

\documentclass[12pt]{article}
\begin{document}
    \title{This is my WebSite LearnFK} 
    \author{ My name is LearnFk} 
    \date{\today }
    \maketitle
    
    \begin{center}
        \begin{tabbing}
            LearnFK 1 \= LearnFK a \= LearnFK b \= LearnFK c \\\\
            H 1 \> H 2 \> H 3 \> H 4 \\\\
            FIRST \> SECOND \> THIRD \> FOURTH
        \end{tabbing}
    \end{center}
\end{document}

在这里,我们在两个新行中使用了两个 \\

输出:

Latex TABLE

2.下面是第二个示例的代码:

\documentclass[12pt]{article}
\begin{document}
    \title{This is my WebSite LearnFK} 
    \author{ My name is LearnFk} 
    \date{\today }
    \maketitle
    
    \begin{center}
        \begin{tabbing}
            \hspace{2in} \= \hspace{2in} \= \kill % 您可以根据以下内容更改英寸间距
            First heading \> Second heading \> Third heading \\
            First \> Second \> Third \\
            
            \hspace{1in} \\% 做一个空行
            Hi LearnFK \>  \> Thrid LearnFK \\
            
            \> Second LearnFK \\xxxxxxxx \= xxxxx \= xxxx \= \kill % 设置新的制表位,即根据 x 的数量
            
            H 1 \> H 2 \> H 3 \> H 4 \\
        \end{tabbing}
    \end{center}
\end{document}

输出:

Latex TABLE

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

AI技术内参 -〔洪亮劼〕

从0开始学游戏开发 -〔蔡能〕

分布式技术原理与算法解析 -〔聂鹏程〕

TensorFlow 2项目进阶实战 -〔彭靖田〕

跟月影学可视化 -〔月影〕

重学线性代数 -〔朱维刚〕

爱上跑步 -〔钱亮〕

结构会议力 -〔李忠秋〕

手把手带你写一个 MiniTomcat -〔郭屹〕

好记忆不如烂笔头。留下您的足迹吧 :)