为什么使用 \everypar 的悬挂缩进会弄乱表格间距?

为什么使用 \everypar 的悬挂缩进会弄乱表格间距?

我正在尝试为表格特定列的每个段落添加悬挂缩进:

\documentclass{article}
\usepackage{array}
\newcolumntype{R}{>{\everypar{\hangindent1em\hangafter1}\arraybackslash}m{3in}}
\usepackage[a4paper, total={7in, 9in}]{geometry}
\begin{document}
\begin{tabular}{|p{1.5in}|p{1.5in}|R|}\hline
first column & second column & first line of third column \newline This line is indented. \par This one is not, but if it goes on and on and eventually wraps, the next line is indented. \\\hline
etc. & etc. & etc. \\\hline
\end{tabular}
\end{document}

问题是,通过使用\everypar,单元格顶部和文本之间的空间减少了,仅在具有悬挂缩进的单元格中:甚至第二行的第三个单元格看起来也很好。我该如何避免这种情况?

答案1

当一个p(或m)单元格启动时,\everypar它不是空的,但它包含

\vrule\@height\ht\@arstrutbox\@width\z@\everypar{}

这是在段落开头插入支柱的代码。使用你的代码,你将删除它。

\documentclass{article}
\usepackage{array}
\newcommand{\dohang}{\hangindent1em\hangafter1 }
\newcolumntype{R}{%
  >{\everypar\expandafter{\the\everypar\dohang\everypar{\dohang}}\arraybackslash}%
  m{3in}%
}
\usepackage[a4paper, total={7in, 9in}]{geometry}
\begin{document}
\begin{tabular}{|p{1.5in}|p{1.5in}|R|}\hline
first column & second column &
  first line of third column \newline
  This line is indented. \par
  This one is not, but if it goes on and on and eventually wraps, 
  the next line is indented. \\\hline
etc. & etc. & etc. \\\hline
\end{tabular}
\end{document}

在此处输入图片描述

另一种解决方案是使用 LaTeX 列表避免弄乱\hangindent\hangafter\everypar。输出是相同的。

\documentclass{article}
\usepackage{array}
\newcommand{\dohang}{\hangindent1em\hangafter1 }
\newcolumntype{R}{%
  >{\tablist}%
  m{3in}%
  <{\endtablist}%
}

\newenvironment{tablist}
 {\list{}{%
    \leftmargin=1em
    \itemindent=-1em
    \listparindent=-1em
    \topsep=0pt
    \parsep=0pt
    \partopsep=0pt}\item[\strut]}
 {\endlist\vspace{-\baselineskip}}

\usepackage[a4paper, total={7in, 9in}]{geometry}
\begin{document}
\begin{tabular}{|p{1.5in}|p{1.5in}|R|}\hline
first column & second column &
  first line of third column \newline
  This line is indented. \par
  This one is not, but if it goes on and on and eventually wraps,
  the next line is indented. \\\hline
etc. & etc. & etc. \\\hline
\end{tabular}
\end{document}

相关内容