如何使表格中每个单元格内的文本垂直居中?

如何使表格中每个单元格内的文本垂直居中?

我试图将文本垂直居中放置在职位和人员标题内,但实际上我做不到。我希望它们如图所示(垂直居中)。我的代码:

\documentclass{article}
\usepackage{multirow}
\usepackage{graphicx}
\begin{document}
% Please add the following required packages to your document preamble:
% \usepackage{multirow}
\begin{table}[h]
    \centering
    \begin{tabular}{|c|c|p{3in}|}
        \hline
        Position          & \multicolumn{1}{c|}{Person(s)} & \multicolumn{1}{c|}{Role} \\ \hline
        Co-head &
        \begin{tabular}[c]{@{}c@{}}Islam Mahdy\\ Youssef Amr\end{tabular} &
        \begin{itemize}
            \item Reviewing the content of each session previously.  
            \item Reviewing the progression of the academic sessions whether they for the Curriculum sessions or the standardized tests' sessions.
            \item Having continuous check on the magazine to ensure the conformity of the topics discussed to the club's goal  
        \end{itemize}    \\ \hline
        Vice head &
        Ahmed Magdy &
    loremipsum \\ \hline
        Head of Academics & Mohamed Badr                   &                           \begin{itemize}
            \item Preparing the content of each session.  
            \item Managing the time table of the weekly-held sessions.
            \item Conducting mini quizzes each week to make sure the students have comprehended the previous session.   
        \end{itemize}           \\ \hline
        \multirow{2}{*}{Magazine} &
        \multirow{2}{*}{\begin{tabular}[c]{@{}l@{}}Ali Ashraf \\ Ahmed Hamed\end{tabular}} &
        \multirow{2}{*}{sdsdsdsds} \\
        &                                &                                    \\ \hline
    \end{tabular}
\end{table}


\end{document}

上面代码中的表格

答案1

除了将表格内容垂直居中之外,我还在下面的 MWE 中实现了以下更改:

  • 用于tabularx确保表格适合文本宽度。在原始示例中,表格比文本宽度宽很多。您会收到一个 voerfull 框警告,告知您这一点。
  • 为了使内容垂直居中,我添加了\renewcommand{\tabularxcolumn}[1]{m{#1}}。最初,X类型列是从p类型列派生出来的,因此会像原始示例一样对齐 ist 内容。上面的行现在重新定义了X类型列,使其充当垂直居中的m类型列。(这里不需要明确添加array包,因为tabularx已经加载了它。)
  • 我添加了该makecell包来替换列标题中的嵌套表格和多列。
  • 我已删除\multirow表格最后一行中的命令以及那里的空行。
  • 我添加了enumitem包并定义了一个新的自定义tabenum环境以供表格使用。此环境为您节省了上方、下方和左侧的大量空间。

在此处输入图片描述

\documentclass{article}
%\usepackage{multirow} % no longer needed
\usepackage{graphicx}

\usepackage{makecell}
\renewcommand{\theadfont}{\normalsize}

\usepackage{tabularx}
\renewcommand{\tabularxcolumn}[1]{m{#1}}

\usepackage{enumitem}
\newlist{tabitem}{enumerate}{1}

\setlist[tabitem,1]{label*=\textbullet,
                    leftmargin=*,
                    nosep,
                    leftmargin=*,
                    before=\begin{minipage}[t]{\hsize},
                    after=\end{minipage}}

\begin{document}
\begin{table}[h]
  \centering
  \begin{tabularx}{\textwidth}{|c|c|X|}
    \hline
    \thead{Position} 
      & \thead{Person(s)} 
        & \thead{Role} \\
    \hline
    Co-head 
      & \makecell{Islam Mahdy\\ Youssef Amr} 
        & \begin{tabitem}
            \item Reviewing the content of each session previously.  
            \item Reviewing the progression of the academic sessions whether they for the Curriculum sessions or the standardized tests' sessions.
            \item Having continuous check on the magazine to ensure the conformity of the topics discussed to the club's goal  
          \end{tabitem}    \\ 
    \hline
    Vice head 
      & Ahmed Magdy 
        & loremipsum \\ 
    \hline
    Head of Academics 
      & Mohamed Badr 
        &  \begin{tabitem}
             \item Preparing the content of each session.  
             \item Managing the time table of the weekly-held sessions.
             \item Conducting mini quizzes each week to make sure the students have comprehended the previous session.   
           \end{tabitem}           \\ 
    \hline
    Magazine 
      &\makecell{Ali Ashraf \\ Ahmed Hamed} 
        & sdsdsdsds \\
    \hline
  \end{tabularx}
\end{table}


\end{document}

答案2

这里对垂直间距进行了一些简化和改进,特别是对于列表、加载enumitemmakecell包。

\documentclass{article}
\usepackage{array, multirow, makecell} 
\usepackage{enumitem}

\begin{document}

\begin{table}[h]
    \centering\setlength{\extrarowheight}{2pt}
    \setlist[itemize]{wide = 0pt, leftmargin = *, topsep =3pt, after =\vspace*{\dimexpr\topsep-\baselineskip}}
    \begin{tabular}{|c|c| m{3in}|}
        \hline
        Position & \multicolumn{1}{c|}{Person(s)} & \multicolumn{1}{c|}{Role} \\ \hline
        Co-head &
        \begin{tabular}[c]{@{}c@{}}Islam Mahdy\\ Youssef Amr\end{tabular} &
        \begin{itemize}
            \item Reviewing the content of each session previously.
            \item Reviewing the progression of the academic sessions whether they for the Curriculum sessions or the standardized tests' sessions.
            \item Having continuous check on the magazine to ensure the conformity of the topics discussed to the club's goal
        \end{itemize} \\ \hline
        Vice head &
        Ahmed Magdy &
    loremipsum \\ \hline
        Head of Academics & Mohamed Badr & \begin{itemize}
            \item Preparing the content of each session.
            \item Managing the time table of the weekly-held sessions.
            \item Conducting mini quizzes each week to make sure the students have comprehended the previous session.
        \end{itemize} \\ \hline
        \multirow{2}{*}{Magazine} &
        \multirowcell{2}{Ali Ashraf \\ Ahmed Hamed} &
        \multirow{2}{*}{sdsdsdsds} \\
        & & \\ \hline
    \end{tabular}
\end{table}

\end{document} 

在此处输入图片描述

相关内容