0
$\begingroup$

Is it possible to format/style a string/text so that it is displayed in lines of fixed character length like on image without converting it to list of characters?

str = Mod[Range[0, 105], 26] /. Thread[Range[0, 25] -> Alphabet[]] // 
  StringJoin

Grid[Partition[Characters@str, 14, 14, 1, ""], 
  Spacings -> {0, 0}] // Framed

enter image description here

$\endgroup$
10
  • $\begingroup$ Would something like StringRiffle[StringPartition[str, UpTo[14]], "\n"] be what you're looking for? $\endgroup$ Commented Aug 28 at 17:43
  • $\begingroup$ @ydd It cuts off the last line. $\endgroup$ Commented Aug 28 at 17:45
  • $\begingroup$ Oh I forgot about that, see the edited comment $\endgroup$ Commented Aug 28 at 17:45
  • $\begingroup$ Can we change spacings between lines? $\endgroup$ Commented Aug 28 at 17:47
  • $\begingroup$ I guess to make it more general, if you have a list of lines lines and a list of spacings between lines spacings you could do something like Riffle[lines,spacings] // StringJoin $\endgroup$ Commented Aug 28 at 17:53

1 Answer 1

0
$\begingroup$
str = Mod[Range[0, 105], 26] /. Thread[Range[0, 25] -> Alphabet[]] // 
   StringJoin;

After setting Global Preferences, Formatting Options, Expression Formatting, LineIndent to 0.

Grid[{{str}}, ItemSize -> 8.5] // Framed

enter image description here

Row[{str}, ImageSize -> 120] // Framed

enter image description here

Column[{str}, ItemSize -> 8.5] // Framed

enter image description here

$\endgroup$
1
  • $\begingroup$ It should be something that I do not have to tune manually. The only input is length of line (in characters), say, 14. $\endgroup$ Commented Aug 28 at 21:43

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.