[texhax] Finding the widest string

Reinhard Kotucha reinhard.kotucha at web.de
Tue May 4 11:39:44 CEST 2010


On 4 May 2010 Uwe Lueck wrote:

 > "José Romildo Malaquias" <j.romildo at gmail.com> wrote at 04.05.2010 03:38:16:
 > > 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}
 > 
 > I think it must be 
 > 
 >     \xWidest#3,xxx,
 > 
 > at the end of your macro. You are presenting the string "\EndList"
 > to end the loop while the loop is designed to end at the string
 > "xxx".

In order to process comma separated lists, the \@for loop defined
in texmf-dist/source/latex/base/ltcntrl.dtx is much easier to use.

% \@for NAME := LIST \do {BODY} : Assumes that LIST expands to A1,A2,
%      ... ,An . 
%      Executes  BODY  n  times, with  NAME = Ai  on the i-th iteration.
%      Optimized for the normal case of n = 1.  Works for n=0.

BTW, it's also quite odd to allocate new box- and dimen-registers each
time the macro is called.

Regards,
  Reinhard

----------------------------------------------------------------------------
Reinhard Kotucha			              Phone: +49-511-3373112
Marschnerstr. 25
D-30167 Hannover	                      mailto:reinhard.kotucha at web.de
----------------------------------------------------------------------------
Microsoft isn't the answer. Microsoft is the question, and the answer is NO.
----------------------------------------------------------------------------



More information about the texhax mailing list