繁体   English   中英

具有固定列宽的 LaTeX 表中的居中元素

[英]Centering element in LaTeX table with fixed column width

我在使用 LaTeX 表时遇到了一些问题。 特别是,我有以下代码:

\begin{table}[ht]
    \centering
    \setlength{\tabcolsep}{2pt}
    \renewcommand{\arraystretch}{1.5}
    \begin{tabular}{|p{0.15\textwidth}>{\centering}p{0.15\textwidth}>{\centering}p{0.15\textwidth}>{\centering}p{0.15\textwidth}>{\centering}p{0.15\textwidth}p{0.15\textwidth}|}
        \hline
         & \textbf{Virtual Reality Experience} & \textbf{First Person Experience} & \textbf{Multimedia Presentation Experience} & \multicolumn{2}{c|}{\textbf{Full sample}} \\ 
         \hline
         & \textit{n} & \textit{n} & \textit{n} & \textit{n} & \textit{\%} \\
         \hline
         \textbf{Gender} &&&&& \\
         Female & 12 & 10 & 18 & 40 & 52.6 \\
         Male & 10 & 13 & 13 & 36 & 47.4 \\
         \hline
         \textbf{Educational level} &&&&& \\
         High School & 8 & 5 & 6 & 19 & 25.0 \\
         Bachelor's Degree & 13 & 9 & 19 & 41 & 53.9 \\
         Master's Degree & 1 & 8 & 6 & 15 & 19.7 \\
         PhD & 0 & 1 & 0 & 1 & 1.3 \\
         \hline \hline
         \textbf{Age} &&&&& \\
         Mean & 23.6 & 24.6 & 24 & \multicolumn{2}{c|}{24.1} \\
         Median & 23.6 & 24.6 & 24 & \multicolumn{2}{c|}{24.1} \\
         \hline
    \end{tabular}
    \caption{Participants demographic information}
    \label{tab:demographic}
\end{table}

这段代码给了我以下 output: generated table

我还想将最后一列居中(与其他列一样,使用>{\centering} )。 但是,当我尝试这样做时,我有很多错误:

  • Misplaced \noalign - 您在错误的地方使用了\hline命令,可能在表格之外。 如果\hline命令写在表中,请尝试在它之前包含\\ (在第 3 个\hline

  • 额外的 alignment 选项卡已更改为\cr - 您在表格中写入了太多 alignment 选项卡,导致其中一个变为换行符。 确保您在表格中指定了正确的列数(在\textbf{Gender}行上)

  • LaTeX 错误:这里没有要结束的行 - 您使用了\\\newline命令,其中 LaTeX 没有预期。 确保只在文本块之后使用换行符,并在列表和其他环境中小心使用换行符(在\textbf{Gender}行上)

有人可以帮我吗? 如果我设法将最后一行也居中,这张桌子会漂亮得多。

\documentclass{article}

\usepackage{array}

\begin{document}

\begin{table}[ht]
    \centering
    \setlength{\tabcolsep}{2pt}
    \renewcommand{\arraystretch}{1.5}
    \begin{tabular}{
      |
      p{0.15\textwidth}
      >{\centering}p{0.15\textwidth}
      >{\centering}p{0.15\textwidth}
      >{\centering}p{0.15\textwidth}
      >{\centering}p{0.15\textwidth}
      >{\centering\arraybackslash}p{0.15\textwidth}
      |
    }
        \hline
         & \textbf{Virtual Reality Experience} & \textbf{First Person Experience} & \textbf{Multimedia Presentation Experience} & \multicolumn{2}{c|}{\textbf{Full sample}} \\ 
         \hline
         & \textit{n} & \textit{n} & \textit{n} & \textit{n} & \textit{\%} \\
         \hline
         \textbf{Gender} &&&&& \\
         Female & 12 & 10 & 18 & 40 & 52.6 \\
         Male & 10 & 13 & 13 & 36 & 47.4 \\
         \hline
         \textbf{Educational level} &&&&& \\
         High School & 8 & 5 & 6 & 19 & 25.0 \\
         Bachelor's Degree & 13 & 9 & 19 & 41 & 53.9 \\
         Master's Degree & 1 & 8 & 6 & 15 & 19.7 \\
         PhD & 0 & 1 & 0 & 1 & 1.3 \\
         \hline \hline
         \textbf{Age} &&&&& \\
         Mean & 23.6 & 24.6 & 24 & \multicolumn{2}{c|}{24.1} \\
         Median & 23.6 & 24.6 & 24 & \multicolumn{2}{c|}{24.1} \\
         \hline
    \end{tabular}
    \caption{Participants demographic information}
    \label{tab:demographic}
\end{table}


\end{document}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM