用与单元格垂直中间对齐的水平线填充表格单元格的左侧和右侧

用与单元格垂直中间对齐的水平线填充表格单元格的左侧和右侧

目前,以下代码实现了所需的结果:

\begin{tabular}{ccc}
1. & Lorem ipsum dolor sit amet & 10\tabularnewline
2. & \textemdash \textemdash \textemdash \textemdash \textendash \textemdash{}
`` \textemdash \textemdash \textemdash \textemdash \textendash \textemdash{} & 20\tabularnewline
3. & \textemdash \textemdash \textemdash \textemdash \textendash \textemdash{}
`` \textemdash \textemdash \textemdash \textemdash \textendash \textemdash{} & 30\tabularnewline
\end{tabular}

期望结果:

在此处输入图片描述

但是,上述代码的问题是我必须手动找出要输入的适当“\textemdash”数量。

我想知道是否有一个命令可以自动实现类似的结果,即用水平线填充表格单元格左侧和右侧的剩余空间。

我尝试过 \hrulefill 但产生的水平线位于单元格的底部(而不是垂直中间):

\begin{tabular}{ccc}
1. & Lorem ipsum dolor sit amet & 10\tabularnewline
2. & \hrulefill{} `` \hrulefill & 20\tabularnewline
3. & \hrulefill{} `` \hrulefill & 30\tabularnewline
\end{tabular}

在此处输入图片描述

答案1

您可以测量最宽元素的宽度并在p{<len>}列规范中使用它,然后使用\xfill

在此处输入图片描述

\documentclass{article}

% Taken from http://mirrors.ctan.org/macros/latex/contrib/xhfill/xhfill-doc.tex
\newcommand{\xfill}[2][.7ex]{{%
  \dimen0=#2\advance\dimen0 by #1
  \mbox{}\leaders\hrule height \dimen0 depth -#1\hfill%
}}

\begin{document}

\begin{tabular}{ c c c }
  1. & Lorem ipsum dolor sit amet & 10 \\
  2. & \textemdash \textemdash \textemdash \textemdash \textendash \textemdash{}
    `` \textemdash \textemdash \textemdash \textemdash \textendash \textemdash{} & 20 \\
  3. & \textemdash \textemdash \textemdash \textemdash \textendash \textemdash{}
    `` \textemdash \textemdash \textemdash \textemdash \textendash \textemdash{} & 30
\end{tabular}

\newlength{\widestlength}
\settowidth{\widestlength}{Lorem ipsum dolor sit amet}

\begin{tabular}{ c p{\widestlength} c }
  1. & Lorem ipsum dolor sit amet & 10 \\
  2. & \xfill{.4pt} `` \xfill{.4pt} & 20 \\
  3. & \xfill{.4pt} `` \xfill{.4pt} & 30
\end{tabular}

\end{document}

您可以使用以下方法自动查找最宽元素eqparbox\eqmakebox[<tag>]{<stuff>}collcell\collectcell<cmd>...\endcollectcell列规范。后者将单元格条目传递给单参数宏<cmd>进行处理。

在此处输入图片描述

\documentclass{article}

\makeatletter
\newcommand{\xfill}[1]{%
  \leavevmode\leaders\hrule height \dimexpr.7ex+#1\relax depth -.7ex\hfill\kern\z@%
}
\makeatother

\usepackage{eqparbox,collcell}
\newcommand{\tagname}{}
\newcommand{\samewidth}[1]{%
  \eqmakebox[\tagname][c]{#1}%
}

\begin{document}

\renewcommand{\tagname}{lorem}
\begin{tabular}{ c >{\collectcell\samewidth}c<{\endcollectcell} c }
  1. & Lorem ipsum dolor sit amet & 10 \\
  2. & \xfill{.4pt} `` \xfill{.4pt} & 20 \\
  3. & \xfill{.4pt} `` \xfill{.4pt} & 30
\end{tabular}

\renewcommand{\tagname}{ipsum}
\begin{tabular}{ c >{\collectcell\samewidth}c<{\endcollectcell} c }
  1. & Lorem ipsum dolor sit & 10 \\
  2. & \xfill{.4pt} `` \xfill{.4pt} & 20 \\
  3. & \xfill{.4pt} `` \xfill{.4pt} & 30
\end{tabular}

\end{document}

上述示例要求对单元格条目的每次更改进行至少两次编译(第一次也需要进行)。

相关内容