# How to wrap text in line by adding spaces

I am writing one text which has some very long words but i don't need to split word in multiline using hyphen. I just want to add spaces to fit in the line as shown below.

       This is my first text for wrapping word
in a file. This text need   to       be
adjusted and acted based   on the table
which need to be  modified   and tested
basedonthethedata  which is given here.


How can i implement it?

-

## Normal Length Words:

One way to do this is to use \nohypens from the hyphenat package. Here is a comparison without (on the left) and with \nohypens (on the right):

## Notes:

• If you wish to disable hyphenation for the entire document (not that I am recommending that), you can use

\usepackage[none]{hyphenat}


## Code:

\documentclass{article}

\usepackage{hyphenat}

\newcommand{\Text}{%
This is my first text for wrapping word
in a file. This text need   to       be
adjusted and acted based   on the table
which need to be  modified   and tested
basedonthethedata  which is given here.
}%

\begin{document}
\begin{minipage}{0.4\linewidth}
\Text
\end{minipage}%
\hspace*{2ex}
\begin{minipage}{0.4\linewidth}
\nohyphens{\Text}
\end{minipage}%
\end{document}


## Very Long Words:

If you have really long words, you need to specify where the breaks can occur in the word. Below, I have used a solution from How to break long word containing symbols but with no hyphen? to automatically add a possible break after every letter (the macro is not complete, but the code just needs to be duplicated for each letter/character after which you want to allow a break).

## Code:

\documentclass{article}

\usepackage{hyphenat}
\usepackage{xstring}
\usepackage{forloop}

% http://tex.stackexchange.com/questions/53965/how-to-break-long-word-containing-symbols-but-with-no-hyphen
\newsavebox\MyBreakChar%
\sbox\MyBreakChar{}% char to display the break after non char
\newsavebox\MySpaceBreakChar%
\sbox\MySpaceBreakChar{\hyp}% char to display the break after space
\makeatletter%
\newcommand*{\BreakableChar}[1][\MyBreakChar]{%
\leavevmode%
\prw@zbreak%
\discretionary{\usebox#1}{}{}%
\prw@zbreak%
}%
\makeatother

\newcounter{index}%
\StrLen{#1 }[\stringLength]%
\forloop[1]{index}{1}{\value{index}<\stringLength}{%
\StrChar{#1}{\value{index}}[\currentLetter]%
\IfStrEqCase{\currentLetter}{%
% All the characters where you don't want hypen
{a}{\currentLetter\BreakableChar[\MyBreakChar]}%
{b}{\currentLetter\BreakableChar[\MyBreakChar]}%
{c}{\currentLetter\BreakableChar[\MyBreakChar]}%
{d}{\currentLetter\BreakableChar[\MyBreakChar]}%
{e}{\currentLetter\BreakableChar[\MyBreakChar]}%
{f}{\currentLetter\BreakableChar[\MyBreakChar]}%
%  list other letters here.
{z}{\currentLetter\BreakableChar[\MyBreakChar]}%
% All the charactes where a break should have a hypen
%{ }{\currentLetter\BreakableChar[\MySpaceBreakChar]}%
}[\currentLetter]%
}%
}%

\newcommand{\Text}{\setlength\emergencystretch{.5\textwidth}%
This is my first text for wrapping word
in a file. This text need   to       be
adjusted and acted based   on the table
which need to be  modified   and tested
basedonthethedata  which is given here.\par
}%

\begin{document}
\begin{minipage}{0.4\linewidth}
\Text
\end{minipage}%
\hspace*{2ex}
\begin{minipage}{0.4\linewidth}
\nohyphens{\Text}
\end{minipage}%
\end{document}

-
It works fine but i don't want to use minipage as i have almost 100 sections in my report where i need to wrap text. Is there any way to use one global command for all the text in a report. –  user15662 Mar 13 '13 at 3:58
@user15662: You don't need to use a mipipage. That was just to show that the text wrapping worked by shortening the width of the page. –  Peter Grill Mar 13 '13 at 4:03
I mean to to say i don't want to repeat this command(\nohyphens) frequently. Is there any equivalent global command. –  user15662 Mar 13 '13 at 4:05
@user15662: Have already updated solution to show that. –  Peter Grill Mar 13 '13 at 4:06
i have one very long name in sentence but it goes out of margin. "This text is having very lengthy word (Having all medical terms) like Ultramicroscopicvolcanoconiosis, Pneumonoultramicroscopicsilicovolcanoconiosis." Ouput: postimage.org/image/iqtape4uf/6398e4be –  user15662 Mar 22 '13 at 9:29
show 1 more comment