## Monday, July 6, 2009

### Centre text within other text in LaTeX

LaTeX function which centres the second argument within the first argument and leaves the correct amount of space at the end.

\newcommand{\centretext}[2]{%
\newlength{\widthA}%
\settowidth{\widthA}{#1}%
%
\newlength{\widthB}%
\settowidth{\widthB}{#2}%
%
\newlength{\moveL}%
\setlength{\moveL}{\widthA}%
\setlength{\moveL}{-0.5\moveL}%
%
\newlength{\moveR}%
\setlength{\moveR}{\moveL}%
\setlength{\moveR}{-1\moveR}%
%
#1\hspace{\moveL}#2\hspace{\moveR}%
}

Usage Example:

$\bigcirc$ bb

\centretext{$\bigcirc$}{{\scriptsize ai}} bb

Note that the text 'ai' is centred within the circle and the text 'bb' at the end of the two lines are aligned with each other.