将表格列垂直对齐到中心直到它变得太高?

将表格列垂直对齐到中心直到它变得太高?

梅威瑟:

\documentclass{article}

\usepackage{lipsum}

\begin{document}

\newcommand{\mytable}[1]{
  \begin{tabular}{ | l | p{0.75\linewidth} | }
    \hline
    \Huge X & #1 \\
    \hline
  \end{tabular}
}

\mytable{Single line should be centered on the X}

\bigskip

\mytable{Whole paragraph should be flush at top with the X.  \lipsum[1]}

\end{document}

目前产生:

MWE 输出

第一个例子显示的内容在高度上“较短”,而第二个例子显示的内容较高。当第二列中的内容高度小于 X 的高度时,我希望第二列的内容在 X 上垂直居中。当第二列中的内容高度大于 X 的高度时,我希望第二列内容的顶部与 X 的顶部齐平。我该如何实现这一点?谢谢!

编辑:

这非常接近我想要的:

\documentclass{article}

\usepackage{lipsum,calc}

\begin{document}

\newcommand{\mytable}[1]{
  \begin{tabular}{ | l | l | }
    \hline
    \raisebox{-\height}{\Huge X}
    &
      \begin{minipage}[t][\maxof{\height}{\totalheightof{\Huge X}}][c]{10cm}
        #1
      \end{minipage}
    \\
    \hline
  \end{tabular}
}

\mytable{Single line should be centered on the X}

\bigskip

\mytable{Whole paragraph should be flush at top with the X.  \lipsum[1]}

\end{document}

几乎解决方案输出

但是为什么当用于垂直居中内部内容minipage时似乎在其上方添加了垂直空间?[c]

答案1

我不知道如何实现自动化。相反,我建议在每种特定情况下使用两个不同的命令:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{lipsum,calc}
\usepackage{makecell, cellspace}
\setlength\cellspacetoplimit{5pt}
\setlength\cellspacebottomlimit{4pt}

\newcommand{\myowntable}[1]{
  \begin{tabular}{ |Sl | p{10cm} | }
    \hline
    \makecell{\Huge X}
    & #1%
    \\
    \hline
  \end{tabular}
}

\newcommand{\mylongowntable}[1]{
  \begin{tabular}{ |Sl | p{10cm} | }
    \hline
    \raisebox{-0.6\height}{\Huge X}
    & #1%
    \\
    \hline
  \end{tabular}
}

\begin{document}

\myowntable{Single line should be centered on the X}
\bigskip

  \mylongowntable{Whole paragraph should be flush at top with the X. \lipsum[1]\leavevmode\vspace*{-2ex}}

\end{document} 

在此处输入图片描述

答案2

新答案:(我认为有更好的代码)

它应该以可靠的方式检测右列的高度和深度是否大于 X 的高度和深度,并据此以两种不同的方式设置表格,而不需要猜测任何长度\raisebox(感谢@Bernard 为小右列的表格提供的出色解决方案):

\documentclass[preview,border=4mm]{standalone}

\usepackage{array}
\usepackage{lipsum,calc}
\usepackage{makecell, cellspace}
\setlength\cellspacetoplimit{5pt}
\setlength\cellspacebottomlimit{3pt}

\newdimen\Xtotal
\newdimen\Xwidth
\newdimen\partotal

\newcommand{\mytable}[2][10cm]{
  \setbox0\hbox{\Huge X}%
  \Xtotal=\dimexpr\ht0+\dp0\relax%
  \Xwidth=\wd0\relax%
  \setbox0\hbox{\parbox[t]{#1}{#2}}%
  \partotal=\dimexpr\ht0+\dp0\relax%
  \ifdim\partotal>\Xtotal\relax%
    \begin{tabular}{ | Sl | p{#1} | }%
      \hline
      \parbox[b][\ht0][t]{\Xwidth}{\Huge X}%
      & #2\\
      \hline
    \end{tabular}
  \else%
    \begin{tabular}{ | Sl | p{#1} | }
      \hline
      \makecell{\Huge X}
      & #2%
      \\
      \hline
    \end{tabular}
  \fi%
}

\begin{document}


\mytable{Single line should be centered on the X}

\bigskip

\mytable{Whole paragraph should be flush at top with the X.  \lipsum[1]}

\bigskip

\mytable{Two lines just to check how it looks on this lovely X. So here they
come}

\end{document}

在此处输入图片描述


旧答案:

下面的操作可以满足您的要求(但不是漂亮的代码——请不要接受我的答案,等待一段时间看看是否有人有更好的答案):

\documentclass[preview,border=4mm]{standalone}

\usepackage{array}
\usepackage{lipsum,calc}
\usepackage{makecell, cellspace}
\setlength\cellspacetoplimit{5pt}
\setlength\cellspacebottomlimit{4pt}

\newdimen\hugewidth

\begin{document}

\newcommand{\mytable}[2][10cm]{
  \setbox0\hbox{\Huge X}%
  \hugewidth=\wd0\relax%
  \setbox0\hbox{#2}%
  \ifdim\wd0>#1\relax% paragraph is too wide for one row
    \setbox0\hbox{\parbox[t]{#1}{#2}}%
    \begin{tabular}{ | Sl | p{#1} | }%
      \hline
      \parbox[b][\ht0][t]{\hugewidth}{\Huge X}%
      & #2\\
      \hline
    \end{tabular}
  \else%
    \begin{tabular}{ | Sl | p{#1} | }
      \hline
      \makecell{\Huge X}
      & #2%
      \\
      \hline
    \end{tabular}
  \fi%
}

\mytable{Single line should be centered on the X}

\bigskip

\mytable{Whole paragraph should be flush at top with the X.  \lipsum[1]}

\bigskip

\mytable{Two lines just to check how it looks on this lovely X. So here they
come}

\end{document}

\documentclass{article}

\usepackage{array}
\usepackage{lipsum,calc}

\newdimen\hugeheight
\newdimen\hugewidth

\begin{document}

\newcommand{\mytable}[1]{
  \setbox0\hbox{\Huge X}%
  \hugeheight=\ht0\relax%
  \hugewidth=\wd0\relax%
  \setbox0\hbox{\parbox[b]{10cm}{#1}}%
  \ifdim\ht0>\hugeheight\relax%
    \begin{tabular}{ | l | p{10cm} | }
      \hline
      \smash{\raisebox{-0.6\hugeheight}{\Huge X}} & #1\\
      \hline
    \end{tabular}%
  \else%
    \begin{tabular}{ | m{\hugewidth} | p{10cm} | }
      \hline
      \Huge X
      &
          #1
      \\
      \hline
    \end{tabular}%
  \fi%
}

\mytable{Single line should be centered on the X}

\bigskip

\mytable{Whole paragraph should be flush at top with the X.  \lipsum[1]}

\bigskip

\mytable{Two lines just to check how it looks on this lovely X. So here they
come}

\end{document}

在此处输入图片描述

答案3

遗憾的是,如果不使用 raisebox 和一些随机数,似乎就没有解决方案。通过调整\fboxsep下面的代码,可以控制单元格内的间距:

\documentclass{article}
\usepackage{lipsum,calc}

\renewcommand{\fboxsep}{4pt} %adjust this to alter the spacing inside the table
\newcommand{\mytable}[1]{
  \noindent\begin{tabular}{ | @{}c @{}|@{} c @{}|@{} c @{}| }
    \hline
    \raisebox{1.8ex}{\fbox{\begin{minipage}[t][][c]{\widthof{\Huge X}}\Huge X \end{minipage}}} &  
    \raisebox{1.8ex}{\fbox{\begin{minipage}[t][\maxof{\height+0.1ex}{\heightof{\Huge X}}][c]{10cm}
        #1
    \end{minipage}}}
    \\
    \hline
  \end{tabular}
}

\begin{document}

\mytable{Single line should be centered on the X}

\bigskip

\mytable{Whole paragraph should be flush at top with the X.  \lipsum[1]}


\bigskip

\mytable{Now with two lines, just for fun. The lines should not be centered, since there vertical space exceeds the X.}

\bigskip

\mytable{lets \\ make \\ it \\ four}

\end{document}

在此处输入图片描述

答案4

由于我不明白的原因,\begin{minipage}[t][][c]...它完全位于基线之下——零高度,全部深度。我相信,下面的解决方案将两个列框的顶部放在同一个位置。我不确定这是否是\normalbaselineskip我将两个框升高到的正确目标,但就我看来它是正确的。(我也在cellspace看到 Bernard 和 Skillmon 使用它后添加了它,因为它确实使结果在视觉上更令人愉悦,但这不是绝对必要的。)

\documentclass{article}

\usepackage{lipsum,calc,etoolbox,cellspace}

\newtoggle{drawframes}
% Uncomment to see frames.
% \toggletrue{drawframes}
\newcommand{\maybefbox}[1]{\iftoggle{drawframes}{\fboxsep=0pt\fbox{#1}}{#1}}

\begin{document}

\newcommand{\mytable}[1]{
  \begin{tabular}{ | Sl | Sl | }
    \hline
    \maybefbox{\raisebox{-\height + \normalbaselineskip}{\Huge X}}
    & \maybefbox{\raisebox{\normalbaselineskip}{%
      \begin{minipage}
        [t]
        [\maxof{\totalheight}{\totalheightof{\Huge X}}]
        [c]
        {0.75\linewidth}
        #1
      \end{minipage}}}
    \\
  \hline
\end{tabular}
}

\mytable{Single line should be centered on the X}

\bigskip

\mytable{Whole paragraph should be flush at top with the X.  \lipsum[1]}

\end{document}

解决方案的输出

相关内容