表格文本左对齐但标题保持居中

表格文本左对齐但标题保持居中

这是表格,我希望表格的标题(即 Ref.no、目标等)保持在中心,但是文本会在左侧对齐。其次,如果我在单元格中写入更多单词或小段落,则它应该保持在同一单元格中调整,而不是像(第 3 列第 1 个单元格)。第三,单词保持紧凑,没有像第一行那样的额外空间,混合有左右空间。提前谢谢您。这是代码

\begin{table*}[h!]
%\setlength\extrarowheight{0.01pt}
\caption{Heuristic techniques}\label{title}
%\renewcommand{\arraystretch}{0.9}
\setlength{\tabcolsep}{25pt}
\centering
\begin{tabular}%{|m{1.5cm}|m{1.5cm}|m{4cm}|m{3cm}|}
 \hline

 \textbf{\small Ref. no(s)}   &  \textbf{\small Objective(s)} & \textbf{\small Distinctive attributes} &  \textbf{\small Remarks} \\ %\midrule
 \hline
Hybrid fdfsadfafdsa  \cite{8}&  cost & comfortdsaladfjlafjdlkfjldafjdk
fjldsajfdlfjdlf&asdf \\
\hline
hh \cite{9} & Cost  &  area & enhanced     \\
 \hline
 hh \cite{10}& Cost & problems & Practical\\
\hline
jj \cite{11}& Cost  & simplicity & ignored\\
\hline
jjj\cite{12}& comfort& Cost  & S  \\
\hline
kk\cite{13}& Cost &   integration& time \\
\hline

\end{tabular}
\end{table*}

在此处输入图片描述

答案1

您可以使用tabularx来充分利用所有可用的 hspace。可以将包含长文本的列设置为X,将包含较短换行文本的列设置为m{..},可以将所有其他列设置为l。要将任何单元格居中,请使用\multicolumn{1}{c|}{..}。请注意\renewcommand{\tabularxcolumn}[1]{m{#1}}重新定义X为 的行m。最后删除\setlength{\tabcolsep}{25pt},这负责单元格左侧和右侧的额外填充。

\documentclass[12pt]{article}
\usepackage{booktabs,tabularx}
\begin{document}

\begin{table}[!tbh]
\centering
\caption{Heuristic techniques}\label{title}
%\setlength{\tabcolsep}{25pt}
\renewcommand{\tabularxcolumn}[1]{m{#1}}
\begin{tabularx}{\textwidth}{|m{1.5cm}|l|X|l|}
    \hline
    \textbf{\small Ref. no(s)}   & \textbf{\small Objective(s)} & \multicolumn{1}{c|}{\textbf{\small Distinctive attributes}} & \textbf{\small Remarks} \\ \hline
    Hybrid fdfsadfafdsa \cite{8} & cost    & comfortdsaladfjlafjdlkfjldafjdk fjldsajfdlfjdlf & asdf \\\hline
    hh \cite{9}                  & Cost    & area                                            & enhanced     \\ \hline
    hh \cite{10}                 & Cost    & problems                                        & Practical\\\hline
    jj \cite{11}                 & Cost    & simplicity                                      & ignored\\\hline
    jjj\cite{12}                 & comfort & Cost                                            & S  \\\hline
    kk\cite{13}                  & Cost    & integration                                     & time \\\hline
\end{tabularx}
\end{table}

\end{document}

在此处输入图片描述

答案2

\thead从包中使用makecell,就不需要\multicolumn\textbf\small

在此处输入图片描述

\documentclass[12pt]{article}
\usepackage{booktabs,tabularx}
\usepackage{makecell}
\renewcommand{\theadfont}{\bfseries\small}
\begin{document}

\begin{table}[!tbh]
\centering
\caption{Heuristic techniques}\label{title}
%\setlength{\tabcolsep}{25pt}
\renewcommand{\tabularxcolumn}[1]{m{#1}}
\begin{tabularx}{\textwidth}{|m{1.5cm}|l|X|l|}
    \hline
    \thead{Ref.\\ no(s)}   & \thead{Objective(s)} & \thead{Distinctive attributes} & \thead{Remarks} \\ \hline
    Hybrid fdfsadfafdsa \cite{8} & cost    & comfortdsaladfjlafjdlkfjldafjdk fjldsajfdlfjdlf & asdf \\\hline
    hh \cite{9}                  & Cost    & area                                            & enhanced     \\ \hline
    hh \cite{10}                 & Cost    & problems                                        & Practical\\\hline
    jj \cite{11}                 & Cost    & simplicity                                      & ignored\\\hline
    jjj\cite{12}                 & comfort & Cost                                            & S  \\\hline
    kk\cite{13}                  & Cost    & integration                                     & time \\\hline
\end{tabularx}
\end{table}

\end{document}

相关内容