Forum How do I...?

Line breaks with specific words

ixgal
Hello,

On the PDF output, I would like to add automatic line breaks in h1, h2, etc., depending on the final word of the line. For example, let's think of this title:
"This is a title with
two lines"

For aesthetic reasons, I would prefer to have the word "with" at the beginning of the second line instead of having it at the end of the first line:
"This is a title
with two lines"

Is it possible to add some code so that specific words (articles, prepositions, etc.) never appear at the end of the line in titles h1, h2, etc.?

Thanks

pjrm
A literal answer to the question would be that you could use a non-breaking space after each preposition, whether with script or with "h1, h2 { prince-text-replace: 'with ' 'with\a0'; }".

However, I suggest that you don't necessarily want to force prepositions never to occur at the end of a line: in a title like

This is a title
among counter-examples

the preposition might well be better at the end of the preceding line.

You can get Prince for Books to make this decision (using "mechanical" criteria such as line length) by using markup such as

<style>.phrase { prince-wrap-inside: avoid; }</style>
<h1>This is a title <span class="phrase">among counter-examples</span></h1>

where prince-wrap-inside:avoid is a less forceful alternative to &#nbsp; or white-space:nowrap. For headings (or other non-justified text), it's best used with a recent build and combined with prince-line-break-choices: heading-lookahead (or body-lookahead for body text).

(See also https://www.princexml.com/doc/11/properties/prince-wrap-inside/, https://www.princexml.com/doc-prince/#pfb-princewrapinside.)

This less forceful alternative will tend to keep the associated text unbroken when the result looks reasonable in terms of line lengths (as in the "This is a title / with two lines" example) while allowing breaking in cases like the "... among / counter-examples" case.

However, even this isn't always what one wants, because the choice can be a matter of author intent: sometimes one wants a connective to be at the end of a line in order to give more prominence to what follows it. A book on learning to drive in Northern Africa might have a section related to the Citroën Sahara entitled

Changing Gears with
Two Engines

because the startling notion of a car with two engines is the most important part of the title. Whereas if the same title occurred in a book about the Citroën Sahara, then the important thing is "Changing Gears", and "with" would best go to the next line.

So, while one could write script to wrap each preposition and following word in a span and style it with prince-wrap-inside:avoid (or to force it by using prince-text-replace and a non-breaking space as mentioned earlier), you might be better off marking up phrases manually with human thought as to what's wanted.
ixgal
thanks!
It's really useful.
I think I will first try with prince-text-replace.

In that case, is there a way to use that property applied to several words?
For example, I would like to apply it to "with", "to", "the" in h2.

pjrm
'prince-text-replace' syntax is similar to that of 'counter-reset' in its "flattened list of pairs". One would usually split the declaration over multiple lines, for readability:

h2 {
  prince-text-replace:
    'with ' 'with\a0'
    'to ' 'to\a0'
    'the ' 'the\a0'
    /* ... and so on for each prepositions, conjunctions etc. */
    ;
}

[Incidentally, I don't know that changing gears was any different from any other 2CV. Perhaps a slight added complexity when front & rear wheels are rotating at noticeably different speeds as might perhaps occur when going around a tight corner.]
ixgal
thanks a lot, it works perfect.
And thanks also for the info about the 2CV ;)
ixgal
I tried to replace some text but got an error message.

The code is:

h1, h2, h3, h4, h5, h6 {
prince-text-replace:
'\a0cabe ' '\a0cabe\a0'
;
}

The idea is to replace:
non-breaking-space+word "cabe"+ simple space
with
non-breaking-space+word"cabe"+non-breaking-space

But I get this error message: "internal error: char.det_from_int: conversion failed"
It's strange because the error happens with this word "cabe" but not with a few others thay I tested.

Any tip on how I can solve it?

Thanks!




mikeday
You need a space after \a0, otherwise it will interpret it as hexadecimal number \a0cabe :D
ixgal
thanks!!
pjrm
What version of Prince are you using? Do you get the same error message with the attached file?

At least with the attached document, current Prince maps \a0cabe to u+fffd (replacement character) as per css-syntax spec rather than giving an error message; so the last example of this document (in which the search string is written as \a0 cabe but the replacement string as \a0cabe) shows as a diamond containing a question mark.
  1. hex-escape.html0.8 kB
    test document
ixgal
thanks! It worked with the extra space.
pjrm
The reason I ask is that if someone does forget to add the extra space, then we'd prefer for it not to stop Prince with an error message.
ixgal
I am using Prince 10 rev 7