![如何让文本在表格的垂直线上居中](https://linux22.com/image/474055/%E5%A6%82%E4%BD%95%E8%AE%A9%E6%96%87%E6%9C%AC%E5%9C%A8%E8%A1%A8%E6%A0%BC%E7%9A%84%E5%9E%82%E7%9B%B4%E7%BA%BF%E4%B8%8A%E5%B1%85%E4%B8%AD.png)
我有以下内容:
\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
\leaders
and
\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
以重叠所有内容。