如何让文本在表格的垂直线上居中

如何让文本在表格的垂直线上居中

我有以下内容:

\documentclass{standalone}
\usepackage{tabularx}

\begin{document}
  \renewcommand\tabularxcolumn[1]{m{#1}}
  \begin{tabularx}{\linewidth}{XcX}
    I am the first of \newline two lines of text &
    \emph{a{\vrule height -.5em}{\hss\vrule depth -1em}nd} &
    the rules in the center should be aligned in the middle \newline
    but they are not \newline
    and it looks bad.
  \end{tabularx}
\end{document}

渲染结果为

在此处输入图片描述

很遗憾:

  • 垂直线未位于单词“and”的中心
  • “a”和“n”之间有多余的空格

我希望用来\rlap解决这些问题,但它似乎垂直截断了框,以至于根本没有绘制任何规则。

我该如何解决上述两个问题?

答案1

我使用了\valign带有单个声明行的原语。每个项目都带有给定的参数。垂直规则由和TeX 原语\vbox创建。单词(当然)在里面以两个 为中心。这在两个相等的 之间,因此它也是垂直居中的。\omit\leadersand\hbox to 0pt\hss\hbox\leaders\vrule\vfill

\def\twocol #1#2{%
   \hbox{
      \valign{\vfil\vboxx{##}\vfil \cr
         #1\cr \omit \leaders\vrule\vfill \hbox to0pt{\strut\hss and\hss} 
                     \leaders\vrule\vfill \cr #2\cr
      }
   }%
}
\def\vboxx#1{\vbox{\hsize=.35\hsize \leftskip=2em \rightskip=2em 
             \def\\{\hfil\break} \noindent\strut #1\strut}}

\twocol{I am the first of\\ two lines of text}
       {the rules in the center should be aligned in the middle
        but they are not and it looks bad.} 

在此处输入图片描述

答案2

使用nicematrix(和),您可以定义一个在规则上放置的tikz新规则样式。and

在此处输入图片描述

\documentclass{article}

\usepackage{nicematrix, tikz}
\usetikzlibrary{decorations.markings}

\tikzset{
    andlabel/.style={
        postaction={decorate, decoration={markings, mark={at position .5 with \node[fill=white]{\itshape and};}}}
    }
}

\begin{document}

\NiceMatrixOptions
    {custom-line={letter=I, tikz={andlabel}, width=12mm}}
  \begin{NiceTabular}{cIc}
  \Block[l]{1-1}{I am the first of\\two lines of text} &
  \Block[l]{1-1}{the rules in the center should be\\aligned in the middle\\but they are not\\and it looks bad.}
  \end{NiceTabular}

\end{document}

答案3

一个解决方案nicematrix

\documentclass{standalone}
\usepackage{nicematrix}
\usepackage{tikz}

\begin{document}
  \begin{NiceTabular}{X[m]cX[m]}
    I am the first of \newline two lines of text &
    \emph{and} &
    the rules in the center should be aligned in the middle \newline
    but they are not \newline
    and it looks bad.
  \CodeAfter
    \begin{tikzpicture}
    \draw (1-2.north) -- (1-|1-2.north) ; 
    \draw (last-2.south) ++ (0,-2pt) -- (last-|1-2.south) ; 
    \end{tikzpicture}
  \end{NiceTabular}
\end{document}

上述代码的输出

答案4

我想出了

\documentclass{standalone}
\usepackage{tabularx}

\begin{document}
  \renewcommand\tabularxcolumn[1]{m{#1}}
  \begin{tabularx}{\linewidth}{XcX}
    I am the first of \newline two lines of text &
    \newbox\mybox
    \setbox\mybox=\hbox{\strut\emph{and}}
    {\vrule height -2pt}\hspace{-\arrayrulewidth}%
    {\vrule depth -6pt}\hspace{-0.5\arrayrulewidth}%
    \hspace{-.5\wd\mybox}\box\mybox &
    the rules in the center should be aligned in the middle \newline
    but they are not \newline
    and it looks bad.
  \end{tabularx}
\end{document}

它用于setbox保存“和”的宽度,然后堆积一些hspace以重叠所有内容。

输出

相关内容