如何使单元格文本垂直居中?

如何使单元格文本垂直居中?

我有一个简单的表格如下:

\begin{table*}
    \centering
    \begin{tabular}{|l|c|c|c|c|p{2in}|}
    ...
    ...
    \end{tabular}
    \caption{The factors the camera solver depends on to evaluate the rules.}
    \label{table:factors}
\end{table*}

如何使单元格文本垂直居中?

答案1

编辑:存在这个新答案让我的答案变得过时且令人尴尬。请继续查看新答案并投票赞成。

在此处输入图片描述

\documentclass{article}
\usepackage[a4paper,vmargin=2cm,hmargin=1cm,showframe]{geometry}
\usepackage[demo]{graphicx}
\usepackage[table]{xcolor}
\usepackage{array}
\usepackage{longtable}

\parindent=0pt

\def\correction#1{%
    \abovedisplayshortskip=#1\baselineskip\relax\belowdisplayshortskip=#1\baselineskip\relax%
    \abovedisplayskip=#1\baselineskip\relax\belowdisplayskip=#1\baselineskip\relax}

\arrayrulewidth=1pt\relax
\tabcolsep=5pt\relax
\arrayrulecolor{red}
\fboxsep=\tabcolsep\relax
\fboxrule=\arrayrulewidth\relax

\newcolumntype{A}[2]{%
    >{\minipage{\dimexpr#1\linewidth-2\tabcolsep-#2\arrayrulewidth\relax}\vspace\tabcolsep}%
    c<{\vspace\tabcolsep\endminipage}}
    

\newenvironment{Table}[4]{%
    \longtable{%
        |A{#1}{1.5}% for figure
        |>{\centering$\displaystyle}A{#2}{1}<{$}% for inline equation
        |>{\correction{-1}\strut\[}A{#3}{1}<{\]\strut}% for displayed equation
        |>{\centering}A{#4}{1.5}% for text
        |}\hline\ignorespaces}{%
    \endlongtable\ignorespacesafterend}

\newcommand{\dummy}{%
    It is practically a big lie that \LaTeX\ 
    makes you focus on the content without
    bothering about the layout.}


\newcommand{\Row}{%
    \includegraphics[width=\linewidth]{newton}&
    \frac{a+b}{a-b}=0&
    \int_a^b f(x)\, \textrm{d}x=\frac{b-a}{b+a}&
    \fcolorbox{cyan}{yellow}{\parbox{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}{\dummy}}
    \tabularnewline\hline}

\begin{document}
\begin{Table}{0.25}{0.25}{0.25}{0.25}
\Row
\Row
\end{Table}

\def\x{\centering$\displaystyle\int_a^bf(x)\,\textrm{d}x=\frac{a-b}{a+b}$}

\longtable{|A{0.2}{1.5}*2{|A{0.25}{1}}|A{0.3}{1.5}|}\hline
\x & \x & \multicolumn{2}{A{0.55}{1.5}|}{\x} \tabularnewline\hline
\multicolumn{2}{|A{0.45}{1.5}|}{\x} & \x & \x\tabularnewline\hline 
\x & \multicolumn{2}{A{0.5}{1}|}{\x} & \x\tabularnewline\hline 
\multicolumn{4}{|A{1}{2}|}{\x}\tabularnewline\hline 
\endlongtable
\end{document}

答案2

一个简单的方法是使用包array,用 指定列宽m{...}。例如:

\begin{tabular}{ m{4cm} m{1cm} }
   ... & ... \\end{tabular}

将生成一个 4 厘米长的列和一个 1 厘米长的列。在每个单元格中,内容将垂直对齐到中心。但请注意,单元格内容将水平左对齐。如果您还想将所有单元格内容水平对齐到中心,则可以执行以下操作:

\begin{tabular}{ >{\centering\arraybackslash} m{4cm} >{\centering\arraybackslash} m{4cm} }
   ... & ... \\end{tabular}

的目的\arraybackslash在于返回\\其原始含义,因为该\centering命令会改变它,并且可能noalign在编译期间给您带来错误。

如果您有几列并且不希望源看起来混乱,您可以在表格环境之前定义新列,例如:

\newcolumntype{C}{ >{\centering\arraybackslash} m{4cm} }
\newcolumntype{D}{ >{\centering\arraybackslash} m{1cm} }
\begin{tabular}{ C D }
   ... & ... \\end{tabular}

有很多有用的信息如果您想进一步探索这一点,请参阅 wiki LaTeX 指南。

答案3

我将使用我的新 LaTeX3 包复制 @xport 的答案tabularray使用此方法时,如果图像仍然完全垂直居中,则操作会容易得多。

具体来说,值m或被h传递给以valign=内容为中心的键,可以在文档中的 2.5 行和列部分找到。

\documentclass{article}

\usepackage[a4paper,vmargin=2cm,hmargin=1cm]{geometry}
\usepackage{graphicx}
\usepackage{xcolor}
\usepackage{tabularray}

\setlength{\parindent}{0pt}

\newcommand{\dummy}{%
  It is practically a big lie that \LaTeX\ 
  makes you focus on the content without
  bothering about the layout.%
}

\newcommand\xx{$\displaystyle\frac{a+b}{a-b}=0$}
\newcommand\yy{$\displaystyle\int_a^bf(x)\,\textrm{d}x=\frac{a-b}{a+b}$}
\newcommand\zz{\fcolorbox{cyan3}{yellow9}{\parbox{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}{\dummy}}}

\begin{document}

\noindent
\begin{tblr}{
  colspec = {X[c,h]X[c]X[c]X[c]},
  stretch = 0,
  rowsep = 6pt,
  hlines = {red5, 1pt},
  vlines = {red5, 1pt},
}
  \includegraphics[width=0.22\textwidth]{example-image-a} & \xx & \yy & \zz \\
  \includegraphics[width=0.22\textwidth]{example-image-b} & \xx & \yy & \zz \\
\end{tblr}

\bigskip

\begin{tblr}{
  colspec = {X[c,m]X[c,m]X[c,m]X[c,m]},
  cell{1}{3} = {c=2}{c}, % multicolumn
  cell{2}{1} = {c=2}{c}, % multicolumn
  cell{3}{2} = {c=2}{c}, % multicolumn
  cell{4}{1} = {c=4}{c}, % multicolumn
  rowsep = 6pt,
  hlines = {red5, 1pt},
  vlines = {red5, 1pt}, % vlines can not pass through multicolumn cells
}
  \yy & \yy & \yy &     \\
  \yy &     & \yy & \yy \\
  \yy & \yy &     & \yy \\
  \yy &     &     &     \\
\end{tblr}

\end{document}

在此处输入图片描述

答案4

有一个命令\vcenter可以在水平模式下垂直居中其内容。它只能在数学模式下使用。

下面是一个使用 Plain XeTeX 的示例(使用 编译xetex yourfilename.tex

{ \offinterlineskip
  \def\trule{\noalign{\hrule}}
  \def\hcenter#1{\hfil#1\hfil}
  \halign{\vrule#&&\hcenter{$\vcenter{\hbox{#}}$}\vrule\cr\trule
    &Lorem ipsum dolor sit amet&\XeTeXpicfile "test-pattern.jpg" &
      \TeX&$E=mc^2$&$\displaystyle{a^2-b^2\over c^2}$\cr\trule
    &Etiam quam lacus&\vrule width 4em height 5ex depth 2ex&\eTeX &
      $E\ne mc^2$&{\it \&} cetera\cr\trule}
}
\bye

在此处输入图片描述

相关内容