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}%
\addtolength{\moveL}{-1\widthB}%
\setlength{\moveL}{-0.5\moveL}%
\addtolength{\moveL}{-1\widthB}%
%
\newlength{\moveR}%
\setlength{\moveR}{\moveL}%
\addtolength{\moveR}{\widthB}%
\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.

No comments:

Post a Comment