0

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}
lockstep
  • 250,273
Vaibhav
  • 6,625
  • 15
  • 49
  • 76
  • 6
    Why don't you look at the answers to your last question, which was almost identical, "dotted line with appropriate length for answering a question?"? The commands \fillin and \answerline in the answers provide exactly what you are asking for. – gernot Oct 03 '16 at 10:49
  • Oh! Yes! That's sort of duplicate. Regrets! But have a look at my comment below ? – Vaibhav Oct 03 '16 at 10:53
  • Just to understand the syntax, can you please explain \def\dotfill#1{\cleaders\hbox to #1{.}\hfill} – Vaibhav Oct 03 '16 at 10:54
  • In your comments I can't find any argument why the answers there don't satisfy your needs. If you don't want to define a new command, then the answer is simple: No, there is no predefined command with an argument to fill the given width with dots. But it can be easily defined. – gernot Oct 03 '16 at 10:55
  • 2
    the correct answer to this question is @gernot's answer to the duplicate question that you asked before, why ask again? – David Carlisle Oct 03 '16 at 11:02

1 Answers1

6

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

  • 1
    Thanks. That's great. But is there no way other than to create a new command ? – Vaibhav Oct 03 '16 at 10:49
  • @Vaibhav To my knowledge, this is the LaTeX way to make periodic lines. If you change the {.} to {-} in the definition, for example, you will have a dashed line. – Steven B. Segletes Oct 03 '16 at 10:51
  • the natural latex definition is as given in the duplicate question \makebox[3cm]{\dotfill} (or with a dotfill variant controlling the spacing) but if you use \hbox best to have a \leavevmode (see difference if you have \dotline{2.1cm}\dotline{2.1cm} at the start) – David Carlisle Oct 03 '16 at 11:06