当前位置: 代码网 > it编程>游戏开发>ar > Latex表格制作详细教程(table, tabular, multirow, multicolumn)

Latex表格制作详细教程(table, tabular, multirow, multicolumn)

2024年08月01日 ar 我要评论
Latex表格制作超详细教程

一、简单表格制作

latex表格需要用到 table 和 tabular 环境。其中 table 环境里写表格的标题(caption)、表格的位置之类的。 tabular 环境则是绘制表格的内容。一个简单的表格绘制代码如下所示:

\documentclass{article}

\begin{document}

\begin{table}[h!]
  \begin{center}
    \caption{your first table.}
    \begin{tabular}{l|c|r} % <-- alignments: 1st column left, 2nd middle and 3rd right, with vertical lines in between
      \textbf{value 1} & \textbf{value 2} & \textbf{value 3}\\
      $\alpha$ & $\beta$ & $\gamma$ \\
      \hline
      1 & 1110.1 & a\\
      2 & 10.1 & b\\
      3 & 23.113231 & c\\
    \end{tabular}
  \end{center}
\end{table}

\end{document}

结果如下:
在这里插入图片描述
首先是 table 环境部分,\begin{center}让表格居中,\caption{your first table.}写表格的标题。

然后是 tabular 环境部分,\begin{tabular}{l|c|r}这里面的{l|c|r},包含了三个字母,代表了表格总共有三列,第一列靠左偏移,第二列居中,第三列靠右偏移。竖线代表列之间用线分隔开来,如果想要左右两边都用线包围起来,应该改成{|l|c|r|}。接下来就是正式的表格绘制部分。

latex里的表格是一行行来绘制的,每一行里面用&来分隔各个元素,用\\来结束当前这一行的绘制。代码中\textbf{value 1} & \textbf{value 2} & \textbf{value 3}\\绘制表格的第一行,是三个加粗的字符串。第二行$\alpha$ & $\beta$ & $\gamma$ \\则是三个希腊字符。

接着是\hline,它的作用是画一整条横线,注意如果想画一条只经过部分列的横线,则可以用cline{a-b},代表的是画一条从第a列到第b列的横线。

二、表格单元占据多个行或者列

表格单元占据多个行或者列需要用到multirowmulticolumn,首先要引入相关的包:

%...
\usepackage{multirow} % required for multirows
\begin{document}
%...

multirowmulticolumn的格式如下:

\multirow{number_of_rows}{width}{content} 

number_of_rows代表该表格单元占据的行数,width代表表格的宽度,一般填 * 代表自动宽度,content则是表格单元里的内容。

\multicolumn{number_of_columns}{alignment}{content}

number_of_columns代表该表格单元占据的列数,alignment代表表格内容的偏移(填l,c或者r),content则是表格单元里的内容。

2. 1 一个multirow的例子

\begin{table}[h!]
  \begin{center}
    \caption{multirow table.}
    \label{tab:table1}
    \begin{tabular}{l|s|r}
      \textbf{value 1} & \textbf{value 2} & \textbf{value 3}\\
      $\alpha$ & $\beta$ & $\gamma$ \\
      \hline
      \multirow{2}{*}{12} & 1110.1 & a\\ % <-- combining 2 rows with arbitrary with (*) and content 12
      & 10.1 & b\\ % <-- content of first column omitted.
      \hline
      3 & 23.113231 & c\\
      4 & 25.113231 & d\\
    \end{tabular}
  \end{center}
\end{table}

在这里插入图片描述

主要是这一句代码\multirow{2}{*}{12} & 1110.1 & a\\,使得内容为 12 的这一个表格单元占据了两行。注意的是,下一行的第一个位置,由于已经被 12 这个单元给占据了,因此第二行的代码是& 10.1 & b\\,这里第一个位置没有写东西,如果写了东西会使得这一行超过3列,表格错位。

2.2 一个multicolumn的例子

\begin{table}[h!]
  \begin{center}
    \caption{multicolumn table.}
    \label{tab:table1}
    \begin{tabular}{l|s|r}
      \textbf{value 1} & \textbf{value 2} & \textbf{value 3}\\
      $\alpha$ & $\beta$ & $\gamma$ \\
      \hline
      \multicolumn{2}{c|}{12} & a\\ % <-- combining two cells with alignment c| and content 12.
      \hline
      2 & 10.1 & b\\
      3 & 23.113231 & c\\
      4 & 25.113231 & d\\
    \end{tabular}
  \end{center}
\end{table}

在这里插入图片描述
这里主要是这一句代码\multicolumn{2}{c|}{12} & a\\,使得内容为 12 的这一个表格单元占据了两列。所以这一行接下来只能填一个元素a。注意这里的alignment写的是c|代表表格内容居中,表格右侧有竖线而左侧没有。

2.3 结合multirow与multicolumn

单元格需要同时占据两行和两列,其实只需要把\multirow{number_of_rows}{width}{content}的content写成multicolumn就可以了。

\begin{table}[h!]
  \begin{center}
    \caption{multirow and -column table.}
    \label{tab:table1}
    \begin{tabular}{l|s|r}
      \textbf{value 1} & \textbf{value 2} & \textbf{value 3}\\
      $\alpha$ & $\beta$ & $\gamma$ \\
      \hline
      \multicolumn{2}{c|}{\multirow{2}{*}{1234}} & a\\ % <-- multicolumn spanning 2 columns, content multirow spanning two rows
      \multicolumn{2}{c|}{} & b\\ % <-- multicolumn spanning 2 columns with empty content as placeholder
      \hline
      3 & 23.113231 & c\\
      4 & 25.113231 & d\\
    \end{tabular}
  \end{center}
\end{table}

在这里插入图片描述最主要的语句\multicolumn{2}{c|}{\multirow{2}{*}{1234}} & a\\使得内容为1234的表格单元占据了2行两列,所以当前这一行右边只能再写一个元素a了,然后注意的是下一行是\multicolumn{2}{c|}{} & b\\用一个空的multicolumn来代表前两个位置空置,然后填剩下的一个元素b。

三、复杂表格实例

\begin{table*}[t]
\centering
\caption{map scores of teacher model, different student models with 4 widths and three baseline models with different length of binary codes on cifar-10 and sun datasets.}
\label{table1}
\begin{tabular}{|c|c|c|c|c|c|c|c|c|c|c|c|}
\hline
\multicolumn{2}{|c|}{\multirow{2}{*}{model}} &\multirow{2}{*}{flops}&\multirow{2}{*}{params} & \multicolumn{4}{c|}{cifar-10}&\multicolumn{4}{c|}{sun}\\
\cline{5-12}

\multicolumn{2}{|c|}{} & & & 12bits & 24bits & 32bits & 48bits & 12bits & 24bits & 32bits & 48bits \\
\hline

\multicolumn{2}{|c|}{teacher} &4.12g	&25.56m	&0.87841	&0.89512	&0.9014	&0.90601	&0.83587	&0.85736	&0.86297	&0.87103\\ 
\hline

%0.25x-----------------
\multirow{4}{*}{$0.25\times$} & stu-1 & 0.15g & 1.03m &
0.70746 & 0.73458 &	0.74909 &	0.75833 &	0.69618 &	0.76631 &	0.78075 &	0.78787 \\
\cline{2-12}

\multirow{4}{*}{} & stu-2 &0.19g	&1.08m	&0.7629	&0.79111	&0.80039	&0.80519	&0.73539	&0.79714	&0.80753	&0.81195\\
\cline{2-12}

\multirow{4}{*}{} & stu-3 &0.26g	&1.43m	&0.84684	&0.86443	&0.87384	&0.88268	&0.79284	&0.83442	&0.84350	&0.84353\\
\cline{2-12}

\multirow{4}{*}{} & stu-4 & 0.29g	&1.99m	&0.85901	&0.87269	&0.8836	&0.88728	&0.81997	&0.84620	&0.85041	&0.85036\\
\hline
\end{tabular}
\label{table_map}
\end{table*}

输出结果为
在这里插入图片描述
这里使用\begin{table*}[t]有一个星号,是为了让表格宽度与页面等宽而不是二分之一宽。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com