【问题标题】:Centering element in LaTeX table with fixed column width具有固定列宽的 LaTeX 表格中的居中元素
【发布时间】:2021-12-02 18:09:12
【问题描述】:

我在使用 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}

这段代码给了我以下输出: generated table.

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

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

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

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

谁能帮帮我?如果我设法将最后一行也居中,表格会更漂亮。

【问题讨论】:

  • 下次请发minimal reproducible example 这样我们就不必猜测您可能会加载哪些包
  • 另外,请将未删节(包括换行符)的乳胶错误消息作为代码块发布。换行符 很重要,因为它们表示错误所在。

标签: latex


【解决方案1】:

你可以用>{\centering\arraybackslash}居中最后一列

\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}

【讨论】:

    猜你喜欢
    • 2010-11-24
    • 2021-11-12
    • 1970-01-01
    • 1970-01-01
    • 2020-11-16
    • 1970-01-01
    • 1970-01-01
    • 2012-09-26
    • 2014-04-17
    相关资源
    最近更新 更多