41

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...

2
  • 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)? Commented Nov 18, 2014 at 8:23
  • 2
    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
    Commented Nov 18, 2014 at 8:33

3 Answers 3

37

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}

Sample output

\documentclass{article}

\begin{document}

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

XXmy long textXX

\end{document}
4
  • 1
    Brilliant, thanks a lot. Just wondering: what does the second argument of \makebox, i.e. the [l], specifiy?
    – Bernd
    Commented Nov 18, 2014 at 8:55
  • 2
    @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. Commented Nov 18, 2014 at 8:57
  • Which is a problem in some contexts like table cells. The solution using calc works better there.
    – stefanct
    Commented May 5, 2021 at 23:59
  • 4
    This version works in math mode if \makebox is replaced with \mathmakebox from the package mathtools. Commented May 19, 2022 at 10:24
33

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}

enter image description here

2
  • What would be the math mode version of it ? Commented May 19, 2022 at 10:25
  • @SphericalTriangle with packages mathtools and calc, \newcommand{\mathtextover}[3][l]{\mathmakebox[\widthof{\(#3\)}][#1]{#2}} would be the math mode equivalent. Commented Jul 1, 2022 at 6:59
12

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

output of example code

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.

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .