49

Up to now I don't understand well what \null actually is and when we need it. I have only known that it can be used to create hypothetical anchors as follows.

\documentclass{article}
\usepackage[a6paper,margin=2cm]{geometry}
\parindent=0pt

\begin{document}
\hfill xport \hfill\null\\
\null\hrulefill{} never \hrulefill\par
\hrulefill{} dies. \hrulefill\par
\hfill really? \hfill\null\par


\newpage
\null
\vfill
\hfill The End \hfill\null
\end{document}
  1. What is \null and when do we need to use it?
  2. Why do \hrulefill and \hfill need starting \null when they follow \\ but not need \null when they follow \par?
  3. Why does \hfill always need ending \null but \hrulefill not need it?
David Carlisle
  • 757,742
Display Name
  • 46,933

3 Answers3

47
  1. What is \null and when do we need to use it?
    \null is just \hbox{}. When is it needed? Never, actually.

  2. Why do \hrulefill and \hfill need starting \null when they follow \\ but not need \null when they follow \par?
    \hrulefill is \leavevmode\leaders\hrule\hfill\kern0pt, so it's not exactly like \hfill, as it adds a null kern after it, but very similar to it. The difference between \par\hrulefill and \\\hrulefill is that after a \par there's a box (the indent box, even with \noindent), but after \\ there's no box on the line, so glue disappears (and leaders, also).

  3. Why does \hfill always need ending \null but \hrulefill not need it?
    Because of the \kern0pt at the end of \hrulefill.

  4. Why is the first line after \begin{document} not regarded as a paragraph recall that starting \null is needed by \hfill there?
    Could you be more precise about this?

Alan Munn
  • 218,180
egreg
  • 1,121,712
  • 4
    Is it really never necessary? For example if I have some environment, which expects to find some text, but I want to tell it "take no text as text", what would be the way to not use something like \null and still tell Tex that there is empty space? (for example with tabulars with alignments) – Zelphir Kaltstahl Mar 09 '16 at 17:50
  • 3
    @Zelphir The question is about \null in the document body. If you know what you're doing, using it in the definition of an environment might be justified. Remember that it never starts or ends horizontal mode. – egreg Mar 09 '16 at 18:34
  • Actually \null is useful when one needs to ensure the correct behavior of \nofrenchspacing mode, which makes spaces after each sentence longer. By default, if a sentence ends in capital letter, TeX thinks it is an initial, and does not implement longer spacing, even though it may be desirable. To ensure the longer space happens, one can insert \null right before the ending punctuation sign. For example the sentence In USA we don't usually drink tea with milk, like they do in the UK\null. will produce a longer space after the period, than without `\null'. – mathreader Aug 24 '22 at 07:53
  • 3
    @mathreader LaTeX has \@ for that, which can be used before or after punctuation; in the latter case it poses no problem for following spaces. – egreg Aug 24 '22 at 08:09
28

TeX ignores spaces in some situation if there is no material. \null is the same as \hbox{} and it can be used for a material which reserves no space but shows TeX that there is a box which is taken into account for typesetting.

\documentclass{article}
    
\usepackage[a6paper,margin=2cm]{geometry}

    \parindent=0pt
    
    
\begin{document}
    
\hfill xport \hfill\null
    
    

\hrulefill never \hrulefill
    
    

\hrulefill dies \hrulefill
    
    
\newpage

\null
\vfill
    
\hfill The End \hfill\null
    
\vfill
    
\end{document}
Alan Munn
  • 218,180
0

Actually \null is useful when one needs to ensure the correct behavior of \nofrenchspacing mode, which makes spaces after each sentence longer. By default, if a sentence ends in capital letter, TeX thinks it is an initial, and does not implement longer spacing, even though it may be desirable. To ensure the longer space happens, one can insert \null right before the ending punctuation sign. For example the sentence In USA we don't usually drink tea with milk, like they do it in the UK\null. will produce a longer space after the period, than without \null, if the option \nofrenchspacing is enabled.

I encountered this usage in the book `TeX for the Impatient' by Paul W. Abrahams, Kathryn A. Hargreaves, and Karl Berry, available here: http://tug.ctan.org/info/impatient/book.pdf

  • 1
    As far as I remember, “TeX for the impatient” is about plain TeX. In LaTeX there's \@ that's even more efficient than \null. I still claim that \null should never be used in a LaTeX document (possible exception for the preamble). – egreg Aug 24 '22 at 13:53
  • @egreg Thanks! I did not know about \@. It is indeed easier to type. – mathreader Aug 25 '22 at 09:58