TransWikia.com

How can I prevent Mathematica from cutting sentences in the middle when printing strings?

Mathematica Asked on October 22, 2021

I have a very simple but annoying problem. Consider the following example:

Print["XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXX 
XXXXXX XXXXXX XXXX X XXXXXXXXXXXXXX XXXX XXXX XXX XXXXXXXXXX XXXXXX 
XXXXXXXXXXX XX XXXXXXXXXX XX XXX XXXXXXXXXX XXXXXX XXXXXXXXXXX XX 
XXXXXXX XXXXXX"]

On my system, in a Mathematica notebook, the output looks on the screen as follows:

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXX XXXXXX XXXXXX XXXX X
XXXXXXXXXXXXXX XXXX XXXX XXX XXXXXXXXXX XXXXXX XXXXXXXXXXX XX XXXXXXXXXX XX XXX XXXXXXXXXX XXXXXX XXXXXXXXXXX XX XXXXXXX XXXXXX

This of course changes when the windows size changes, but the point is that for some reason, the second line is much longer than the first, and it looks ugly and unnatural.

What I would like instead is to have something like:

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXX XXXXXX XXXXXX XXXX X XXXXXXXXXXXXXX XXXX XXXX XXX
XXXXXXXXXX XXXXXX XXXXXXXXXXX XX XXXXXXXXXX XX XXX XXXXXXXXXX XXXXXX XXXXXXXXXXX XX XXXXXXX XXXXXX

with the first line being longer, and the full width of the window.

However, putting a new line at a particular position only solves this problem for a particular window size; when the window size changes, it no longer looks good. I want Mathematica to keep the first line longer than the second no matter what the window size is, and/or prevent it from splitting the string at an unnatural position.

  1. Why does Mathematica split the text at this very unnatural point in the first place? It doesn’t really make sense, unless I’m missing something.
  2. How can this be avoided, such that for any window size, the first line is always longer than the second?

Thanks in advance.

One Answer

Input cells linebreak as math, while text cells linebreak as text. So, use:

Print @ TextCell["XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXX XXXXXX XXXXXX XXXX X XXXXXXXXXXXXXX XXXX XXXX XXX XXXXXXXXXX XXXXXX XXXXXXXXXXX XX XXXXXXXXXX XX XXX XXXXXXXXXX XXXXXX XXXXXXXXXXX XX XXXXXXX XXXXXX"]

to enforce text linebreaking.

Answered by Carl Woll on October 22, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP