[Tex/LaTex] Want to fill line with repeating string

loops

Similar to the \dotfill command, but with your own text, rather than dots.

If the string was: kitty!

The entire line would look like the following:
kitty!kitty!kitty!kitty!kitty!kitty!kitty!kitty!kitty!

Is there any way to do this?

Best Answer

One way of doing this is using leaders:

enter image description here

\documentclass{article}

\newcommand\kitty{\leavevmode\xleaders\hbox{kitty!}\hfill\kern0pt}

\begin{document}
This is a test. \kitty \par
This is a test.

\noindent \kitty
\end{document}

The general format for constructing leaders is \leaders<box or rule><glue> (which repeats <box or rule>). There are three kinds of leaders that you can use: \leaders, \cleaders and \xleaders. Here is an informal description of each, taken from The Advanced TeXBook:

When \leaders is used, TeX first locates the innermost box A containing the \leader command. It then fills up A, from the left, with copies of the leader. There may be some space left on the right. ...

The \cleaders command centers the leaders in the leader window, regardless of the size of the enclosing box A. There is normally some space left on both sides of the window. The \xleaders is still different. It distributes the window space evenly between the individual copies of the leader.

Here, and just for fun, the difference between using \kitty with \leaders, \cleaders and \xleaders:

enter image description here

\documentclass{article}
\usepackage{showframe}% http://showframe
\begin{document}
\noindent \leaders\hbox{kitty!}\hfill\kern0pt \par
\noindent \cleaders\hbox{kitty!}\hfill\kern0pt \par
\noindent \xleaders\hbox{kitty!}\hfill\kern0pt
\end{document}

Also see the TeXBook for reference (chapter 21 Making Boxes, p 223):

The dots you see before your eyes here . . . . . . . . . . . . are called "leaders" because they lead your eyes across the page; such things are often used in indexes or tables of contents. The general idea is to repeat a box as many times as necessary to fill up some given space. TeX treats leaders as a special case of glue; no, wait, it's the other way around: TeX treats glue as a special case of leaders. Ordinary glue fills space with nothing, while leaders fill space with any desired thing. In horizontal mode you can say

\leaders<box or rule>\hskip<glue>

and the effect will be the same as if you had said just \hskip<glue>, except that the space will be occupied by copies of the specified <box or rule>. The glue stretches or shrinks in the usual way.

Related Question