Optimal Line Break Calculator
Nearly every text box on nearly every screen wraps greedily: fit as many words as you can on this line, then start another. It is fast and it is often not the tidiest answer, because cramming one line can leave the next three ragged. Considering the paragraph as a whole gives a visibly more even block, and the arrangement found here is not merely better — for a short paragraph it is proved to be the best there is.
What this generator does
Scores each line by how far short of the measure it falls, then finds the set of breaks with the lowest total — and sets the same paragraph greedily alongside it for comparison.
How to use this tool
- Paste a paragraph.
- Set the measure, in characters, that a line may run to.
- Compare the two blocks — the tidiest arrangement against filling each line in turn.
- Narrow the measure to see the difference grow.
Understanding the controls
- The paragraph
- Up to 120 words. Runs of spaces are treated as one, and no word may be longer than the measure.
- Measure
- How many characters a line may run to. Narrow measures make the difference between the two approaches much more visible.
Worked examples
- Where greedy visibly loses
- "a bb ccc dddd eeeee ffffff a bb ccc dddd eeeee ffffff" at a measure of 14 costs 10 set greedily but only 6 set optimally — the optimal arrangement moves one word to the next line so two lines end up short rather than one line ending up very short.
Common use cases
- Setting a pull quote or heading so the lines balance
- Working out where to break a caption by hand
- Comparing wrapping approaches before choosing one
- Explaining why a text block looks ragged when nothing is wrong with it
- Teaching dynamic programming with a result you can see
How this generator works
The cost of a line is the square of what it falls short by, which punishes one very short line more than two slightly short ones. The last line is free, since a paragraph is meant to end early. For paragraphs of eighteen words or fewer, every possible set of breaks is enumerated and compared, so the arrangement shown is demonstrably the best rather than the best found.
Randomness and fairness
Nothing is random. The same paragraph and measure always give the same arrangement.
For how randomness is produced across the whole site, see how Generate Random works.
Limitations and good to know
- Widths are counted in characters, so this matches a monospaced setting. Proportional type needs real character widths.
- No hyphenation: words are never split, which is a real constraint that changes what is possible.
- The cost is the square of the shortfall, which is one reasonable choice among several — cubes punish a bad line harder.
- Widows, orphans, rivers and the other things typesetters care about are not considered.
Privacy and your data
Your text stays in your browser. Nothing you paste is transmitted or stored.
Frequently asked questions
- Why isn't greedy wrapping just as good?
- Greedy wrapping decides one line at a time and never looks ahead, so cramming a word onto the current line can leave several later lines much shorter than they needed to be. Considering the whole paragraph at once can trade a slightly earlier break for a visibly more even result overall — the example above shows the same words costing 10 greedily against 6 optimally.
- Is the 'optimal' result really the best possible, or just a good one?
- For a paragraph of 18 words or fewer, every possible set of line breaks is actually enumerated and compared, so the result shown is provably the best — not merely the best this method happened to find. Longer paragraphs use the same dynamic-programming approach used in real typesetting systems, which is optimal for this cost function but isn't re-verified by brute force at that length.
Related generators
- Plus Code ConverterTurns a latitude and longitude into an Open Location Code, and shows exactly which patch of ground it names.
- Geohash ConverterTurns coordinates into a geohash, whose useful trick is that nearby places usually share a prefix.
- Colour Palette GeneratorGenerate a real colour palette — six harmony types, with hex, RGB and HSL values and the contrast each colour gives with text.
- Print Resolution CalculatorHow large an image prints at a given resolution, in inches and centimetres, with an honest verdict on quality.