LaTeX插入表格

LaTeX的问题还是优先查阅《IShort》和刘海洋老师的《LaTeX入门》。
也可参考 https://www.latexstudio.net/hulatex/package/table.htm
细节之后补上。

其他参考链接

Excel插件的使用

使用Excel插件可以方便地生成LaTeX代码,尤其是存在合并行合并列等复杂操作时。
但是Excel插件生成的LaTeX代码是没有调整列宽度的。

三线表

《Ishort》中“3.6.3 横线”

在科技论文排版中广泛应用的表格形式是三线表,形式干净简明。三线表由booktabs 宏包支持,它提供了\toprule、\midrule 和\bottomrule 命令用以排版三线表的三条线,以及和\cline 对应的\cmidrule。除此之外,最好不要用其它横线以及竖线:

\begin{tabular}{cccc}
\toprule
& \multicolumn{3}{c}{Numbers} \\
\cmidrule{2-4}
& 1 & 2 & 3 \\
\midrule
Alphabet & A & B & C \\
Roman & I & II& III \\
\bottomrule
\end{tabular}

用“booktabs”宏包绘制一个LaTeX的三线表,要能指定每一列的宽度和表格的总宽度,还要能设置单元格的对齐方式。

\documentclass{article}
\usepackage{booktabs}
\usepackage{tabularx}

\begin{document}

\begin{table}
  \centering
  \begin{tabularx}{0.8\textwidth}{>{\raggedleft\arraybackslash}p{2cm}>{\centering\arraybackslash}p{4cm}X}
    \toprule
    Column 1 & Column 2 & Column 3 \\
    \midrule
    Text & More text & Some really long text that should wrap to the next line \\
    Text & More text & Some really long text that should wrap to the next line \\
    \bottomrule
  \end{tabularx}
  \caption{A three-line table.}
\end{table}

\end{document}

在上面的示例中,使用>{}来设置单元格的对齐方式。例如,>{\raggedleft}表示右对齐,>{\centering}表示居中对齐,>{\arraybackslash}用于解决\与\hline等命令之间的冲突。
对于>{\arraybackslash} 的具体含义,又咨询了以下ChatGPT-3.5,得到的回答如下

在tabular环境中,使用\\命令表示换行,同时\hline命令用于生成水平线。然而,在某些情况下,这些命令可能会出现一些问题,特别是当它们位于表格的最后一行时。这是因为在表格最后一行的末尾,\hline命令会导致一些意外的垂直空格,而\\命令则可能导致一些意外的错误。
为了解决这些问题,>{\arraybackslash}命令可以用于表格列格式说明符的末尾,以确保\hline和\\命令正常工作。具体来说,>{\arraybackslash}命令会将\arraybackslash命令插入到表格列中,以替换原始的\\命令。这样,即使\出现在表格的最后一行,它也不会产生任何错误或意外的空格。
在上面的例子中,使用了>{\raggedleft\arraybackslash}来设置第一列的对齐方式,并使用了>{\arraybackslash}来确保\\命令正常工作。

设置表格宽度

相关方法有很多,最后感觉比较好用的是下面这篇文章介绍的
https://blog.csdn.net/weixin_41271939/article/details/121993833

采用box(可以单独修改任意列列宽、比较推荐这种方法)

\begin{center}
  \begin{tabular}{ccccc}
    \toprule[1.5pt]
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %\textwidth 是每一行的宽度.[0.1\textwidth]设定单元格宽度
    % [c]  单元格文本居中对齐
    % {name} 单元格内容
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    \makebox[0.1\textwidth][c]{name} & \makebox[0.2\textwidth][c]{taskA} & \makebox[0.1\textwidth][c]{taskB}
                                     & \makebox[0.4\textwidth][c]{taskC} & \makebox[0.2\textwidth][c]{taskD}             \\
    \midrule[1pt]
    xiaowang                         & 80                                & 50                                & 70  & 90  \\
    laowang                          & 90                                & 70                                & 80  & 100 \\
    gblaowang                        & 100                               & 100                               & 100 & 100 \\
    \bottomrule[1.5pt]
  \end{tabular}
\end{center}

采用\setlength (修改单元格列宽、所有的列宽都会修改)

这个方法设置的是单元格的宽度,代码如下:

\begin{center}
  \setlength{\tabcolsep}{7mm}{
    \begin{tabular}{ccccc}
      \toprule[1.5pt]
      name      & taskA & taskB & taskeC & taskD \\
      \midrule[1pt]
      xiaowang  & 80    & 50    & 70     & 90    \\
      laowang   & 90    & 70    & 80     & 100   \\
      gblaowang & 100   & 100   & 100    & 100   \\
      \bottomrule[1.5pt]
    \end{tabular}   }%%%%%%注意,这边还有个大括号
\end{center}

如何设置与页面同宽?

可参考的链接
https://www.zhihu.com/question/376527391/answer/2578990308?utm_id=0
https://blog.csdn.net/robert_chen1988/article/details/79505794
https://blog.csdn.net/qq_36158230/article/details/125833281

此问题直接咨询的ChatGPT-3.5,尚未验证。
LaTeX绘制表格如何与页面同宽?
如果想让LaTeX绘制的表格与页面同宽,可以使用tabularx宏包,它提供了一个tabularx环境,可以自动调整表格宽度,使其与页面同宽。下面是一个例子:

\documentclass{article}
\usepackage{tabularx}
\usepackage{lipsum} % for dummy text
\begin{document}

\lipsum[1]

\begin{table}[htbp]
  \centering
  \begin{tabularx}{\textwidth}{|X|X|X|}
    \hline
    Column 1 & Column 2 & Column 3 \\
    \hline
    This is a long sentence that should wrap to the next line & This is a long sentence that should wrap to the next line & This is a long sentence that should wrap to the next line \\
    \hline
  \end{tabularx}
  \caption{A table with equal column width}
  \label{tab:my-table}
\end{table}

\lipsum[2]

\end{document}

在上面的例子中,tabularx环境的第一个参数\textwidth指定了表格的总宽度为页面宽度,X列类型表示该列的宽度应该根据表格剩余的宽度自动调整。这样,LaTeX会自动计算每列的宽度,使得表格的总宽度等于页面宽度。如果需要调整列宽,可以使用\hsize命令来指定列的宽度,例如\begin{tabularx}{\textwidth}{|p{3cm}|X|X|}表示第一列宽度为3cm,第二列和第三列的宽度会根据表格剩余的宽度自动调整。

那如何同时设置单元格的对齐方式呢?
可以使用array宏包来设置单元格的对齐方式。array宏包提供了一系列的列类型,可以用于指定单元格的对齐方式。

下面是一些常用的列类型及其对齐方式:

l:左对齐
c:居中对齐
r:右对齐
p{width}:指定宽度的左对齐列
m{width}:指定宽度的居中对齐列
b{width}:指定宽度的右对齐列
|:在单元格左右两侧添加竖线
在使用tabularx环境时,可以在X列类型的参数中指定对齐方式,例如:

\begin{tabularx}{\textwidth}{|l|c|r|}
  \hline
  Left & Center & Right \\
  \hline
  This is a long sentence that should wrap to the next line & This is a long sentence that should wrap to the next line & This is a long sentence that should wrap to the next line \\
  \hline
\end{tabularx}

在上面的例子中,第一列左对齐,第二列居中对齐,第三列右对齐。如果需要使用自定义的列类型,可以使用\newcolumntype命令定义,例如:

\usepackage{array}
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}

在上面的例子中,定义了一个C列类型,它是一个指定宽度的居中对齐列,宽度由{#1}参数指定。可以在tabularx环境中使用C列类型,例如:

\begin{tabularx}{\textwidth}{|C{2cm}|X|X|}
  \hline
  Centered & This is a long sentence that should wrap to the next line & This is a long sentence that should wrap to the next line \\
  \hline
\end{tabularx}

在上面的例子中,第一列使用了C{2cm}列类型,宽度为2cm,居中对齐。

能不能对上面例子中的三列的宽度分别设置?而不是三列的宽度相等?
可以使用tabularx环境的X列类型来自动计算剩余宽度,同时使用p、m、b等列类型来指定固定宽度的列。例如,如果要将表格的三列宽度设置为2cm、4cm和自动计算剩余宽度,可以这样写:

\begin{tabularx}{\textwidth}{|p{2cm}|p{4cm}|X|}
  \hline
  Column 1 & Column 2 & Column 3 \\
  \hline
  Text & More text & Some really long text that should wrap to the next line \\
  \hline
\end{tabularx}

在上面的例子中,第一列和第二列分别使用了p{2cm}和p{4cm}来指定固定宽度,第三列使用了X来自动计算剩余宽度,使表格的总宽度等于页面宽度。你可以根据需要自行调整列的宽度。

在刚刚举的例子中设置单元格的对齐方式。
可以使用array宏包中提供的列类型来设置单元格的对齐方式,例如:

\begin{tabularx}{\textwidth}{|>{\raggedleft\arraybackslash}p{2cm}|>{\centering\arraybackslash}p{4cm}|X|}
  \hline
  Column 1 & Column 2 & Column 3 \\
  \hline
  Text & More text & Some really long text that should wrap to the next line \\
  \hline
\end{tabularx}

在上面的例子中,第一列使用了p{2cm}指定了固定宽度,同时使用了>{\raggedleft\arraybackslash}来将单元格内容右对齐;第二列也使用了p{4cm}指定了固定宽度,同时使用了>{\centering\arraybackslash}来将单元格内容居中对齐;第三列使用了X自动计算剩余宽度,并且默认左对齐。你可以根据需要自行调整对齐方式和列宽。

表格跨行

https://blog.csdn.net/accumulate_zhang/article/details/127001116
https://blog.csdn.net/hnjzsyjyj/article/details/123101907

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值