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.

9
  • 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. Commented Dec 13, 2020 at 0: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
    Commented Dec 13, 2020 at 1:57
  • Are you open to a LaTeX solution?
    – Werner
    Commented Dec 13, 2020 at 2: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. Commented Dec 13, 2020 at 2: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? Commented Dec 13, 2020 at 2:29

2 Answers 2

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
5
  • 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
    Commented Dec 14, 2020 at 0: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. Commented Dec 14, 2020 at 1: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. Commented Dec 14, 2020 at 1: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”.) Commented Dec 15, 2020 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. Commented Dec 16, 2020 at 2: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}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .