I wanted to create a new command, which allows me to repeat
any characters
I want for n times
. After some searching and trying, I came up with this:
\usepackage{multido}
\newcommand{\myrepeat}[2]{%
\newcount\iterations%
\iterations #1%
\advance\iterations -1
\multido{\iN=0+1}{\iterations}{#2\ }#2%
}
There was some weird spacing after the command in the resulting PDF, so I added the comment symbols %
and then it disappeared.
My question is: Is there a better way of doing this, which is as easy to understand as this one, preferably not introducing many dependencies?
One without multido
is fine too, if it's not too complicated, or if you can explain it, so that it's not complicated anymore.
I guess using multido
for such a thing is ok, since its name means to do something multiple times, but I am not sure I took the easiest and cleanest way of doing it.
Note that my solutions adds one less space than it adds items to the pdf. The way of substracting one seems suspiciously verbose to me.
I've got two versions working now:
The one from @egreg modified:
\makeatletter
\newcount\my@repeat@count% initialize a new counter for the loop
\newcommand{\myrepeat}[3]{% new command with 2 arguments
\begingroup% ???
\my@repeat@count=1% initialize at 1, so that there are argument - 1 iterations and the last iterations doesn't have a separator following it
\@whilenum\my@repeat@count<#1\do{#2#3\advance\my@repeat@count1}#2% as long as the iteration count is smaller than the argument, advance, meaning that the counter will be increased by 1
\endgroup% ???
}
\makeatother
\newcommand{\mediumgap}{%
\myrepeat{5}{.....}{\ }
}
The one from @christian modified:
\newcount\myloopcounter
\newcommand{\repeatit}[3][10]{%
\myloopcounter1% initialize the loop counter
\loop\ifnum\myloopcounter < #1
#2#3%
\advance\myloopcounter by 1%
\repeat% start again
#2%
}
\newcommand{\longgap}{%
\repeatit[5]{.....}{\ }
}
I don't know if one has any advantage over the other. Maybe there is also a better way of removing the last whitespace or separation character, instead of doing one less iteration and writing only the charactersto repeat again without the separator. I introduced the separator, because I thought it could be useful and it's just a third parameter.
%
after-1
otherwise\multido
wlil start expanding before the\advance
is finished. (it probably accidentally works in this case but...)%
after-1
, still works, thanks! Although I don't understand in which cases it might be a problem. Can you explain? @Werner I saw that one, but didn't find an acceptable answer, and went on searching. Inacceptable:PGF/TikZ
dependency,expl3
looks like manually writing a loop for only 12 cases,\romannumeral
solution thingy I don't understand what's going on there,\numexpr
solution thingy looks complicated too and I don't understand it as no explanation in detail was given. Note: Still a beginner, this is one of the first commands I define.\newcount
in the command, since this allocates a register each time the loop command is used