[Tex/LaTex] Make box with minimum width (in math mode)

boxesvarwidthwidth

How can I give a box a minimal width, in math mode? That is, I would like a command something like \minwidth{exp}{width}, which will always typeset exp, but additionally, if the natural length of exp is less than width, will pad it by centering it in a box of width width.

For instance, $A \minwidth{i}{2em} B$ should come out like $A \makebox[2em][c]{i} B$; but $A \minwidth{WXYZ}{2em} B$ should come out like $A WXYZ B$.

The varwidth package provides this variable-width functionality, but only for a minipage, not for a plain box or similar.

(The use case is for a variant of the inline TikZ arrow commands defined in this answer, padding the arrow label to a minimal width, so that the arrow extends for large labels, but does not get too short when the label are short or absent.)

Edit: This turned out to be a bit of an XY problem: Harish Kumar has excellently answered my real problem, without answering this specific question. What I am now using, adapted from his answer, is:

\newcommand{\myto}[1][]{ \mathrel{
  \tikz[baseline={([yshift=-0.55ex]a.south)}]{%
    \node[minimum width=1.5em,align=center,inner xsep=0.3ex,inner ysep=0.15ex] (a) {$\scriptstyle #1$};
    \draw[->] (a.south west) -- ([xshift=0.6ex]a.south east);}
  }\mkern-1mu}

with usage as in $A \myto[f] B$.

I would still be interested in answers to the specific question asked, though.

Best Answer

Here is a tikz only approach. Adjust exs suitably.

\documentclass{article}

\usepackage{tikz,mathtools}

\usetikzlibrary{arrows}

\newcommand*{\ident}[1]{\texttt{\small #1}}

\tikzstyle{refines} = [->, >=open triangle 45]

\newcommand{\refi}[3]{%
  \( #1 \)
  \tikz[baseline={([yshift=-0.5ex]a.south)}]{%
  \node[minimum width=2em,align=center] (a) {$#2$};     %%<<---- width here
  \draw[refines] (a.south west) --  ([xshift=1ex]a.south east);}   %% <<---- shift here
  \(#3 \)}

\begin{document}

This is a test \refi{\ident{1}}{\text{t}}{\ident{2}}\ident{2} that continues here.

This is a test \refi{1}{\text{longer text}}{1} that continues here.

A further test \refi{j_2}{\alpha-x e^{p/q}}{a^2} with more text.

\end{document}

enter image description here

The magic word here is minimum width=2em option for the node. I have also used some xshift for the arrow head to make it more symmetric. Adjust these values as you need.