使表格内容垂直和水平居中

使表格内容垂直和水平居中

我正在尝试复制下面这个表格:

我尝试使用 Latex 实现的目标

使用以下代码:

    \documentclass{article}
    \usepackage{array,booktabs}% http://ctan.org/pkg/{array,booktabs}
    \usepackage{lineno,hyperref,amsmath}
    \usepackage[table]{xcolor} %colour tables
    \usepackage{color, colortbl} % choose and define a colour
    \usepackage{tabu} %for the font size of the tables
    \usepackage{array}% tables
    \usepackage{booktabs} %tables
    \usepackage[tmargin=1in,bmargin=1in,lmargin=2.7cm,rmargin=2.7cm]{geometry}
    \modulolinenumbers[5]

    \begin{document}

    \begin{table}[ht]
    \definecolor{LightBlue}{rgb}{0.78,0.85,0.95}
    \centering
    \newcolumntype{A}{ >{\centering\arraybackslash}m{0.8cm} }
    \newcolumntype{B}{ >{\centering\arraybackslash}m{0.5cm} }
    \begin{tabu}{|p{4.5cm} B A A A A A|}
    \hline
    \rowcolor{LightBlue}    \textbf{Variable} & \textbf{Obs.} & \textbf{Mean} & \textbf{Median} & \textbf{SD} & \textbf{Min} & \textbf{Max} \\
    \hline
\rowfont{\small} \fontdimen2\font=0.6ex{Variable name with characters on the bottom line annoyingly} & 00 & 0.0000 & 0.0000 & 0.0000 & 0.0000 & 0.00\\
\hline
\rowfont{\small} \fontdimen2\font=0.6ex{In Latex you don't worry about floating numbers} & 00 & 0.0000 & 0.0000 & 0.0000 & 0.0000 & 0.00\\
    \end{tabu}
    \end{table}
    \end{document}

结果是这样的: 乳胶桌

很难说这个表格有多少问题,但特别是:

  1. 变量名触及底线
  2. 尽管我尽了最大努力,但单元格中的数字仍然在上升,而不在中间>{\centering\arraybackslash}

有人知道如何解决这个问题吗?

答案1

虽然我没有消除您的列类型,但我没有使用它们来实现目标。相反,我使用\parbox[c]垂直居中单元格左侧文本,这将解决其他列居中的问题。我还在\strut开头和结尾添加了一个\parbox以提供垂直缓冲区。

\documentclass{article}
\usepackage{array,booktabs}% http://ctan.org/pkg/{array,booktabs}
\usepackage{lineno,hyperref,amsmath}
\usepackage[table]{xcolor} %colour tables
\usepackage{color, colortbl} % choose and define a colour
\usepackage{tabu} %for the font size of the tables
\usepackage{array}% tables
\usepackage{booktabs} %tables
\usepackage[tmargin=1in,bmargin=1in,lmargin=2.7cm,rmargin=2.7cm]{geometry}
\modulolinenumbers[5]
\usepackage{stackengine}
\begin{document}

\begin{table}[ht]
\definecolor{LightBlue}{rgb}{0.78,0.85,0.95}
\centering
\newcolumntype{A}{ >{\centering\arraybackslash}m{0.8cm} }
\newcolumntype{B}{ >{\centering\arraybackslash}m{0.5cm} }
\begin{tabu}{|p{4.5cm} B A A A A A|}
\hline
\rowcolor{LightBlue}    \textbf{Variable} & \textbf{Obs.} & \textbf{Mean} & \textbf{Median} & \textbf{SD} & \textbf{Min} & \textbf{Max} \\
\hline
\parbox[c]{4.32cm}{\strut\small \fontdimen2\font=0.6ex{Variable name with characters on the bottom line annoyingly\strut}} & 00 & 0.0000 & 0.0000 & 0.0000 & 0.0000 & 0.00\\
\hline
\parbox[c]{4.32cm}{\strut\small \fontdimen2\font=0.6ex{\strut In Latex you don't worry about floating numbers\strut}} & 00 & 0.0000 & 0.0000 & 0.0000 & 0.0000 & 0.00\\
\hline
\end{tabu}
\end{table}
\end{document}

在此处输入图片描述

答案2

您尝试复制的表是由booktabs或类似程序制作的。由于您已经加载此包两次……您实际上可以在这里使用它:

% arara: pdflatex

\documentclass{article}
\usepackage[tmargin=1in,bmargin=1in,lmargin=2.7cm,rmargin=2.7cm]{geometry}
\usepackage{array,booktabs,threeparttable}
\usepackage[table]{xcolor}
\definecolor{LightBlue}{rgb}{0.78,0.85,0.95}
\usepackage{siunitx}
\usepackage{ragged2e}
\usepackage{caption}

\newcommand{\topline}{\arrayrulecolor{black}\specialrule{0.08em}{\abovetopsep}{0pt}%
    \arrayrulecolor{LightBlue}\specialrule{\belowrulesep}{0pt}{-1pt}%
    \arrayrulecolor{black}
    }
\newcommand{\midline}{\arrayrulecolor{LightBlue}\specialrule{\aboverulesep}{-1pt}{0pt}%
    \arrayrulecolor{black}\specialrule{\lightrulewidth}{0pt}{0pt}%
    \arrayrulecolor{white}\specialrule{\belowrulesep}{0pt}{-1pt}%
    \arrayrulecolor{black}
    }

\begin{document}        
\begin{table}[ht]
    \centering
    \begin{threeparttable}
        \caption{Some caption}
    \begin{tabular}{%
        >{\RaggedRight}m{7.35cm} 
        S[table-format=1.0]
        S[table-format=1.4]
        S[table-format=1.4]
        S[table-format=1.4]
        S[table-format=1.4]
        S[table-format=1.2]
        }
        \topline
        \rowcolor{LightBlue} \textbf{Variable} & \textbf{Obs.} & \textbf{Mean} & \textbf{Median} & \textbf{SD} & \textbf{Min} & \textbf{Max} \\
        \midline
        Variable name with characters on the bottom line annoyingly & 00 & 0.0000 & 0.0000 & 0.0000 & 0.0000 & 0.00\\
        \midrule
        In \LaTeX{} you don't worry about floating numbers & 00 & 0.0000 & 0.0000 & 0.0000 & 0.0000 & 0.00\\\bottomrule
    \end{tabular}
    \begin{tablenotes}
        \item Legend: Obs.: Observation, SD: Standard Deviation.
    \end{tablenotes}
    \end{threeparttable}
\end{table}
\end{document}

在此处输入图片描述

相关内容