Text FormattingString splitting at the end of the line

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
marco.c
Posts: 3
Joined: Sun Aug 23, 2009 6:32 pm

String splitting at the end of the line

Post by marco.c »

Hello,
I've got a problem writing a thesis in Latex
I have to show a text message sent through the Internet. The message has no spaces. Say it is "THIS_IS_A_VERY_LONG_MESSAGE_WITHOUT_SPACES_THAT_CAUSED_ME_A_LOT_OF_PROBLEMS"
The message is shown in Typewrites (with the \texttt{text} command) and is contained in a parbox.
By default, \texttt disables the hyphenation, so most of the longest lines go beyond the page border. I tried adding \hyphenchar\font45\relax before the text. This re-enables the hyphenation (and words are split by the '-' char). But this did not solve my problem, since big words with no spaces does not get split.
I'd like to find a way to make the message splittable at any character, so that the long strings would be printed in a line until the space ends, and then just continue on the next line, splitting the string in a random point when the space ends.
Do you think it's possible? (Sure it is!)

(A possible workaround would be to put \hyphenchar\font45\relax and put a \- after every character, so that strings could split anywhere, and write something like "T\-H\-I\-S\-_\-I\-S\-_\-A\-_...", but i'm sure there's a better way!)

Thanks!

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

String splitting at the end of the line

Post by gmedina »

Hi,

the first idea that comes to my mind is to use the \url command provided by the url package:
url documentation wrote: % Changing linebreaks:
% The list of characters that allow line-breaks is given by "\UrlBreaks"
% and "\UrlBigBreaks", which have the format "\do\c" for character "c"...


The package documentation can be found at the end of url.sty file.
1,1,2,3,5,8,13,21,34,55,89,144,233,...
marco.c
Posts: 3
Joined: Sun Aug 23, 2009 6:32 pm

String splitting at the end of the line

Post by marco.c »

gmedina wrote: the first idea that comes to my mind is to use the \url command
Thanks for your reply. Unfortunately, the \url command did not solve my problem, since urls are not line-broken by default. You can set the list of characters where to brake but, in my case, I have to brake at any character, so I would need something like \UrlBreaks{abcdefg...}. Of course I will use this way if i won't be able to find a better one :)
phi
Posts: 577
Joined: Tue Oct 21, 2008 8:10 pm

Re: String splitting at the end of the line

Post by phi »

Hello,
have a look at the listings package. It provides an option to do line breaks.
marco.c
Posts: 3
Joined: Sun Aug 23, 2009 6:32 pm

String splitting at the end of the line

Post by marco.c »

phi wrote:have a look at the listings package. It provides an option to do line breaks.
Neither this solved my problem. If I set lstset{breaklines=true}, the lines get broken but only at some special charachers (=, -, whitespace, ...) still making the lines longer than the page.
Post Reply