# Function to define how many lines to be displayed

I have just made a command in my own document class wherein if I type: \mline it would automatically output 5 lines only.

My problem would be producing lines by just defining how many lines I want to be displayed by the command \mline.

## This is what I want to achieve

Examples:

• \mline[10] outputs 10 lines

• \mline[3] outputs 3 lines

## Code

\newcommand{\mline}{
\noindent \null\hrulefill{} \\
\null\hrulefill{} \\
\null\hrulefill{} \\
\null\hrulefill{} \\
\null\hrulefill{} \\
}

-
 Can you please show your definition of \mline? – Gonzalo Medina Nov 4 '12 at 1:07 This is how i make it thats why it produce 5 lines. How can i make more by just defining it as argument using \mline command? ##Code \newcommand{\mline} { \noindent \null\hrulefill{} \\ \null\hrulefill{} \\ \null\hrulefill{} \\ \null\hrulefill{} \\ \null\hrulefill{} \\ } – Kayla Nov 4 '12 at 1:08 Usually, we don't put a greeting or a "thank you" in our posts. While this might seem strange at first, it is not a sign of lack of politeness, but rather part of our trying to keep everything very concise. Upvoting is the preferred way here to say "thank you" to users who helped you. BTW: your accept rate is very less, only 60%. – Kurt Nov 4 '12 at 1:16 sorry! But Im glad of making me notified about that matter sir. – Kayla Nov 4 '12 at 1:25 I think it's worth to have a look at here svenhartenstein.de/Software/LaTeX-Questionnaires . \Qlines{5} – karathan Nov 4 '12 at 1:41
show 1 more comment

## 3 Answers

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}


-
 Thank you so much! :) I compiled my .tex file using \document{myclass} successfully without causing any error! Gracias! – Kayla Nov 4 '12 at 1:18 @kayla I'm glad I could help. ¡De nada! – Gonzalo Medina Nov 4 '12 at 1:19

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}


-

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}

-
 I tried to code this already using pgffor package but i always have a problem when i insert \usepackage{pgffor}in my own document class. – Kayla Nov 4 '12 at 1:21 @kayla You need to provide more information about the problem you're having. Error message? – Qrrbrbirlbel Nov 4 '12 at 1:30 @kayla: As Qrrbrbirlbel said more information would be useful. However as this question is really not about using the pgffor package, you could post a new question with the error message so people here can help you with that problem. You could also try usepackage{tikz}, but I suspect the problem you have is that you have old packages somewhere. Also, does this example as provided above yield the correct results for you? – Peter Grill Nov 4 '12 at 1:53