表格垂直对齐

表格垂直对齐

我需要制作一个命令,该命令可以像左边的表格一样使用,但可以像右边的表格一样创建垂直对齐。

我的意思是表格中的左列是顶部对齐的。如果右列低于左列,则右列垂直居中到左侧内容的中间。如果右列等于或高于左列,则右列是顶部对齐的。

\documentclass[12pt,a4paper]{article}
\usepackage{geometry,array,graphicx,multicol}
\begin{document}

\newcommand{\myrow}[1]{
    \hline 
    \scalebox{3}{A} & #1 \\
    \hline
    }

\begin{multicols}{2}

\begin{tabular}{|m{2em}|m{7em}|}
    \myrow{a a a}
    \myrow{a a a a a a a a a a a a}
    \myrow{a a a a a a a a a a a a a a a a a a a a a}
    \myrow{a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a}
    \myrow{a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a}
    \myrow{a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a}
\end{tabular}

\begin{tabular}{|m{2em}|m{7em}|}
    \hline
    \scalebox{3}{A} & a a a \\
    \hline
    \scalebox{3}{A} & a a a a a a a a a a a a \\
    \hline
    \scalebox{3}{A} & a a a a a a a a a a a a a a a a a a a a a \\
    \hline
    \scalebox{3}{A} & a a a a a a a a a a a a a a a a a a a a a a a a a a a \\
                    & a a a \\
    \hline
    \scalebox{3}{A} & a a a a a a a a a a a a a a a a a a a a a a a a a a a \\
                    & a a a a a a a a a \\
                    & a a a \\
    \hline
    \scalebox{3}{A} & a a a a a a a a a a a a a a a a a a a a a a a a a a a \\
                    & a a a a a a a a a \\
                    & a a a a a a a a a \\
                    & a a a \\
    \hline
\end{tabular}

\end{multicols}

\end{document}

在此处输入图片描述

答案1

将大字母适当降低一点(在下方留出一些空间,避免碰到规则)。第二列说明符应该是p

\documentclass[12pt,a4paper]{article}
\usepackage{graphicx}

\newcommand{\bigletter}[1]{%
  \raisebox{\dimexpr1ex-\height}{%
    \scalebox{3}{#1\rule[-1pt]{0pt}{1pt}}}%
}

\newcommand{\myrow}[1]{%
  \bigletter{A} & #1 \\
  \hline
}

\begin{document}

\begin{tabular}{|c|p{7em}|}
  \hline
  \myrow{a a a}
  \myrow{a a a a a a a a a a a a}
  \myrow{a a a a a a a a a a a a a a a a a a a a a}
  \myrow{a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a}
  \myrow{a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a}
  \myrow{a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a}
\end{tabular}

\end{document}

在此处输入图片描述

如果大字母垂直方向大于文本,则采用不同的居中解决方案。注意:我假设大字母始终为大写。

\documentclass[12pt,a4paper]{article}
\usepackage{graphicx}

\makeatletter
\newcommand{\myrow}[2]{%
  \sbox{\z@}{\scalebox{3}{#1}}%
  \sbox{\z@}{\vrule height \dimexpr\ht\z@+4pt depth 2pt width 0pt \box\z@}%
  \sbox{\tw@}{\parbox{7em}{#2}}%
  \ifdim\ht\z@>\dimexpr\ht\tw@+\dp\tw@\relax
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi
  {\begin{tabular}{@{}c@{}}\box\z@\end{tabular} & \parbox{7em}{#2}}%
  {\raisebox{\dimexpr1ex+4pt-\height}{\box\z@} & #2}%
  \\ \hline
}
\makeatother

\begin{document}

\begin{tabular}{|c|p{7em}|}
  \hline
  \myrow{A}{a a a}
  \myrow{A}{a a a a a a a a a a a a}
  \myrow{A}{a a a a a a a a a a a a a a a a a a a a a}
  \myrow{A}{a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a}
  \myrow{A}{a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a}
  \myrow{A}{a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a}
\end{tabular}

\end{document}

相关内容