![用与单元格垂直中间对齐的水平线填充表格单元格的左侧和右侧](https://linux22.com/image/385447/%E7%94%A8%E4%B8%8E%E5%8D%95%E5%85%83%E6%A0%BC%E5%9E%82%E7%9B%B4%E4%B8%AD%E9%97%B4%E5%AF%B9%E9%BD%90%E7%9A%84%E6%B0%B4%E5%B9%B3%E7%BA%BF%E5%A1%AB%E5%85%85%E8%A1%A8%E6%A0%BC%E5%8D%95%E5%85%83%E6%A0%BC%E7%9A%84%E5%B7%A6%E4%BE%A7%E5%92%8C%E5%8F%B3%E4%BE%A7.png)
目前,以下代码实现了所需的结果:
\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}
上述示例要求对单元格条目的每次更改进行至少两次编译(第一次也需要进行)。