我正在尝试制作一个表格,其中有一些单元格跨越多行,如下所示:
(我在我的桌子上使用了打印屏幕,然后对其进行了编辑以显示我想要的样子)。
我一直在寻找,但没有找到解决办法,尽管我见过类似的问题,但无法将他们的解决方案应用到我的问题上。
这是我正在使用的代码:
\begin{table}
\caption{Funções e conexões do conector X1}
\begin{center}
\begin{tabular}{|c|l|l|}
\hline
\textbf{X1} &\textbf{Descrição} &\textbf{Função} \\
\hline
1 &C &Saída digital 1 \\
\hline
2 &NA &a relé \\
\hline
3 &Dl1 &Entradas digitais \\
\hline
4 &Dl2 &optoacopladas \\
\hline
5 &COM 1,2 &1 e 2 \\
\hline
6 &Dl3 &Entrada digital 3 \\
\hline
7 &COM3 &optoacoplada \\
\hline
8 &Al1 + &Entrada analógica 1 \\
\hline
9 &Al1 - &diferencial \\
\hline
\end{tabular}
\end{center}
\end{table}
谢谢您的帮助。
答案1
最简单的方法就是在您想要合并行时不绘制完整的水平线:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{booktabs}
\begin{document}
\begin{table}
\caption{Funções e conexões do conector X1}
\centering
\begin{tabular}{|c|l|l|}
\hline
\textbf{X1} &\textbf{Descrição} &\textbf{Função} \\\hline
1 &C &Saída digital 1 \\\cline{1-2}
2 &NA & a relé\\\hline
3 &Dl1 &Entradas digitais\\\cline{1-2}
4 &Dl2 & optoacopladas\\\hline
5 &COM 1,2 &1 e 2 \\\cline{1-2}
6 &Dl3 &Entrada digital 3 \\\cline{1-2}
7 &COM3 &optoacoplada \\\hline
8 &Al1 + &Entrada analógica 1 \\\cline{1-2}
9 &Al1 - &diferencial \\\hline
\end{tabular}
\end{table}
\begin{table}
\caption{Funções e conexões do conector X1}
\centering
\begin{tabular}{cll}
\toprule
\textbf{X1} &\textbf{Descrição} &\textbf{Função} \\\midrule
1 &C &Saída digital 1 \\\cmidrule{1-2}
2 &NA & a relé \\\midrule
3 &Dl1 &Entradas digitais \\\cmidrule{1-2}
4 &Dl2 & optoacopladas \\\midrule
5 &COM 1,2 &1 e 2 \\\cmidrule{1-2}
6 &Dl3 &Entrada digital 3 \\\cmidrule{1-2}
7 &COM3 &optoacoplada \\\midrule
8 &Al1 + &Entrada analógica 1 \\\cmidrule{1-2}
9 &Al1 - &diferencial \\\bottomrule
\end{tabular}
\end{table}
\end{document}
第二个表格显示了如何根据其中列出的指导方针booktabs
(或其中一些指导方针)重新格式化表格。
或者,如果采用这种方法,您可以完全删除部分水平线:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{booktabs}
\begin{document}
\begin{table}
\caption{Funções e conexões do conector X1}
\centering
\begin{tabular}{cll}
\toprule
\textbf{X1} &\textbf{Descrição} &\textbf{Função} \\\midrule
1 &C &Saída digital 1 \\
2 &NA & a relé \\\midrule
3 &Dl1 &Entradas digitais \\
4 &Dl2 & optoacopladas \\\midrule
5 &COM 1,2 &1 e 2 \\
6 &Dl3 &Entrada digital 3 \\
7 &COM3 &optoacoplada \\\midrule
8 &Al1 + &Entrada analógica 1 \\
9 &Al1 - &diferencial \\\bottomrule
\end{tabular}
\end{table}
\end{document}
答案2
该 makecell
包仅适用于多行单元格(无论是否多行)。此外,如上所述,仅使用水平线看起来要好得多。此外,使用规则命令会给booktabs
单元格提供一些间距。我也使用该caption
包:只需加载它就可以在标题和表格之间提供更好的垂直间距。作为另一条评论,您不应该 center
在这里使用环境:它会将自己的垂直间距添加到提供的垂直间距中table
。
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{caption}
\usepackage{booktabs}
\usepackage{multirow}
\usepackage{makecell}
\renewcommand{\theadfont}{\bfseries}
\renewcommand{\cellalign}{lc}
\begin{document}
\begin{table}
%\renewcommand{\arraystretch}{1.5}
\centering
\caption{Funções e conexões do conector X1}
\begin{tabular}{cll}
\toprule
\thead{X1} &\thead{Descrição} &\thead{Função} \\
\toprule
1 &C &\multirowcell{2}{Saída digital 1 \\a relé}\\
\cmidrule{1-2}
2 &NA & \\
\midrule \addlinespace
3 &Dl1 & \multirowcell{2}{Entradas digitais \\ optoacopladas}\\
\cmidrule{1-2}
4 &Dl2 &\\
\midrule
5 &COM 1,2 &\multirowcell{3}{\\[-2ex]1 e 2 \\ Entrada digital 3 \\ optoacoplada}\\
\cmidrule{1-2}
6 &Dl3 & \\
\cmidrule{1-2}
7 &COM3 & \\
\midrule \addlinespace
8 &Al1 + & \multirowcell{2}{Entrada analógica 1 \\ diferencial} \\
\cmidrule{1-2}
9 &Al1 - & \\
\bottomrule
\end{tabular}
\end{table}
\end{document}