Yeah, you can do this, using the graphics
or graphicx
package.
\newlength{\longword}
\settowidth{\longword}{\textbf{LONGERTEXT}}
\resizebox{\longword}{!}{\textbf{TEXT}}
So what I've done here is set up a new length \longword
, which I can then give a value. This is what I've done here:
\settowidth{\longword}{\textbf{LONGERTEXT}}
This assigns the width of the second argument (here \textbf{LONGERTEXT}
) to the length command given in the first argument (here \longword
). So I've assigned the width of \textbf{LONGERTEXT}
to be the length of my length \longword
.
Now the \resizebox
command provided by the graphics
package allows me to scale the text in the third and final argument to the width and height specified in the first and second arguments. So you can see that, what I've done, is scaled the width of \textbf{TEXT}
to the length of \longword
. And, of course, I have just set the length of \longword
as the width of \textbf{LONGERTEXT}
. Thus I have scaled the width of \textbf{TEXT}
to the width of \textbf{LONGERTEXT}
. Naturally, I can change the width of \longword
as I need to, which is one of the advantages of defining a variable.
An !
in either argument of \resizebox
maintains the aspect ratio of the text.
Thus:
\documentclass[12pt]{article}
\pagestyle{plain}
\usepackage[margin=1.8cm]{geometry}
\geometry{a4paper}
\usepackage[parfill]{parskip}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{graphicx}
\begin{document}
\newlength{\longword}
\settowidth{\longword}{\textbf{LONGERTEXT}}
\resizebox{\longword}{!}{\textbf{TEXT}} \\
\textbf{LONGERTEXT} \\
\textbf{TEXT}
\end{document}
Gives

In general, I would really recommend preserving the aspect ratio. If, however, you want to keep the height the same, you can do this by using \totalheight
as the second argument of the \resizebox
command thus:
\resizebox{\longword}{\totalheight}{\textbf{TEXT}}

This is not so nice.
Added for the edit:
Well that makes it even easier, just use a \makebox
\newlength{\longword}
\settowidth{\longword}{longertext}
This gives text normally. \\
This gives \makebox[\longword][l]{text} normally. \\
This gives longertext normally.

The first optional argument of \makebox
sets the width and the second (here l
) sets the alignment, l
moves the contents of the box to the left (the default is for the contents to be centred).
I was about to offer a fun additional bit of code, for a more general solution, but I was beaten to it by A. Ellett who deserves the tick. But I thought I might as well add what I've done to give a bit of explanation:
We can define a command which allows us to set the space taken up by one word to be the width of another, like so:
\newlength{\firstword}
\newcommand{\stretchy}[2]{%
\settowidth{\firstword}{#1}%
\makebox[\firstword][l]{#2}%
}
So what I've done here is define the command \stretchy
which has two arguments. The length \firstword
is given the width of the first argument by:
\settowidth{\firstword}{#1}%
A box is then made which has the width of the first word, with the second word as its contents:
\makebox[\firstword][l]{#2}%
A box is treated by LaTeX as one large letter of a predetermined width, which means it leaves only as much space as the width of the box. What this means is that if you set the width of a box to be shorter than the width of its contents, the contents will overflow. Thus:
\newlength{\firstword}
\newcommand{\stretchy}[2]{%
\settowidth{\firstword}{#1}%
\makebox[\firstword][l]{#2}%
}
This gives text normally. \\
This gives \stretchy{longertext}{text} normally. \\
This gives longertext normally.
This gives a long gap -- see? \\
This gives \stretchy{Floccinaucinihilipilification}{a long gap} --
see? \\
This gives Floccinaucinihilipilification -- see?
This gives Floccinaucinihilipilification -- see? \\
This gives \stretchy{not enough gap}{Floccinaucinihilipilification} --
see? \\
This gives not enough gap -- see?
