9

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{} \\
}
Kayla
  • 1,655

3 Answers3

10

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

Moriambar
  • 11,466
Gonzalo Medina
  • 505,128
6

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

egreg
  • 1,121,712
5

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}

Peter Grill
  • 223,288
  • 1
    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 04 '12 at 01:21
  • 1
    @kayla You need to provide more information about the problem you're having. Error message? – Qrrbrbirlbel Nov 04 '12 at 01:30
  • 1
    @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 04 '12 at 01:53