tabularx
我在垂直空白出现在我不希望的环境中时遇到了一些麻烦。考虑以下 MWE:
\documentclass{article}
\usepackage{tabularx}
\makeindex % removing this line also removes the
\newcommand{\loc}[1]{\index{#1@\emph{#1} (location)}\emph{#1}}
\begin{document}
\begin{tabular}{|l|}
\hline
foo \\
\hline
\loc{bar} \\
\hline
baz \\
\hline
\end{tabular}
And now for something completely different:
\begin{tabularx}{\textwidth}{|X|}
\hline
foo \\
\hline
\loc{bar} \\
\hline
baz \\
\hline
\end{tabularx}
\end{document}
两个表格的主体相同。由于某些原因,第二个表格中存在垂直间隙:
如果我注释掉该\makeindex
命令,空格就会消失,但显然这不是我实际文档中的选项。空格从何而来?我该如何避免它?
答案1
tabularx
与此无关;甚至
xxx\parbox[t]{5cm}{\loc{bar} blah}
也会出现同样的问题,即“酒吧”在“xxx”下面一行。
问题在于用“whatsit”启动 parbox。添加\leavevmode
,这在其他情况下不会造成任何有害影响:
\newcommand{\loc}[1]{\leavevmode\index{#1@\emph{#1} (location)}\emph{#1}}
完整示例
\documentclass{article}
\usepackage{makeidx}
\usepackage{tabularx}
\makeindex % removing this line also removes the
\newcommand{\loc}[1]{\leavevmode\index{#1@\emph{#1} (location)}\emph{#1}}
\begin{document}
xxx\parbox[t]{5cm}{\loc{bar} blah}
\begin{tabular}{|p{10cm}|}
\hline
foo \\
\hline
\loc{bar} \\
\hline
baz \\
\hline
\end{tabular}
And now for something completely different:
\begin{tabularx}{\textwidth}{|X|}
\hline
foo \\
\hline
\loc{bar} \\
\hline
baz \\
\hline
\end{tabularx}
\end{document}