[Tex/LaTex] customizing the length of \dotfill

fillline

How can I set a length of \dotfill ?
I used \dotfill {3 cm} and \dotfill \phantom{3 cm} without success. I can create a box and put dotted line there, but can it not be simpler?

\documentclass{article}
\begin{document}
Write down your name ? \dotfill \newline 
What is your age ? \dotfill {3 cm} and Gender \dotfill {3cm} 
\end{document}

Best Answer

Here is \dotline[<period>]{length}. EDITED to \leavevmode.

\documentclass{article}
\def\dotfill#1{\cleaders\hbox to #1{.}\hfill}
\newcommand\dotline[2][.5em]{\leavevmode\hbox to #2{\dotfill{#1}\hfil}}
\begin{document}
\dotline{2.1cm}

xyz\dotline[2pt]{30pt}pdq
\end{document}

enter image description here

Related Question