[Tex/LaTex] How to wrap text in line by adding spaces

hyphenationline-breakingwrap

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?

Best Answer

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):

enter image description here

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).

enter image description here

Code:

\documentclass{article}

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

% https://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}%
\newcommand{\AddBreakableChars}[1]{%
  \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 
    \AddBreakableChars{Ultramicroscopicvolcanoconiosis},
    \AddBreakableChars{Pneumonoultramicroscopicsilicovolcanoconiosis}
    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}