如何垂直居中对齐表格中的文本,并在其他列中逐项列出

如何垂直居中对齐表格中的文本,并在其他列中逐项列出

我想将表格单元格中的文本垂直居中对齐,而其他列包含itemize列表。偶然发现这个问题,我尝试实现接受的答案在那里。但是当我添加第二行时,它不起作用。以下是 MWE:

\documentclass{article}
\usepackage{array}
\usepackage{enumitem}
\newcolumntype{M}[1]{%
    >{\begin{minipage}{#1}}
        m{#1}
        <{\end{minipage}}
}
\begin{document}
    \begin{table}[h!]
        \setlist[itemize]{leftmargin=12pt}
        \centering
        \begin{tabular}{|m{0.05\linewidth}|M{0.25\linewidth}|M{0.25\linewidth}|M{0.25\linewidth}|}
            \hline
            &   \multicolumn{1}{c|}{\textbf{Arts}} & \multicolumn{1}{c|}{\textbf{Science}} & \multicolumn{1}{c|}{\textbf{Commerce}}\\
            \hline
                UG & 
                \begin{itemize}[topsep=0pt, itemsep=0.5ex]
                    \item English
                    \item Education
                    \item Geography
                    \item History
                    \item Mass Communication \& Journalism
                    \item Political Science
                    \item Psychology
                    \item Sociology
                    \item Bachelor of Social Work
                    \item Physical Education (as general subject)
                \end{itemize} & 
                \begin{itemize}[topsep=0pt, itemsep=0.5ex]
                    \item Computer Science
                    \item Economics
                    \item Mathematics
                    \item Physics
                    \item Statistics (as general subject)
                \end{itemize} &
                \begin{itemize}[topsep=0pt, itemsep=0.5ex]
                    \item B. Com. Honours
                    \item B. Com. General
                \end{itemize}\\
                \hline
                PG &
                \begin{itemize}[topsep=0pt, itemsep=0.5ex]
                    \item Education
                    \item English
                    \item Psychology
                \end{itemize}
            & &\\
            \hline
        \end{tabular}
    \end{table}
\end{document}

我只想在第一列垂直居中对齐。其余列应顶部对齐。请帮我实现这一点。

答案1

使用以下multirow细胞:

\documentclass{article}
\usepackage{geometry}                       % <---
\usepackage{ragged2e}                       % <---
\usepackage{makecell, multirow, tabularx}   % <---
\renewcommand\theadfont{\small\bfseries}
\renewcommand\theadgape{}
\newcolumntype{L}{>{\RaggedRight}X}         % <---
\usepackage{enumitem}
\usepackage{etoolbox}                       % <---
\AtBeginEnvironment{table}{%                % <---
\setlist[itemize]{nosep,
                 leftmargin=*,
                 label=\textbullet,
                 before=\begin{minipage}[t]{\linewidth}, 
                 after=\end{minipage}}                   
                        }

\begin{document}
    \begin{table}[ht]
    \centering
    \setlength\tabcolsep{4pt}   % <---
    \setcellgapes{3pt}          % <---
    \makegapedcells             % <---
\begin{tabularx}{\linewidth}{|c|*{3}{L|}}    % <---
    \hline
    & \thead{Arts}  & \thead{Science}   & \thead{Commerce}  \\  % <---
    \hline
\multirow{12}{*}{UG}
    &   \begin{itemize}
    \item English
    \item Education
    \item Geography
    \item History
    \item Mass Communication \& Journalism
    \item Political Science
    \item Psychology
    \item Sociology
    \item Bachelor of Social Work
    \item Physical Education (as general subject)
        \end{itemize}   &   \begin{itemize}
                        \item Computer Science
                        \item Economics
                        \item Mathematics
                        \item Physics
                        \item Statistics (as general subject)
                            \end{itemize}   &   \begin{itemize}
                                            \item B. Com. Honours
                                            \item B. Com. General
                                                \end{itemize}   \\
    \hline
\multirow{3}{*}{PG}
    &   \begin{itemize}
    \item Education
    \item English
    \item Psychology
        \end{itemize}   &                   &                   \\
    \hline
\end{tabularx}
    \end{table}
\end{document}

在此处输入图片描述

正如你所注意到的,上面的 MWE 引入了包:

  • geometry为了使\textwidth更大
  • ragged2e用于单元格内容左对齐
  • enumitem用于定义表中使用的列表
  • etoolbox用于定义itemize表中的属性

答案2

我建议用和来做到这tabularx 一点multirow

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

\makeatletter
\newcommand\compress{\@minipagetrue}
\makeatother

\begin{document}

\begin{table}[h!]
    \setlist[itemize]{topsep=0pt, itemsep=0.5ex, wide=0pt, leftmargin=*,after=\vspace{-\baselineskip}}
    \centering
    \begin{tabularx}{\linewidth}{|m{0.05\linewidth}|*{3}{>{\compress\arraybackslash}X|}}
        \hline
        & \multicolumn{1}{c|}{\textbf{Arts}} & \multicolumn{1}{c|}{\textbf{Science}} & \multicolumn{1}{c|}{\textbf{Commerce}}\\
        \hline
            \multirow{20}{=}{UG} &
            \begin{itemize}
                \item English
                \item Education
                \item Geography
                \item History
                \item Mass Communication \& Journalism
                \item Political Science
                \item Psychology
                \item Sociology
                \item Bachelor of Social Work
                \item Physical Education (as general subject)
            \end{itemize} &
            \begin{itemize}
                \item Computer Science
                \item Economics
                \item Mathematics
                \item Physics
                \item Statistics (as general subject)
            \end{itemize} &
            \begin{itemize}[topsep=0pt, itemsep=0.5ex]
                \item B. Com. Honours
                \item B. Com. General
            \end{itemize}\tabularnewline
            \hline
            \multirow{4.2}{=}{PG} &
            \begin{itemize}[topsep=0pt, itemsep=0.5ex]
                \item Education
                \item English
                \item Psychology
            \end{itemize}
        & &\\
        \hline
    \end{tabularx}
\end{table}

\end{document} 

在此处输入图片描述

相关内容