# How to remove first line indent only if paragraph has only one line?

How do I remove the first line indent in a paragraph / section / … only if this paragraph / section / … has only one line?

example:

  Lorem ipsum dolor sit amet, consetetur
sadipscing elitr, sed diam nonumy eirmod
tempor invidunt ut labore et dolore magna
aliquyam erat, sed diam voluptua.


but

Lorem ipsum dolor sit amet, consetetur.

• could do this in luatex, but I can't think of any way to do this on the main vertical list in classic tex (you could do it in \parbox or minipage where it is possible to remove boxes from the list) also there are edge cases where it takes two lines with the indent but one line if the indent is removed, so (if you want one line in that case) it isn't enough simply to check how many lines are in the paragraph when set normally – David Carlisle Oct 30 '14 at 12:37
• If you allow the paragraph to be in a macro argument the it is easy to do trial settings (as caption does) so \mypara{Lorem ipsum dolor sit amet, consetetur} could try setting #1 both ways and pick a suitable one. Is that acceptable markup? – David Carlisle Oct 30 '14 at 12:41
• Is there any particular reason for doing so? – egreg Oct 30 '14 at 12:59

This is easiest if you have the paragraph as a macro argument so it may be trial typeset with no indentation in a temporary box register:

\documentclass{article}

\def\mypara#1{\sbox0{\parbox{\linewidth}{%
\parindent0pt
#1\par\xdef\myparasize{\the\prevgraf}}}%
\ifnum\myparasize=1
{\parindent0pt #1\par}%
\else
#1
\fi}

\begin{document}

\mypara{Lorem ipsum dolor sit amet, consetetur
sadipscing elitr, sed diam nonumy eirmod
tempor invidunt ut labore et dolore magna
aliquyam erat, sed diam voluptua.}

\mypara{Lorem ipsum dolor sit amet, consetetur.}
\end{document}

• Why wouldn't the much more concise \def\mypara#1{\setbox0=\hbox{#1}\ifdim\wd0<\linewidth\noindent\fi#1} be preferable? – Steven B. Segletes Oct 30 '14 at 13:43
• @StevenB.Segletes because it wouldn't work:-) – David Carlisle Oct 30 '14 at 14:48
• @StevenB.Segletes measuring using \hbox does not take account of any shrink in the inter-word space so the hbox could be wider than a line but it would still set as one line. and using \noindent at the start can go wrong if #1 has any vertical material such as vspace or hrule – David Carlisle Oct 30 '14 at 14:49