LaTeX之双栏模板表格布局(单双栏满宽+不满宽) 您所在的位置:网站首页 怎么设置双页面 LaTeX之双栏模板表格布局(单双栏满宽+不满宽)

LaTeX之双栏模板表格布局(单双栏满宽+不满宽)

2023-12-21 18:28| 来源: 网络整理| 查看: 265

引言

        跟Microsoft word一样,LaTex也是一套排版系统。二者的区别是前者排版属于富文本编辑,后者排版更像是写程序。LaTeX的使用者只要调用模板即可,完全不用去处理字体样大小、位置、目录生成和图片公式序号等诸多细节。这样,我们能够更专注地编辑内容。

        表格,又称为表,既是一种可视化交流模式,又是一种组织整理数据的手段。人们在通讯交流、科学研究以及数据分析活动当中广泛采用着形形色色的表格。各种表格常常会出现在印刷介质、手写记录、计算机软件、建筑装饰、交通标志等许许多多地方。随着上下文的不同,用来确切描述表格的惯例和术语也会有所变化。此外,在种类、结构、灵活性、标注法、表达方法以及使用方面,不同的表格之间也炯然各异。在各种书籍和技术文章当中,表格通常放在带有编号和标题的浮动区域内,以此区别于文章的正文部分。

        好了,废话不多说(主要是CSDN发文助手太不智能了,老是检测我文章质量不行)。直接上干货。论文中的表格排版是很重要的!每个科研人都希望弄出一个美观又使用的表格,这应该没有人反驳吧?

        博客中第一、二、三、四、五章节适用于Elsevier LaTeX模板(“cas-dc-template.tex”这个文件)。

        博客的第六章节适用于Elsevier和其他LaTeX论文模板,普适性更大。

一、使用两栏的LaTeX模板,表格占整页宽度

        实现整页宽度的思路是\begin{tabular*}{\linewidth}{@{}LLLL@{}}中的“\linewidth”,另外\begin{table*}中的“*”表示这个表格在两栏模板中使用一栏。

        LaTeX代码

\begin{table*} \caption{This is a table with full width in single column.} \label{tab_fwsc} \begin{tabular*}{\linewidth}{@{}LLLL@{}} \toprule Col 1 & Col 2 & Col 3 & Col4\\ \midrule 12345 & 12345 & 123 & 12345 \\ 12345 & 12345 & 123 & 12345 \\ 12345 & 12345 & 123 & 12345 \\ 12345 & 12345 & 123 & 12345 \\ 12345 & 12345 & 123 & 12345 \\ \bottomrule \end{tabular*} \end{table*}

        生成的PDF效果(可以看到是整页宽度)

二、使用两栏的LaTeX模板,表格宽度自定义

        实现的思路:还是\begin{tabular*}{\linewidth}{@{}LLLL@{}}中的“\linewidth”,在“\linewidth”之前添加一个(0,1)的小数值可以自定义表格的宽度,例如,我在下面的代码中取了0.8。当然这个数值主要取决于你的表格内容,具体取多少、表格美观就可以。

        LaTeX代码

\begin{table*} \caption{This is a table with diy width in single column.} \label{tab_dwsc} \begin{tabular*}{0.8\linewidth}{@{}LLLL@{}} \toprule Col 1 & Col 2 & Col 3 & Col4\\ \midrule 12345 & 12345 & 123 & 12345 \\ 12345 & 12345 & 123 & 12345 \\ 12345 & 12345 & 123 & 12345 \\ 12345 & 12345 & 123 & 12345 \\ 12345 & 12345 & 123 & 12345 \\ \bottomrule \end{tabular*} \end{table*}

        生成的PDF效果(可以看到比整页宽度要小20%)

三、使用两栏的LaTeX模板,表格宽度自适应

        实现的思路:其实上面的自定义宽度基本够用了。但是,如果不想调“\linewidth”前面的小数值,可以试试自适应的宽度。这会儿主要用\begin{tabular*}{\tblwidth}{@{}LLLL@{}}中的“\tblwidth”来实现这个需求。

        LaTeX代码

\begin{table*} \caption{This is a table with adaptive width in single column.} \label{tab_awsc} \begin{tabular*}{\tblwidth}{@{}LLLL@{}} \toprule Col 1 & Col 2 & Col 3 & Col4\\ \midrule 12345 & 12345 & 123 & 12345 \\ 12345 & 12345 & 123 & 12345 \\ 12345 & 12345 & 123 & 12345 \\ 12345 & 12345 & 123 & 12345 \\ 12345 & 12345 & 123 & 12345 \\ \bottomrule \end{tabular*} \end{table*}

        生成的PDF效果(自适应的宽度,好像不是很明显,跟上面自定义的差不多)

四、使用两栏的模板,表格占满一栏宽度

        实现的思路:\begin{table}不带星号,然后在\begin{tabular*}{\linewidth}{@{}LLLL@{}}中使用“\linewidth”这个东西。

        LaTeX代码

\begin{table} \caption{This is a table with full width in double column.} \label{tab_fwdc} \begin{tabular*}{\linewidth}{@{}LLLL@{}} \toprule Col 1 & Col 2 & Col 3 & Col4\\ \midrule 12345 & 12345 & 123 & 12345 \\ 12345 & 12345 & 123 & 12345 \\ 12345 & 12345 & 123 & 12345 \\ 12345 & 12345 & 123 & 12345 \\ 12345 & 12345 & 123 & 12345 \\ \bottomrule \end{tabular*} \end{table}

        生成的PDF效果(充满了一栏布局的宽度)

五、使用两栏的模板,表格占一栏宽度+自适应

        实现的思路:很简单,\begin{table}不带星号+\begin{tabular*}{\tblwidth}{@{}LLLL@{}}中的“\tblwidth”。

        LaTeX代码

\begin{table} \caption{This is a table with adaptive width in double column.} \label{tab_awdc} \begin{tabular*}{\tblwidth}{@{}LLLL@{}} \toprule Col 1 & Col 2 & Col 3 & Col4\\ \midrule 12345 & 12345 & 123 & 12345 \\ 12345 & 12345 & 123 & 12345 \\ 12345 & 12345 & 123 & 12345 \\ 12345 & 12345 & 123 & 12345 \\ 12345 & 12345 & 123 & 12345 \\ \bottomrule \end{tabular*} \end{table}

        生成的PDF效果(跟占满宽度没啥区别...我也不知道为什么)

六、(更新) 针对其他LaTeX模板表格满宽和自定义宽度的设置

        很遗憾上述的方法仅适用于Elsevier的LaTeX模板,而对于其他模板,会出现下面的问题:右边空出空白,影响整个表格的美观性。

        上面对应的源代码是

\documentclass[twocolumn]{article} \usepackage{multirow} \title{A demo to DIY set the table width in double column template} \author{See Chen} \date{March 2023} \begin{document} \maketitle \section{Introduction} This is an introduction with a specific table \ref{tab_demo}. \begin{table*} \caption{A Table Demo} \label{tab_demo} \begin{tabular*}{0.99\linewidth}{@{}ccccccccc@{}} \hline \multirow{2}{*}{Animal} & \multicolumn{2}{c}{Sample 1} & \multicolumn{2}{c}{Sample 2} & \multicolumn{2}{c}{Sample 3} & \multicolumn{2}{c}{Sample 4} \\ & Weight & Color & Weight & Color & Weight & Color & Weight & Color \\ \hline Dog & 20.1 & White & 18.0 & Gray & 30.5 & Black & 25.2 & White \\ Cat & 10.2 & Yellow & 11.2 & Black & 11.5 & White & 12.5 & White \\ Fox & 15.5 & Gold & 15.6 & Gold & 16.5 & Gold & 17.0 & Gold \\ Duck & 2.4 & White & 3.0 & White & 4.0 & White & 3.8 & White \\ \hline \end{tabular*} \end{table*} \end{document}

        解决办法:使用“\tabcolsep=len”命令,其中len是表格宽度大小,可以设置为具体长度,也可以通过\linewidth设置。通过这个方法,实现的表格宽度设置效果如下:可以看出表格内容已经居中显示了。

        相应的代码如下:

\documentclass[twocolumn]{article} \usepackage{multirow} \title{A demo to DIY set the table width in double column template} \author{See Chen} \date{March 2023} \begin{document} \maketitle \section{Introduction} This is an introduction with a specific table \ref{tab_demo}. \begin{table*} \caption{A Table Demo} \label{tab_demo} \centering %\tabcolsep=0.35cm \tabcolsep=0.016\linewidth \begin{tabular}{ccccccccc} \hline \multirow{2}{*}{Animal} & \multicolumn{2}{c}{Sample 1} & \multicolumn{2}{c}{Sample 2} & \multicolumn{2}{c}{Sample 3} & \multicolumn{2}{c}{Sample 4} \\ & Weight & Color & Weight & Color & Weight & Color & Weight & Color \\ \hline Dog & 20.1 & White & 18.0 & Gray & 30.5 & Black & 25.2 & White \\ Cat & 10.2 & Yellow & 11.2 & Black & 11.5 & White & 12.5 & White \\ Fox & 15.5 & Gold & 15.6 & Gold & 16.5 & Gold & 17.0 & Gold \\ Duck & 2.4 & White & 3.0 & White & 4.0 & White & 3.8 & White \\ \hline \end{tabular} \end{table*} \end{document}

        如果表格宽度太大怎么办?例如(已经超出双栏宽度,快到页面边界了)

        解决办法:使用\resizebox{1.0\linewidth}{!}命令(需要配合\usepackage{graphicx}同时使用),该命令可以使表格宽度缩小至LaTeX论文单行宽度,大的文字会变小,小的文字会变大。一般用在大的文字缩小这种情况,后面的情况不需要使用、只需要扩大表格宽度变得好看就好。下面是使用了该命令的效果:

        上面的代码如下:

\documentclass[twocolumn]{article} \usepackage{multirow} \usepackage{graphicx} \title{A demo to DIY set the table width in double column template} \author{See Chen} \date{March 2023} \begin{document} \maketitle \section{Introduction} This is an introduction with a specific table \ref{tab_demo}. \begin{table*} \caption{A Table Demo} \label{tab_demo} \resizebox{1.0\linewidth}{!}{ \begin{tabular}{ccccccccccccc} \hline Animal & Weight & Color & Weight & Color & Weight & Color & Weight & Color & Weight & Color & Weight & Color\\ \hline Dog & 20.1 & White & 18.0 & Gray & 30.5 & Black & 25.2 & White & 25.2 & White & 25.2 & White\\ Cat & 10.2 & Yellow & 11.2 & Black & 11.5 & White & 12.5 & White & 12.5 & White & 12.5 & White\\ Fox & 15.5 & Gold & 15.6 & Gold & 16.5 & Gold & 17.0 & Gold & 16.5 & Gold & 17.0 & Gold\\ Duck & 2.4 & White & 3.0 & White & 4.0 & White & 3.8 & White & 4.0 & White & 3.8 & White\\ \hline \end{tabular} } \end{table*}

        其实,resizebox也可以和tabcolsep同时配合使用,如果你想的话。前者使表格宽度自适应到LaTeX单栏一行宽度,而后者使表格列宽被设为等同大小。配合起来的效果在一定程度下是叠加的。

七、结束语

        上面的记录都是非常简单的思路,如果有更加简单有效(最好不要导包)的方式,敬请各位大神留言。我感觉最强大的功能是tabcolsep命令和resizebox命令。另外如果想调整表格行间距的话,可以看看LaTeX表格行高、列宽设置这篇博客。



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有