26

I am looking for a command like \makebox{text to set width}[c]{new text} which would make a box with the width of text to set width containing ____new text____ centered.

0

5 Answers 5

33

Something like this?

\documentclass{article}
\newlength\stextwidth
\newcommand\makesamewidth[3][c]{%
  \settowidth{\stextwidth}{#2}%
  \makebox[\stextwidth][#1]{#3}%
}
\begin{document}
  \fbox{\makesamewidth[c]{text to set width}{new text}}
\end{document}

matched width

If the order of arguments is important you can use xparse:

\documentclass{article}
\usepackage{xparse}
\newlength\stextwidth
\NewDocumentCommand\makesamewidth{ m O{c} m }{%
  \settowidth{\stextwidth}{#1}%
  \makebox[\stextwidth][#2]{#3}%
}
\begin{document}
  \fbox{\makesamewidth{text to set width}[c]{new text}}
\end{document}

using \NewDocumentCommand rather than \newcommand.

21

calc provides \widthof{<stuff>}:

enter image description here

\documentclass{article}
\usepackage{calc}
\setlength{\parindent}{0pt}% Just for this example
\begin{document}
Here is some text that is lengthy.\par
\makebox[\widthof{Here is some text that is lengthy}][l]{Left}\par
\makebox[\widthof{Here is some text that is lengthy}][c]{Centre}\par
\makebox[\widthof{Here is some text that is lengthy}][r]{Right}\par
\makebox[\widthof{Here is some text that is lengthy}][s]{S p a c e d}
\end{document}

And another awkward way of achieving your goal using a tabular:

enter image description here

\documentclass{article}
\setlength{\parindent}{0pt}% Just for this example
\makeatletter
\newcommand{\widthbox}[1]{\gdef\stext{#1}\widthbox@}
\newcommand{\widthbox@}[2][c]{%
  \begin{tabular}{@{}#1@{}}
    \phantom{\stext} \\[-\normalbaselineskip]
    #2
  \end{tabular}}
\begin{document}
Here is some text that is lengthy.\par
\widthbox{Here is some text that is lengthy}[l]{Left}\par
\widthbox{Here is some text that is lengthy}[c]{Centre}\par
\widthbox{Here is some text that is lengthy}[r]{Right}
\end{document}
13

Define a new length \mylen and then set it to the width of text.

\documentclass{article}
\newlength{\mylen}
\settowidth{\mylen}{text to set width}
\begin{document}
  text to set width

  \makebox[\mylen][c]{new text}
\end{document}

enter image description here

8

One way using directly \wd:

\documentclass{article}
\newcommand\mybox[3][c]{\setbox0\hbox{#2}\fbox{\makebox[\the\wd0][#1]{#3}}}
\begin{document}
\obeylines
\mybox[c]{text to set width}{new text}
\mybox[l]{text to set width}{new text}
\mybox[r]{text to set width}{new text}
\mybox[s]{text to set width}{new text}
\mybox{text to set width}{new text}     
\end{document}

mwe

1
  • Note: obviously, one should remove the \fbox{ and an extra } to get rid of the frame (and its inner sep).
    – ebosi
    Sep 20, 2017 at 14:49
6

The simplest is perhaps to use the eqparbox package: it defines \eqparbox, eqmakebox, \eqframebox, \eqsavebox commands that have the same arguments as their basic LaTeX counterparts, except the width argument is replaced with a tag. All boxes with the same tag have the same width as the widest of them. In addition there is an \eqboxwidth{tag} length which may be used as a length argument.

For simple needs, the small makebox package defines a \makebox*{longer reference text}{shorter text} command.

    \documentclass[12pt]{article}
    \usepackage[utf8]{inputenc}
    \usepackage{lipsum}
    \usepackage{makebox}
    \usepackage{eqparbox}
    \setlength\fboxsep{12pt}

    \begin{document}

     \begin{tabular}{c}
            \fbox{\makebox*{Blahblahblah, blahblahblah, blahblahblah…}{Blahblahblahblah}}\\\\
            \fbox{\eqparbox{boxa}{Blahblahblah, blahblahblah, blahblahblah…}}\\\\
            \eqframebox[boxa]{Blahblahblahblah}\\\\
            \eqframebox[boxa]{Blahblahblah, blahblahblah, blahblahblah…}\\\\
            \fbox{\parbox{\eqboxwidth{boxa}}{\lipsum[2]}}
     \end{tabular}

    \end{document} 

enter image description here

You must log in to answer this question.

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