0

Visually speaking, ending one paragraph and beginning another is just a matter of inserting a linebreak and an (potential) indent. Two ways come to mind to achieve this: \par (or empty line) and \\\indent. In my testing below, they produce exact same result visually. Is there any case where the two will produce results that differ even slightly (like kern, hypehnation/linebreak, font expansion, etc)? Will tex.linebreak be called internally only once for all text if \\\indent were used? (AFAIK tex.linebreak is called on each chunk of text separated by \par (or empty lines)) Given tex's linebreaking algorithm tries to optimize some metrics across linebreaks, and using \\\indent means all linebreaks (even across visually different paragraphs) are considered together for optimization, will this result in slowdown when number of linebreaks increase a lot?

Here's console output, code, and screenshot of output that compares \par, and \\\indent side-by-side:

Console:

\\\indent adds:
......\penalty 10000
......\glue 0.0 plus 1.0fil
......\penalty -10000

\par adds: ......\penalty 10000 ......\glue(\parfillskip) 0.0 plus 1.0fil

Why does \\ add some unnamed glue \glue 0.0 plus 1.0fil vs \glue(\parfillskip) 0.0 plus 1.0fil added by \par? Is there a separate named glue or way of controlling glue added by \\ or will it always equal glue added by \parfillskip (like it is in this case 0.0 plus 1.0fil)? If it is same by default, then why is there a hook to control one, and not another?

Code:

>>> lualatex par_or_linebreak.tex
\documentclass{article}
\usepackage[english]{babel}
\usepackage{blindtext}
\usepackage[paper=a2paper,lmargin=2in,tmargin=2in,rmargin=0.5in,bmargin=0.5in]{geometry}
\usepackage[absolute]{textpos}
\setlength{\TPHorizModule}{1in}
\setlength{\TPVertModule}{1in}
\textblockorigin{0in}{0in}

\begin{document} \showoutput \sloppy \thispagestyle{empty}

\newcommand{\sampletext}{This an equation $x=a+b^2$ without any intrinsic meaning unless one sees some. \textbf{And this is some bold text to add some variety to this piece of text.} And am typing here, difference diffierence $b=c^3$. \blindtext[1]}

\newcommand{\sampletextmore}{\blindtext[2]}

\newsavebox{\vboxwithpar} \setbox\vboxwithpar = \vbox{{\hsize=2in\relax\noindent\sampletext\par\sampletext\par\sampletext\par\sampletextmore\par}}

\newsavebox{\vboxwithlinebreak} \setbox\vboxwithlinebreak = \vbox{{\hsize=2in\relax\noindent\sampletext\\indent\sampletext\\indent\sampletext\\indent\sampletextmore\par}}

\begin{textblock}{2in}(1in,0.25in) \copy\vboxwithpar \end{textblock}

\begin{textblock}{2in}(3.5in,0.25in) \copy\vboxwithlinebreak \end{textblock}

\end{document}

Output:

Screenshot of output

codepoet
  • 1,316
  • 1
    \par ends the paragraph and \\\indent does not so the output is in general completely unrelated (and the second form should never be used). They may in some cases give similar visual output. – David Carlisle Nov 23 '20 at 07:35
  • @DavidCarlisle So the fact that the two vboxes above look exactly same is just an uncommon coincidence? – codepoet Nov 23 '20 at 07:37
  • the question is basically a duplicate of this I think https://tex.stackexchange.com/questions/82664/when-to-use-par-and-when-newline-or-blank-lines/82666#82666 – David Carlisle Nov 23 '20 at 07:38
  • there are so many ways to make the output different perhaps the most obvious is try adding \setlength\parskip{\baselineskip} to the preamble, but as well as vertical space you can get different line breaking so different numbers of lines in the paragraph, different hyphenation points in words .... – David Carlisle Nov 23 '20 at 07:41
  • @DavidCarlisle \setlength\parskip{\baselineskip}'s effect on \par can still be emulated by replacing \\ with \\[1\baselineskip]. Given both are external user intervention, I still don't see them really different... would wait for a minimal example where the contents of the paragraph intrinsically cause the results to be visually different (something that cannot be offset from outside) – codepoet Nov 23 '20 at 07:50
  • Your example is in a box so there is no page breaking but if you have page breaking then widow and orphan penalties will top single lines appearing at top or bottom of a page, but if you use \\ it is all one paragraph so you only have these penalties at the start and end of the text not for each logical paragraph. – David Carlisle Nov 23 '20 at 07:57
  • @DavidCarlisle Ok, though if I were to put all contents in a very long vbox & manually use vsplit, then it’s still equivalent right? Like will the outputs be different if I were to not use latex’s page breaking algorithm? – codepoet Nov 23 '20 at 08:00
  • Even using vsplit the results will be different, as the penalties between the lines are different. In general, the penalties between lines are different so page breaking is different, the application of \finalhyphendemerits is different so line breaking can be different, \everypar gets applied in different places. So yes (especially in luatex if you redefine the linebreaking algorithm in Lua), you can probably address each issue and make them more or less the same, but why?) – David Carlisle Nov 23 '20 at 08:07
  • Add \centering to the last paragraph. – Ulrike Fischer Nov 23 '20 at 08:19
  • @UlrikeFischer Thanks for pointing that out, it shows how limited \\\indent is. – codepoet Nov 23 '20 at 08:35

1 Answers1

5

enter image description here

\par ends the paragraph but \\ does not so using is \\\noindent almost never the correct markup, although can accidentally give the same visual output.

see When to use \par and when \\, \newline, or blank lines

It is easy to see examples of different vertical space perhaps a less obvious one is to have different linebreaking. I used a high value of \finalhyphendemerits here to make it a bit simpler to construct an example, but you could see the same effect even with the more moderate value of \finalhyhendemerits in the standard class settings.

\documentclass{article}

\finalhyphendemerits=100000

\begin{document}

\def\txt{% Random words in example paragraph text, showing different behaviours for different markup. Random words in example paragraph text, showing different behaviours for different markup. Random words in example paragraph text, showing different behaviours for different markup. Random words in example paragraph text, showing different behaviours for different markup. Random words in example paragraph text, showing different behaviours for different markup. Random words in example paragraph text, showing different behaviours for different markup. Random words in example paragraph text, showing different behaviours forzzzz different markup.\zz Random words in example paragraph text, showing different behaviours for different markup. Random words in example paragraph text, showing different behaviours for different markup. Random words in example paragraph text, showing different behaviours for different. }

\noindent \begin{minipage}[t]{6cm} \def\zz{\par} \txt \end{minipage}\quad \begin{minipage}[t]{6cm} \def\zz{\\indent} \txt \end{minipage}

\end{document}


For the question in the title, on different glue, there is no reason to expect that the glue is the same. \par is a tex primitive that causes the tex primitive \parfillskip glue to be added, \\ is a macro that in its standard latex definition ends up using

\def\@gnewline #1{%
  \ifvmode
    \@nolnerr
  \else
    \unskip \reserved@e {\reserved@f#1}\nobreak \hfil \break
  \fi}

so adds \hfil glue.

David Carlisle
  • 757,742
  • Thanks! That answers the question, and helps better understand the value of processing each paragraph by separately calling tex.linebreak on them. – codepoet Nov 23 '20 at 08:34