# Writing a word with the length of another word

Ive got a quick question: How can I create text that has the length (or all the dimensions) of another string? What I mean is:

Have latex write TEXT, but let it be printed as having the dimensions of LONGERTEXT.

EDIT: After bad wording on my side, I should clarify what I meant:

This gives text normally.
This gives text       normally.
This gives longertext normally.


I would like to have the second line, with correct spacing by latex, not by hand.

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.

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?


• Thanks, but not quite what i wanted. I would like latex to write "text" give it the (horizontal) length of "longertext", so that the two words will take up the same space inside a line. Might have worded that badly, sorry. Nov 17, 2015 at 18:54
• @MichaelA that's alright, I had to eat dinner, but I was going to add to this answer anyway Nov 17, 2015 at 18:55
• Still not quite what I meant. I updated my question. Probably should have made that example from the beginning, sorry about that. Nov 17, 2015 at 19:57

Perhaps this is more what you're looking for:

\documentclass{article}

\newlength\aetmplength
\newcommand\matchlength[3][c]{%%
\settowidth\aetmplength{#3}%%
\makebox[\aetmplength][#1]{#2}}

\setlength{\parindent}{0pt}
\pagestyle{empty}
\begin{document}

This gives text normally.       \newline
This gives \matchlength{text}{longertext}       normally. \newline
This gives longertext normally.

\vspace{0.5in}

This gives text normally.       \newline
This gives \matchlength[l]{text}{longertext}       normally. \newline
This gives longertext normally.

\vspace{0.5in}

This gives text normally.       \newline
This gives \matchlength[r]{text}{longertext}       normally. \newline
This gives longertext normally.

\end{document}


The control sequence \matchlength takes three arguments. The first is optional and allows you to select how the text is to appear within the space it's being set with in. The second argument is the string itself. And, the third argument is the text which determines the space you want to use.

• I was just about to offer this as my bonus bit of fun - +1 for the more general solution Nov 17, 2015 at 20:21

Quite simple with calc:

\documentclass{article}
\usepackage{calc}

\newcommand\matchlength[3][c]{\makebox[\widthof{#3}][#1]{#2}}

\begin{document}

This gives text normally.

This gives \matchlength{text}{longertext} normally.

This gives longertext normally.

\bigskip

This gives text normally.

This gives \matchlength[l]{text}{longertext} normally.

This gives longertext normally.

\bigskip

This gives text normally.

This gives \matchlength[r]{text}{longertext} normally.

This gives longertext normally.

\end{document}