2

Example:

\noindent
abcd abcd abcd abcd abcd abcd abcd aaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
abcd abcd abcd abcd abcd abcd abcd aaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
abcd abcd abcd abcd abcd \hbox{abcd abcd aaaaaaaaaaaaaaaaaaa}
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
abcd abcd abcd abcd abcd abcd abcd aaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
\end

enter image description here

As you can see the line with \hbox has different spacing. My question is, if we must have this \hbox, is it possible to let TeX see through this box and stretch the spaces inside and outside in the same way?

Edit. This example shows there is a difference between the second and the third lines, which is caused by the \hbox (for sake of this problem you can ignore the first and last lines). I need this \hbox to prevent potential line breaks, but still would like its internal spaces to be stretched in the same way as the outside ones. There may be other ways to prevent line breaks, but I really like \hbox for its simplicity and would like to stick with it if there is a way to solve the stretching problem.

David Carlisle
  • 757,742
Cyker
  • 667
  • The spacing of the first two lines seems to be the same to me? I would think that the last line is a special case. – Peter Grill Dec 13 '20 at 00:58
  • Perhaps you can show your actual use case as well? It may be that there are ways around what you're after, without having to struggle with generalizations. – Werner Dec 13 '20 at 01:57
  • Are you open to a LaTeX solution? – Werner Dec 13 '20 at 02:07
  • 1
    the spacing within the box is fixed at its natural width, but your example is a bit misleading, the spacing in each line of a paragraph varies, so as to justify the text, the spacing in the first two lines here is only the same as it is not normal text and the line is repeated. – David Carlisle Dec 13 '20 at 02:12
  • You're probably looking for sloppypar or one of the other tactics suggested here: How to avoid using \sloppy document-wide to fix overfull \hbox problems? – barbara beeton Dec 13 '20 at 02:29
  • @PeterGrill Sorry this example is probably distracting. What really matters is the difference between Line 2 and 3. Updated the question. – Cyker Dec 13 '20 at 06:23
  • @Werner Use case is I want to use an \hbox to bind several pieces together so that there won't be a line break in between. But then I noticed the spaces inside are not stretched as the ones outside, which produces ugly inconsistency. While there may be other ways to prevent line breaks, I really want to apply an \hbox since it's very simple and illustrative. Any solutions are fine, TeX or LaTeX. – Cyker Dec 13 '20 at 06:26
  • @DavidCarlisle This is where I'm interested: the spacing within the box is fixed at its natural width. Is there any way to tell the box something like you should stretch your spaces using the same rules on the current context? Best I could find is \hbox to {width}, but I have to manually probe this fixed width but that is both inaccurate and clumsy. – Cyker Dec 13 '20 at 06:31
  • The last line is the end of a paragraph and is not justified. Use \raggedright to turn off justification for the other lines, or \parfillskip=0pt to justify the last line. – John Kormylo Dec 13 '20 at 16:26

2 Answers2

2

Changing the spacing inside the box changes the width of the box so would potentially change the linebreaking and spacing in the rest of the paragraph.

Depending on the higher level use case, the most natural way to make a box take part in the white space calculations for a justified paragraph is to unbox it, as in the second paragraph below.

enter image description here

\newbox\mybox
\setbox\mybox\hbox{abcd abcd aaaaaaaaaaaaaaaaaaa}

\noindent abcd abcd abcd abcd abcd abcd abcd aaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa abcd abcd abcd abcd abcd abcd abcd aaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa abcd abcd abcd abcd abcd \box\mybox
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa abcd abcd abcd abcd abcd abcd abcd aaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

\bigskip

\setbox\mybox\hbox{abcd abcd aaaaaaaaaaaaaaaaaaa}

\noindent abcd abcd abcd abcd abcd abcd abcd aaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa abcd abcd abcd abcd abcd abcd abcd aaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa abcd abcd abcd abcd abcd \unhbox\mybox
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa abcd abcd abcd abcd abcd abcd abcd aaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \end

David Carlisle
  • 757,742
  • I guess the \unhbox works like not using a box at all? This indeed exposes the box contents for white space calculation, but it no longer prevents line breaks between the box contents. So now what I'm doing is using \unhbox only at places where bad stretch is noticeable, and double check there isn't a line break happening there. Fortunately, I only have a few places in the document with very bad stretch. – Cyker Dec 14 '20 at 00:40
  • I don't think \unhbox is a solution to any real-world use, although it might answer the literal question. Notably, TeX will break lines within the text that had been boxed. – Donald Arseneau Dec 14 '20 at 01:24
  • 1
    @DonaldArseneau my assumption was that the tex was coming in boxed "from elsewhere" and the requirement was to merge it in to a paragraph while keeping white space consistent with surrounding text. I thought it worth floating this as if it is suitable it's likely to be a lot simpler than any automated mechanism that unifies the spacing while keeping things boxed. – David Carlisle Dec 14 '20 at 01:28
  • @DonaldArseneau IMO if the only reason for wanting it to remain boxed is to prevent line breaks, then maybe one could also insert huge penalties everywhere. (Or I imagine one could do something fancy in LuaTeX with whatsits or whatever, that does a post-linebreak check to see whether there has been a line break “inside”.) – ShreevatsaR Dec 15 '20 at 18:26
  • @ShreevatsaR Yes, exactly what I think. Package nolbreaks.sty does the penalties in a LaTeX context. To do literal adjustment of spaces in a box, one could reset the box in certain contexts, but paragraphs are very difficult. The only potential, I think, is with some luaTeX post-processing of the nodes. – Donald Arseneau Dec 16 '20 at 02:49
1

Since you want to keep the content together in a justified paragraph, identify the entire line that holds the content you want to keep together, and place it inside a \makebox[\linewidth][s] so it will stretch out the space between letters.

enter image description here

\documentclass{article}

\usepackage[textwidth=6.5in]{geometry} \setlength{\parindent}{0pt}% Just for this example

\begin{document}

abcd abcd abcd abcd abcd abcd abcd aaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa abcd abcd abcd abcd abcd abcd abcd aaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa abcd abcd abcd abcd abcd \hbox{abcd abcd aaaaaaaaaaaaaaaaaaa} aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa abcd abcd abcd abcd abcd abcd abcd aaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

\bigskip

abcd abcd abcd abcd abcd abcd abcd aaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa abcd abcd abcd abcd abcd abcd abcd aaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \makebox[\linewidth][s]{abcd abcd abcd abcd abcd abcd abcd aaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa} abcd abcd abcd abcd abcd abcd abcd aaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

\end{document}

Werner
  • 603,163