# Fill space created by \phantom with other text

I am looking for a command I am pretty certain to have used before but which I cannot recall (and I did search for it):

I want to create (horizontal) space of some length, e.g. created using \phantom{text}, with some other (shorter) text. It should simply fill the white space for as far as the replacing text reaches, i.e. it does not need to be stretched to fill the whole space.

I hope I explained this sufficiently since providing a MWE is hard without knowing the answer...

• What you describe sounds like \phantom to me! Can you explain why this doesn't do what you require (and perhaps add a picture to illustrate)? – Ian Thompson Nov 18 '14 at 8:23
• If I am not mistaken, \phantom places the "alternative" text after the spacing created, whereas I would like to put it inside the space. Maybe I am not using it correctly? – Bernd Nov 18 '14 at 8:33

You can just put the short text in a box of zero width and then place the phantom afterwards as follows

\makebox[0pt][l]{short}\phantom{my long text}


\documentclass{article}

\begin{document}

XX\makebox[0pt][l]{short}\phantom{my long text}XX

XXmy long textXX

\end{document}

• Brilliant, thanks a lot. Just wondering: what does the second argument of \makebox, i.e. the [l], specifiy? – Bernd Nov 18 '14 at 8:55
• @Bernd the second argument is l (ell) for left alignment. Without it the text is centered, so in this zero width case sticks out to both sides of the current position, overlapping the previous text, which is not what you asked for. – Andrew Swann Nov 18 '14 at 8:57

With the calc package you can simply say

\makebox[\widthof{my long text}][l]{short}


A personal command seems appropriate:

\documentclass{article}
\usepackage{calc}

\newcommand{\textover}[3][l]{%
% #1 is the alignment, default l
% #2 is the text to be printed
% #3 is the text for setting the width
\makebox[\widthof{#3}][#1]{#2}%
}

\begin{document}

XXmy long textXX

XX\textover{short}{my long text}XX

XX\textover[c]{short}{my long text}XX

XX\textover[r]{short}{my long text}XX

XX\textover[s]{s h o r t}{my long text}XX

\end{document}


if it's just for one use, i wouldn't bother with a definition:

XXmy long textXX

XX\rlap{short}\phantom{my long text}XX


granted, it uses "plain" commands, not latex. it can easily be made into a macro if you really want to, though just for the "simple" case.

• Just as info, this does not work in an item. If you need that use the makebox approach by @Andrew Swann – kap Mar 8 '16 at 22:27