8

I'm trying to add a hanging indent to every paragraph of a particular column of a table:

\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}

The problem is that by using \everypar, the space between the top of the cell and the text is reduced, only in cells that have hanging indents: even the third cell in the second row looks fine. How can I avoid this?

9

When a p (or m) cell is started, \everypar is not void, but it contains

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

which is the code that provides the insertion of a strut at the beginning of the paragraph. With your code you're removing this.

\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}

enter image description here

A different solution that avoids messing with \hangindent, \hangafter and \everypar, using LaTeX lists. The output is the same.

\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}
  • @fvogel I added a different solution, with a list. And welcome to TeX.SX! – egreg Jan 24 '15 at 22:11
  • Thanks. Why or in what cases is that preferable to the everypar method? – fvogel Jan 25 '15 at 2:59
  • 1
    @fvogel As you discovered, LaTeX uses \everypar in several situations and playing with it is not easy. The \list method, on the other hand, uses standard LaTeX constructions. – egreg Jan 25 '15 at 10:20
  • It seems that this solution does not work with longtable. See here: tex.stackexchange.com/questions/336546/… – phx Oct 29 '16 at 13:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.