[英]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.