Showing posts with label LaTeX. Show all posts
Showing posts with label LaTeX. Show all posts

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.

Measure Width of Text in LaTeX

\newlength{\textwidth}
\settowidth{\textwidth}{hello}

Example:
hello\hspace{\textwidth}hell\_

Similarly:
\settodepth

References:
http://www-h.eng.cam.ac.uk/help/tpl/textprocessing/teTeX/latex/latex2e-html/ltx-90.html
http://www-h.eng.cam.ac.uk/help/tpl/textprocessing/teTeX/latex/latex2e-html/ltx-312.html