居中垂直文本

居中垂直文本

我有一张表格,我想让文本垂直居中。但不知何故表格中的文本没有居中。你能帮我将垂直文本居中吗?


% page setup 
\documentclass[a4paper, 11pt]{article}
\usepackage[left=2cm,top=2cm,right=2cm, bottom=2cm]{geometry}

\usepackage[utf8]{inputenc} 
\usepackage[english]{babel} 

\usepackage{amsmath} 
\usepackage{amssymb} 
\usepackage{amsthm}  

\usepackage{ragged2e} 
\usepackage{booktabs, tabularx} 
\usepackage{array,multirow,graphicx} 
\newcolumntype{C}{>{\small\raggedright\arraybackslash}X}
\newcolumntype{P}[1]{>{\RaggedRight\hspace{0pt}}m{#1}}
\newlength\colwidth


\begin{document}

\begin{table}

\small
\settowidth\colwidth{SEThh} % set width of 1st col.
\begin{tabularx}{\textwidth}{@{}P{\colwidth}* {1}P{\colwidth}  * {5}{C} @{}}
    \toprule
& \textbf{Set}  & \textbf{category A} & \textbf{category B} &\textbf{category C} & \textbf{category D} \\
\midrule
\multirow{8}{*}{\rotatebox[origin=c]{90}{\textbf{Center}}} & 1.1 & Text & text &text&   - \\
\addlinespace
 & 1.1 & text & text &text&   - \\
\addlinespace
 & 1.1 & text & text &text&   - \\
\addlinespace
 & 1.1 & text & text &text&   - \\
\addlinespace
 & 1.1 & text & text &text&   - \\
\addlinespace
 & 1.1 & text & text &text&   - \\
\addlinespace
 & 1.1 & text & text &text&   - \\
 \addlinespace
 & 1.1 & text & text &text&   - \\
    \bottomrule
\end{tabularx}
\endtabularx
\end{table}


\end{document}


答案1

\addlinespace命令添加了 0.5 em 的空格 ( \defaultaddspace=.5em)。您有 7 个\addlinespaces,它们为 8 行添加了 3.5 em 的额外垂直空间\multirow。因此,您需要将文本向下移动约 1.75 em(我的计算是近似的,抱歉):

\multirow{8}{*}[-1.75em]{\rotatebox[origin=c]{90}{\textbf{Center}}} 

平均能量损失

\documentclass{article}
\usepackage[margin=2cm]{geometry}
\usepackage{ragged2e} 
\usepackage{booktabs, tabularx} 
\usepackage{array,multirow,graphicx} 
\newcolumntype{C}{>{\small\raggedright\arraybackslash}X}
\newcolumntype{P}[1]{>{\RaggedRight\hspace{0pt}}m{#1}}
\newlength\colwidth

\begin{document}
 
 \begin{table}
  \small
  \settowidth\colwidth{SEThh} % set width of 1st col.
  \begin{tabularx}{\textwidth}{@{}P{\colwidth}*{1}P{\colwidth}*{5}{C}@{}}
  \toprule
   & \textbf{Set}  & \textbf{category A} & \textbf{category B} &\textbf{category C} & \textbf{category D} \\
   \midrule
   \multirow{8}{*}[-1.75em]{\rotatebox[origin=c]{90}{\textbf{Center}}} 
   & 1.1 & Text & text &text&   - \\
   \addlinespace
   & 1.1 & text & text &text&   - \\
   \addlinespace
   & 1.1 & text & text &text&   - \\
   \addlinespace
   & 1.1 & text & text &text&   - \\
   \addlinespace
   & 1.1 & text & text &text&   - \\
   \addlinespace
   & 1.1 & text & text &text&   - \\
   \addlinespace
   & 1.1 & text & text &text&   - \\
   \addlinespace
   & 1.1 & text & text &text&   - \\
   \bottomrule
  \end{tabularx}
  \endtabularx
 \end{table}
 
\end{document}

在此处输入图片描述

答案2

同样的道理,我们必须考虑规则的\aboverulesepbelowrulesep。通过反复试验,降低文本的正确值似乎非常接近 1.9em:

\documentclass[a4paper, 11pt]{article}
\usepackage[left=2cm,top=2cm,right=2cm, bottom=2cm]{geometry}

\usepackage[utf8]{inputenc}
\usepackage[english]{babel}

\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm}

\usepackage{ragged2e}
\usepackage{booktabs, tabularx}
\usepackage{array,multirow,graphicx}
\newcolumntype{C}{>{\small\raggedright\arraybackslash}X}
\newcolumntype{P}[1]{>{\RaggedRight\hspace{0pt}}m{#1}}
\newlength\colwidth


\begin{document}

\begin{table}

\small
\settowidth\colwidth{SEThh} % set width of 1st col.
\begin{tabularx}{\textwidth}{@{}P{\colwidth}* {1}P{\colwidth} * {5}{C} @{}}
    \toprule
& \textbf{Set} & \textbf{category A} & \textbf{category B} &\textbf{category C} & \textbf{category D} \\
\midrule
\multirow{8}{*}[-1.9em]{\rotatebox[origin=c]{90}{\textbf{Center}}} & 1.1 & Text & text &text& - \\
\addlinespace
 & 1.1 & text & text &text& - \\
\addlinespace
 & 1.1 & text & text &text& - \\
\addlinespace
 & 1.1 & text & text &text& - \\
\addlinespace
 & 1.1 & text & text &text& - \\
\addlinespace
 & 1.1 & text & text &text& - \\
\addlinespace
 & 1.1 & text & text &text& - \\
 \addlinespace
 & 1.1 & text & text &text& - \\
    \bottomrule
\end{tabularx}
\endtabularx
\end{table}

\end{document}

在此处输入图片描述

答案3

表格中的文本已经垂直居中,因此不太清楚问题出在哪里。第一列中旋转文本的垂直居中?为此,我将使用包\rothead中的以下makecell代码:

\documentclass[a4paper, 11pt]{article}
\usepackage[margin=2cm]{geometry}
\usepackage[english]{babel}

\usepackage{amsmath, amssymb, amsthm}  % not used in this MWE
\usepackage{rotating}

\usepackage{ragged2e}
\usepackage{booktabs, makecell, multirow, tabularx}
\renewcommand\theadfont{\normalsize\bfseries}
\renewcommand\theadgape{}
\newcolumntype{C}{>{\small\raggedright\arraybackslash}X}


\begin{document}

\begin{table}
    \settowidth\rotheadsize{ Center } 
    \setcellgapes{2pt}
    \makegapedcells
\begin{tabularx}{\linewidth}{@{} c c  * {5}{C} @{}}
    \toprule
    & \textbf{Set}  
        & \textbf{category A} 
            & \textbf{category B} 
                &\textbf{category C} 
                    & \textbf{category D}       \\
    \midrule
\multirow{9.5}{*}{\rothead{\textbf{Center}}} 
    & 1.1 & Text & text &text&   - \\
    & 1.1 & text & text &text&   - \\
    & 1.1 & text & text &text&   - \\
    & 1.1 & text & text &text&   - \\
    & 1.1 & text & text &text&   - \\
    & 1.1 & text & text &text&   - \\
    & 1.1 & text & text &text&   - \\
    & 1.1 & text & text &text&   - \\
    \bottomrule
\end{tabularx}
    \end{table}
\end{document}

在此处输入图片描述

相关内容