这是一些表格。我们希望将表格单元格中的文本垂直居中。如何编写这样的代码?
表格1:
\documentclass[conference]{IEEEtran}
\usepackage{graphicx}
\usepackage[utf8]{inputenc}
\usepackage{longtable}
\usepackage{array}
\usepackage{hyperref}
\usepackage{tabularx}
\usepackage{bm}
\usepackage{cite}
\begin{document}
\begin{center}
\label{some table}
\begin{tabularx}{4.9in}{ p{5cm} | c | c | c | c | c | c | c }
\hline
\textbf{Input of Model} & \textbf{B@1} & \textbf{B@2} & \textbf{B@3} & \textbf{B@4} & \textbf{M} & \textbf{R} & \textbf{C} \\ \hline
\textbf{image embedding} & $70.3$ & $52.9$ & $38.3$ & $27.5$ & $24.3$ & $51.8$ & $99.5$ \\ \hline
\textbf{detected objects and directly related terms} & $63.3$ & $43.4$ & $29.1$ & $20$ & $19.8$ & $46.1$ & $74.3$ \\ \hline
\end{tabularx}
\end{center}
\end{table*}
\end{document}
表 2:
\documentclass[conference]{IEEEtran}
\usepackage{graphicx}
\usepackage[utf8]{inputenc}
\usepackage{longtable}
\usepackage{array}
\usepackage{hyperref}
\usepackage{tabularx}
\usepackage{bm}
\usepackage{cite}
\begin{document}
\begin{center}
\label{some table 2}
\begin{tabularx}{7.4in}{ c | p{1.8cm} | p{1.8cm} | p{3.5cm} | p{3.5cm} | p{3.5cm} }
\hline
\textbf{Image} & \textbf{Detected} & \textbf{Indirectly Related} & \textbf{Sentences Generated by Model with Indirectly Related} & \textbf{Sentences Generated by Model without Indirectly Related} & \textbf{Standard Model} \\ \hline
\raisebox{-0.5\height}{\includegraphics[width=0.8in]{some image}} & Winsor chair, deck chair, furnishing, pot & item of furniture, upholstered, found in house, chairs & 0) a dining room with a table and chairs\newline 1) a dining room with a table, chairs and a table\newline 2) a dining room with a table and chairs and a fireplace & 0) a table with a vase of flowers on it\newline 1) a dining room with a table and chairs\newline 2) a table with a vase of flowers on it & 0) a table with a vase of flowers on it\newline 1) a table with a vase of flowers on it\newline 2) a table with a vase of flowers on it \\ \hline
\end{tabularx}
\end{center}
\end{table*}
\end{document}
答案1
关于你的第二张桌子和你的评论:
\documentclass[conference]{IEEEtran}
\usepackage{graphicx}
\usepackage{array}
\begin{document}
\begin{tabular}{ c | m{1.8cm} | m{1.8cm} | m{3.5cm} | m{3.5cm} | m{3.5cm} }
\hline
\textbf{Image} & \textbf{Detected} & \textbf{Indirectly Related} & \textbf{Sentences Generated by Model with Indirectly Related} & \textbf{Sentences Generated by Model without Indirectly Related} & \textbf{Standard Model} \\ \hline
\raisebox{-0.5\height}{\includegraphics[width=0.8in]{example-image}} & Winsor chair, deck chair, furnishing, pot & item of furniture, upholstered, found in house, chairs & 0) a dining room with a table and chairs\newline 1) a dining room with a table, chairs and a table\newline 2) a dining room with a table and chairs and a fireplace & 0) a table with a vase of flowers on it\newline 1) a dining room with a table and chairs\newline 2) a table with a vase of flowers on it & 0) a table with a vase of flowers on it\newline 1) a table with a vase of flowers on it\newline 2) a table with a vase of flowers on it \\ \hline
\end{tabular}
\end{document}
尽管如此,我还是建议以下结果:
\documentclass[conference]{IEEEtran}
\usepackage{graphicx}
\usepackage{array}
\usepackage{ragged2e}
\usepackage{tabularx}
\newcolumntype{P}[1]{>{\RaggedRight}p{#1}}
\usepackage{enumitem}
\newlist{tabenumi}{enumerate}{1}
\setlist[tabenumi]{label={\arabic*.},nosep,leftmargin=*,after=\vspace{-\baselineskip},start=0,before=\vspace{-0.5\baselineskip}}
\usepackage[export]{adjustbox}
\usepackage{booktabs}
\usepackage{multirow}
\begin{document}
\begin{table*}
\caption{Table caption}
\label{some table 2}
\begin{tabularx}{\textwidth}{ c P{1.8cm} P{2.1cm} X X X }
\toprule
\multirow{2}{*}{Image}
& \multirow{2}{*}{Detected}
& \multirow{2}{*}{Indirectly Related}
& \multicolumn{2}{c}{Sentences Generated by Model}
& \multirow{2}{*}{Standard Model} \\
&&& \emph{with} Indirectly Related
& \emph{without} Indirectly Related \\
\midrule
\includegraphics[width=0.8in,valign=t]{example-image}
& Winsor chair, deck chair, furnishing, pot
& item of furniture, upholstered, found in house, chairs
& \begin{tabenumi}
\item a dining room with a table and chairs
\item a dining room with a table, chairs and a table
\item a dining room with a table and chairs and a fireplace
\end{tabenumi}
& \begin{tabenumi}
\item a table with a vase of flowers on it
\item a dining room with a table and chairs
\item a table with a vase of flowers on it
\end{tabenumi}
& \begin{tabenumi}
\item a table with a vase of flowers on it
\item a table with a vase of flowers on it
\item a table with a vase of flowers on it
\end{tabenumi} \\
\bottomrule
\end{tabularx}
\end{table*}
\end{document}
变化简要概述:
- 删除了垂直线并
\hline
用 中的规则替换booktabs
。 - 使用
tabularx
和X
列使表格与文本宽度一样宽。 - 将文本的对齐方式从两端对齐改为右对齐,以避免单词之间出现较大的空白。
- 删除了第 4 列和第 5 列标题中的重复内容。删除了所有粗体,并为第 4 列和第 5 列之间的差异添加了更细微的斜体突出显示。
- 引入了一个新的枚举类列表以供表格使用。
- 将所有行中的所有内容顶部对齐(关于图像,借助
valign
fromadjustbox
)。 - 借助 实现垂直居中的列标题
multirow
。 - ...
答案2
您可能正在寻找类似这样的第一个表格:
编辑(1):
由于您在答案的第一个版本之后提供了 mwe,因此在第二个版本中被视为您的文档类,而不是第一个表格的宽度。
\documentclass[conference]{IEEEtran}
\usepackage{siunitx}
\usepackage{booktabs, tabularx}
\renewcommand\tabularxcolumn[1]{m{#1}} % <--- for vertical centering of "X" cell contents
%---------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%
\usepackage{lipsum}
\begin{document}
\lipsum[1]
\begin{table}[ht]
\centering
\setlength\tabcolsep{3pt}
\caption{table caption ...}
\label{some table 1}
\begin{tabularx}{\linewidth}{>{\bfseries\raggedright}X | c | c | c | c | c | c | c}
\hline
Input of Model & \textbf{B@1} & \textbf{B@2} & \textbf{B@3}
& \textbf{B@4} & \textbf{M} & \textbf{R} & \textbf{C} \\ \hline
image embedding & $70.3$ & $52.9$ & $38.3$
& $27.5$ & $24.3$ & $51.8$ & $99.5$ \\ \hline
detected objects and directly related terms
& $63.3$ & $43.4$ & $29.1$
& $20$ & $19.8$ & $46.1$ & $74.3$ \\ \hline
\end{tabularx}
\end{table}
\begin{table}[ht]
\centering
\setlength\tabcolsep{4pt}
\caption{table caption ...}
\label{some table 2}
\begin{tabularx}{\linewidth}{@{}>{\bfseries\raggedright}X *{7}{c}@{}}
\toprule
Input of Model & \textbf{B@1} & \textbf{B@2} & \textbf{B@3}
& \textbf{B@4} & \textbf{M} & \textbf{R} & \textbf{C} \\
\midrule
Image embedding & $70.3$ & $52.9$ & $38.3$
& $27.5$ & $24.3$ & $51.8$ & $99.5$ \\
\addlinespace
Detected objects and directly related terms
& $63.3$ & $43.4$ & $29.1$
& $20$ & $19.8$ & $46.1$ & $74.3$ \\
\bottomrule
\end{tabularx}
\end{table}
\begin{table}[ht]
\centering
\setlength\tabcolsep{4pt}
\caption{table caption ...}
\label{some table 3}
\begin{tabularx}{\linewidth}{@{}>{\bfseries\raggedright}X *{7}{S[table-format=2.1]}@{}}
\toprule
Input of Model & \textbf{B@1} & \textbf{B@2} & \textbf{B@3}
& \textbf{B@4} & \textbf{M} & \textbf{R} & \textbf{C} \\
\midrule
Image embedding & 70.3 & 52.9 & 38.3
& 27.5 & 24.3 & 51.8 & 99.5 \\
\addlinespace
Detected objects and directly related terms
& 63.3 & 43.4 & 29.1
& 20 & 19.8 & 46.1 & 74.3 \\
\bottomrule
\end{tabularx}
\end{table}
\end{document}
笔记:
- 你的 mwe 中的代码仍然不完整
- 您使用
tabularx
没有任何列的表格环境X
(这会导致表格看起来很丑陋) - 在没有表格标题的情况下标记表格以供参考是没有意义的
- 第一个表格可以容纳一列的宽度
- 我更喜欢最后一张表格的例子
编辑(2):
让我用第二个表格的例子来完成答案。我会按照建议的方式设计它莱安德里斯在他很好的回答的第二个例子中,但有细微的差别(在代码中指示% <---
):
\documentclass[conference]{IEEEtran}
\usepackage{booktabs, multirow, tabularx}
\usepackage{graphicx}
\usepackage{enumitem}
%---------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%
\begin{document}
\begin{table*}
\caption{Table with image ...}
\label{some table 2}
\setlist[enumerate]{nosep,
leftmargin=*,
label={\arabic*.},start=0,
before=\vspace{-0.6\baselineskip},
after=\vspace{-\dimexpr\baselineskip+\partopsep} % <---
} % <---
\setlength\tabcolsep{3pt} % <---
\begin{tabularx}{\textwidth}{*{3}{>{\raggedright}p{2cm}} XXX }
\toprule
\multirow{2.4}{=}{Image} % <---
& \multirow{2.4}{=}{Detected} % <---
& \multirow{2.4}{=}{Indirectly Related} % <---
& \multicolumn{2}{c}{Sentences Generated by Model}
& \multirow{2.4}{=}{Standard Model} \\ % <---
\cmidrule(lr){4-5}
&&& with Indirectly Related
& without Indirectly Related \\
\midrule
\multirow{6}{*}{\includegraphics[width=\linewidth]{example-image}} % <---
& Winsor chair, deck chair, furnishing, pot
& item of furniture, upholstered, found in house, chairs
& \begin{enumerate} % <---
\item a dining room with a table and chairs
\item a dining room with a table, chairs and a table
\item a dining room with a table and chairs and a fireplace
\end{enumerate}
& \begin{enumerate} % <---
\item a table with a vase of flowers on it
\item a dining room with a table and chairs
\item a table with a vase of flowers on it
\end{enumerate}
& \begin{enumerate} % <---
\item a table with a vase of flo\-wers on it
\item a table with a vase of flowers on it
\item a table with a vase of flowers on it
\end{enumerate} \\
\bottomrule
\end{tabularx}
\end{table*}
\end{document}