
我試圖讓我的單元格處於水平和垂直居中的表格環境中。 MWE 可以在最後找到。
我嘗試了不同的方向:哪個做了它應該做的事情,以及m{4cm}
像{\centering}m{4cm}}
or 這樣的複雜變化{\centering\arraybackslash}m{4cm}}
,儘管我必須誠實地不知道 arraybackslash 參數到底應該改變什麼 - 我只是從另一個 stackexchange 帖子中復制了那個。
所有文本,包括垂直書寫的文本,都應該水平和垂直居中。表格的主要部分是帶有標題的圖片,圖片是正方形的。如果有人可以告訴我如何對齊單元格,我想這也應該適用於包括圖片在內的單元格,不是嗎?
編輯:更新了程式碼並截圖了結果。\parbox
用作佔位符來製作更高的單元格。保持1,5
居中,這是事實。然而,其他文本部分似乎有點偏離頂部,可能是因為逗號低於基線(這是正確的詞嗎?)?有什麼辦法可以將整體寫作降低一兩分嗎?有什麼辦法可以讓細胞稍微高一點嗎?在「foobar foobar」儲存格中,文字在頂部和底部劃線。此外,垂直文字偏離中心。我是否必須使用小型頁面才能使其居中?
\documentclass{scrreprt}
\usepackage{array}
\usepackage{multirow}
\usepackage{rotating}
\begin{document}
\begin{table}
\centering
\begin{tabular}{|c|c|c|c|c|c|c|}
\hline
\multicolumn{2}{|c|}{} & \multicolumn{5}{c|}{foo}\\
\cline{3-7}
\multicolumn{2}{|c|}{} & 1,0 & 2,0 & 3,0 & 4,0 & 5,0\\
\hline
\multirow{5}{*}{\rotatebox{90}{bar}} & 1,0 & & & & & \\
\cline{2-7}
& 1,5 & \parbox{2cm}{foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar } & & & & \\
\cline{2-7}
& 2,0 & & & & & \\
\cline{2-7}
& 2,5 & & & & & \\
\cline{2-7}
& 3,0 & & & & & \\
\hline
\end{tabular}
\end{table}
\end{document}
答案1
您可以建立一個新的列類型,例如
\newcolumntype{C}[1]{>{\centering\arraybackslash}m{#1}}
然後用作C{<your width>}
列定義,<your width>
根據需要進行設定。這樣您的列就會垂直和水平居中。
我還建議刪除垂直線,避免旋轉單元格並使用booktabs
.
\documentclass{scrreprt}
\usepackage{array}
\usepackage{multirow}
\usepackage{rotating}
\usepackage{booktabs}
\newcolumntype{C}[1]{>{\centering\arraybackslash}m{#1}}
\begin{document}
\chapter{A chapter}
Table \ref{tab:yours} is your table, but I suggest to you to redesign your
table like \ref{tab:mine}.
\begin{table}
\centering\setlength\extrarowheight{2pt}
\caption{Your table\label{tab:yours}}
\begin{tabular}{|*2{C{.5cm}|}*5{C{2cm}|}}
\hline
\multicolumn{2}{|c|}{} & \multicolumn{5}{c|}{foo}\\
\cline{3-7}
\multicolumn{2}{|c|}{} & 1,0 & 2,0 & 3,0 & 4,0 & 5,0\\
\hline
& 1,0 & & & & & \\
\cline{2-7}
\multirow{4}{*}{\rotatebox[origin=c]{90}{bar}} & 1,5 & foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar & & & & \\
\cline{2-7}
& 2,0 & & & & & \\
\cline{2-7}
& 2,5 & & & & & \\
\cline{2-7}
& 3,0 & & & & & \\
\hline
\end{tabular}
\end{table}
\begin{table}
\centering\setlength\extrarowheight{2pt}
\caption{My suggestion\label{tab:mine}}
\begin{tabular}{C{.7cm}*5{C{2cm}}}
\toprule
& \multicolumn{5}{c}{foo}\\
\cmidrule(l){2-6}
bar & 1,0 & 2,0 & 3,0 & 4,0 & 5,0\\
\midrule
1,0 & & & & & \\
\midrule
1,5 & foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar & & & & \\
\midrule
2,0 & & & & & \\
\midrule
2,5 & & & & & \\
\midrule
3,0 & & & & & \\
\toprule
\end{tabular}
\end{table}
\end{document}