# [texhax] Finding the widest string

José Romildo Malaquias j.romildo at gmail.com
Tue May 4 03:38:16 CEST 2010

Hello.

Given a list of strings, I have to find the widest one.

I have tried to implement a macro for that, but it does not work. Any help is welcome.

\documentclass[12pt,a5paper]{article}

\def\Format#1{{\itshape\tiny #1}}

\def\Widest#1#2(#3){%   dimension (the result)    formatter    list of strings
\def\EndList{xxx}%
\newbox\Box
\newdimen\WidthOther
\def\xWidest##1,{%
\def\temp{##1}%
\ifx\temp\EndList
\else
\setbox\Box{\hbox{#2{##1}}}%
\WidthOther=\wd\Box
\ifdim#1<\WidthOther
#1=\WidthOther
\else
\fi
\expandafter\xWidest
\fi
}%
\xWidest#3,\EndList,
}

\begin{document}

\newdimen\mydim
\Widest{\mydim}{\Format}(Good,morning,world)
\the\mydim

\end{document}

Regards,

Romildo