1

Coloring commands sometimes change how TeX breaks lines. Here's a minimal example:

\documentclass{article}
\begin{document}

% Used to produce log statements below this snippet.
\tracingparagraphs=1

% First time (uncolored)
Apple orange banana lime plum grape blueberry currant blackberry
boisenberry\hbox{BOX}  % <-- The only difference is the "boisenberry" line
avocado pluot apricot peach tomato lemon
persimmon raspberry strawberry cherry nectarine pineapple guava
mango lychee dragonfruit.

% Second time (with color)
Apple orange banana lime plum grape blueberry currant blackberry
boisenberry\pdfcolorstack0 push {1 0 0 rg 1 0 0 RG}\hbox{BOX}\pdfcolorstack0 pop
avocado pluot apricot peach tomato lemon
persimmon raspberry strawberry cherry nectarine pineapple guava
mango lychee dragonfruit.

\end{document}

Above, I color with the \pdfcolorstack commands instead of the color package as this is the minimal part of the color commands that causes this behavior.

Put the above TeX in a file called example.tex and compile it with a pdftex-based command like latexmk -pdf example, and you see this output:

enter image description here

Above, when there are no colors, there is no hyphenation (though ignore for this toy example that the line is overfull). When there is a color around the hbox, the line break algorith hyphenates "boisenberry". This is the unexpected difference in line breaking that I've been seeing.

The log from compilation example.log shows that extra [] elements are added to the horizontal list when coloring. Might this affect the line breaking algorithm? Here's the log from the line break algorithm without colors (important lines preceded with ######...

@firstpass
@secondpass
################# See below, boisenberry followed by 1 "[]"
[]\OT1/cmr/m/n/10 Apple or-ange ba-nana lime plum grape blue-berry cur-rant bla
ck-berry boisenberry[] 
@ via @@0 b=* p=0 d=*
@@1: line 1.3 t=0 -> @@0
av-o-cado pluot apri-cot peach tomato lemon per-sim-mon rasp-berry straw-berry 
@ via @@1 b=51 p=0 d=13721
@@2: line 2.1 t=13721 -> @@1
cherry nec-tarine pineap-ple guava mango ly-chee drag-on-fruit. 
@\par via @@2 b=0 p=-10000 d=*
@@3: line 3.2- t=13721 -> @@2

And here's the log with colors:

@firstpass
@secondpass
[]\OT1/cmr/m/n/10 Apple or-ange ba-nana lime plum grape blue-berry cur-rant bla
ck-berry boisen-
@\discretionary via @@0 b=1 p=50 d=2621
@@1: line 1.2- t=2621 -> @@0
################# See below, boisenberry followed by 3 "[]"s
berry[][][] av-o-cado pluot apri-cot peach tomato lemon per-sim-mon rasp-berry 
@ via @@1 b=78 p=0 d=7744
@@2: line 2.1 t=10365 -> @@1
straw-berry cherry nec-tarine pineap-ple guava mango ly-chee drag-on-fruit. 
@\par via @@2 b=0 p=-10000 d=*
@@3: line 3.2- t=10365 -> @@2

Why does pdflatex layout the text differently in these two cases? If I want to keep these commands in the same place (that is, right outside the hbox's in this case), is there some way of rewriting the command, or writing commands to consume added spaces, so that the two paragraphs have the same exact line breaks?

  • 1
    Welcome to TeX.SX! Please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem. – Stefan Pinnow Oct 15 '19 at 7:06
  • note to get the same linebreaking behaviour anyone who wants to try this has to guess exactly the same line width as you are using so only providing a fragment not a complete small test document makes it harder for anyone to answer. – David Carlisle Oct 15 '19 at 8:22
  • Thanks Stefan and David! My mistake, I meant to put in the documentclass but forgot to select it. I've updated the code sample. – Andrew Head Oct 15 '19 at 15:28
3

letters that are immediately followed by a box do not constitute a "word" to tex's scan for words to hyphenate so boisenberry is not considered for hyphenation in your first example, but the color whatsit between the letters and the box does let the word be considered.

|improve this answer|||||
  • Great, thanks @David Carlisle! This answer is key for me. While it doesn't make sense for the toy example above, is there a way to preserve the original alignment, i.e. disable the whatsit from changing the hyphenation behavior? I found through another test that you can put the pdfcolorstack commands in \llap commands and that seems to do the trick, but feels pretty hacky. – Andrew Head Oct 15 '19 at 15:37
  • @AndrewHead \hbox{\colo..\hbox{BOX}\color..} ? – David Carlisle Oct 15 '19 at 16:19
  • Yup, thanks @David Carlisle, that appears to work too :-) – Andrew Head Oct 15 '19 at 17:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.