Function to define how many lines to be displayed

Here's one possibility using a \loop, \repeat construct:

\documentclass{article}

\newcounter{mycont}

\newcommand\mline[1][5]{%
\setcounter{mycont}{0}
\par\noindent\loop
\ifnum\value{mycont}<#1
\null\hrulefill\\
\stepcounter{mycont}
\repeat\par}

\begin{document}

\mline

\mline[3]

\mline[8]

\end{document}

enter image description here


The mandatory expl3 answer:

\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\mline}{ O{5} }
 {
  \par
  \prg_replicate:nn { #1 } { \hrulefill\par }
 }
\ExplSyntaxOff

\begin{document}
\mline

\medskip

\mline[2]

\medskip

\mline[4]
\end{document}

enter image description here


Here is a version using pgffor for the looping:

Code:

\documentclass{article}
\usepackage{pgffor}

\newcommand{\mline}[1][5]{%
    \foreach \x in {1,...,#1} {%
         \par\noindent\null\hrulefill{}%
    }%
    \par% Thanks to Gonzalo Medina: Need this for the case of text following \mline.
}
 
\begin{document}
\bigskip
\mline
some text.% shows why the trailing \par is required.

\mline[3]

\bigskip
\mline[2]
\end{document}

Tags:

Loops

Macros