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?


soulpackage can line break underlined text, see e.g. https://tex.stackexchange.com/a/9552/36296 – samcarter_is_at_topanswers.xyz Sep 28 '18 at 14:51