Q:

Field for handwritten text

I'm preparing a small workbook. I'm using gray empty underlines as in this answer to make empty fields for handwritten text. I'm using \documentclass[a4paper,11pt]{book}. 
\documentclass[a4paper,11pt]{book}
\usepackage[francais]{babel}
\usepackage{geometry}
\usepackage{xcolor}
\geometry{paperwidth=21cm,paperheight=29.7cm, body={18cm,25.7cm}, top=2.5cm, left=1.5cm}

\begin{document}
{

\frontmatter
\tableofcontents
\cleardoublepage

\mainmatter

\chapter{Hi}
Mathematics is the study of numbers, quantity, space, structure, and change. Mathematics \color{gray}\underline{\hspace{5cm}}\color{black} is used throughout the world as an essential tool in many fields, including natural science, engineering, medicine, and the social sciences. Applied mathematics, the branch of mathematics concerned with application of mathematical knowledge to other fields, inspires and makes use of new mathematical discoveries and sometimes leads to the development of entirely new mathematical disciplines, such as statistics and game theory. Mathematicians also engage in pure mathematics, or mathematics for its own sake, without having any application in mind. There is no clear line separating pure and applied mathematics, and practical applications for what began as pure mathematics are often discove

\end{document}

I get the following output

As you can see, the underline gets out of the margins and the word "is" following it is not shown. How can I make latex treat these fields as ordinary text so for example, in \color{gray}\underline{\hspace{5cm}}\color{black} latex should treat it as \color{gray}\underline{\hspace{1.5cm}}\color{black} \\ \color{gray}\underline{\hspace{3.5cm}}\color{black} or something like that?

A:

@egreg's answer https://tex.stackexchange.com/a/22199/36296 can be applied to your example:
\documentclass[a4paper,11pt]{book}

\usepackage[T1]{fontenc}
\usepackage[french]{babel}
\usepackage{geometry}
\usepackage{xcolor}

\geometry{paperwidth=21cm,paperheight=29.7cm, body={18cm,25.7cm}, top=2.5cm, left=1.5cm}

\newcommand{\piece}{\hskip1sp\kern-1sp\hbox to .5cm{\hrulefill}}
\newcommand{\nbpiece}{\hbox to .5cm{\hrulefill}}

\makeatletter
\newcommand{\build}[2]{\leavevmode
  \count@=\z@ \toks@={}%
  \loop\ifnum\count@<\numexpr#1\relax
    \toks@=\expandafter{\the\toks@#2}%
    \advance\count@\@ne
  \repeat
  \the\toks@}
\makeatletter

\newcommand{\blank}[2][1]{%
  \build{#1}{\nbpiece}\build{2*(#2-#1)}{\piece}\build{#1}{\nbpiece}}

\begin{document}

Mathematics is the study of numbers, quantity, space, structure, and change. Mathematics {\color{lightgray}\blank{5}} is used throughout the world as an essential tool in many fields, including natural science, engineering, medicine, and the social sciences. Applied mathematics, the branch of mathematics concerned with application of mathematical knowledge to other fields, inspires and makes use of new mathematical discoveries and sometimes leads to the development of entirely new mathematical disciplines, such as statistics and game theory. Mathematicians also engage in pure mathematics, or mathematics for its own sake, without having any application in mind. There is no clear line separating pure and applied mathematics, and practical applications for what began as pure mathematics are often discove

\end{document}

A:

Maybe you can try the dashundergaps package. See documentation.
\documentclass[a4paper,11pt]{book}
\usepackage[francais]{babel}
\usepackage{geometry}
\usepackage{xcolor}
\usepackage{dashundergaps}
\geometry{paperwidth=21cm,paperheight=29.7cm, body={18cm,25.7cm}, top=2.5cm, left=1.5cm}

\dashundergapssetup{
    ,teacher-mode = true
    ,gap-numbers = false
    %,gap-number-format  = \,\textsuperscript{\normalfont(\thegapnumber)}
    ,gap-font           = \itshape
    ,teacher-gap-format = underline
    ,gap-widen
    ,gap-extend-percent=20
}

\begin{document}
    {

        \frontmatter
        \tableofcontents
        \cleardoublepage

        \mainmatter

        \chapter{Hi}
        Mathematics is the study of numbers, quantity, space, structure, and change. Mathematics \gap{Empty box in student mode} is used throughout the world as an essential tool in many fields, including natural science, engineering, medicine, and the social sciences. Applied mathematics, the branch of mathematics concerned with application of mathematical knowledge to other fields, inspires and makes use of new mathematical discoveries and sometimes leads to the development of entirely new mathematical disciplines, such as statistics and game theory. Mathematicians also engage in pure mathematics, or mathematics for its own sake, without having any application in mind. There is no clear line separating pure and applied mathematics, and practical applications for what began as pure mathematics are often discove

\end{document}

