% Removed parts marked by ellipsis dots (mjd) . . . UKTeX Digest Friday, 4 Jan 1991 Volume 91 : Issue 1 Today's Topics: RE: Testing for numeric-only input RE: Help with TeX fonts re: Footnotes without the number metafont bombs out on cmbase.mf . . . ------------------------------------------------------------ Date: Tue, 13 Nov 90 19:35:05 +0000 From: TEX@UK.AC.CRANFIELD.RMCS Subject: RE: Testing for numeric-only input In message 84 of Tue, 13 Nov 90 11:20:22, C20249@UK.AC.POLY-SOUTH-WEST.PRIME-A wrote: > Back in March, I wrote to you asking for help with a TeX/LaTeX > query. I have still not had any acknowledgment or reply. Can you > please help? Here is my query: I'm sorry, but there appears to be no record of your submission to UKTeX ever having arrived. Perhaps it came in when Aston were experiencing some difficulty in forwarding Janet mail to the UKTeX editor, and you missed the ``bounce''. > I am writing a LaTeX "program" and need your help. I am using > \typein to prompt the user to enter a "number" via the terminal. > Then I set a counter to the value of the "number" read in. > > \typein[\mynumber]{Please type a number} > \setcounter{mycounter}{\mynumber} > > What I would like to do is to insert some TeX logic (before > setting the counter) to check the validity of the "number" > entered (it should consist of a sequence of digits 0-9 only) and > to prompt the user again if it contains anything else. > > I have scoured the TeXbook looking for anything that might appear > to be useful or relevant, but I'm not a TeXpert and my search was > fruitless. Having had a slightly similar requirement when writing my crossword.sty option, and eventually being pointed towards \afterassignment by Frank Mittelbach, I can sympathize. I've just knocked together (and tested!) the following style option, which fills the bill, I think. Enjoy!! Brian {Hamilton Kelly} . . . +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ [Start of attached file CHECKNUM.STY] % CHECKNUM.STY % % A style file providing the macro \getnumber. % % Usage: \getnumber{A prompt string}{countername} % or: \getnumber{A prompt string}[default]{countername} % % Outputs the specified prompt and accepts input interactively. % % If the input forms a valid number, it will be assigned to the % LaTeX counter `countername'. Invalid input causes repetition % of the prompt until a valid response is given. % % The second form, with the optional parameter, will assign the % value `default' (which must be a valid number) to countername % if the user types return by itself. % % Caveat: Input commencing with leading zeros will be spuriously % rejected. % % % Author: Brian {Hamilton Kelly} % Address: Royal Military College of Science, % Shrivenham, SWINDON, SN6~8LA, United Kingdom % Tel: ++44 793 785252 % e-mail: % Date: 13th November 1990 % % This macro reads and consumes all following text, up to and % including the special marker \@nil \def\special@gobble #1\@nil{} % This if allows us to decide whether a valid number was entered \newif\ifg@rbage % % Here is the macro to be invoked by the user % \def\getnumber#1{\@ifnextchar[{\g@tnumber#1}{\g@tnumber#1[]}} % % This is the macro that does the work! % \def\g@tnumber#1[#2]#3{% \loop % Doing things this way allows us to get the prompt and the % response onto the same line; the alternative course of using % \typein would have separated these items, and the prompt line % itself would have read as % ``\@tempa=''. \typeout{}% Ensure we start on a newline; % prompt & response then together % If the optional default value is provided, include it in the % prompt \ifx #2 \@empty \message{#1: }% \else \message{#1 [#2]: }% \fi % Read the user's response \read\m@ne to\@tempa % If the user types just a return, \@tempa will be set to `\par '; % convert this into just a space by itself. When we later perform % the assignment to \@tempcnta, the latter will assume the value % zero, so the expansion of \@tempa will NOT equate to that of % \@tempb, and the input of a return by itself will thus be % rejected. (NB \@defpar is defined by LaTeX.TeX) \ifx \@tempa\@defpar \xdef\@tempa{#2\space}\fi % We'll now assign to \@tempcnta whatever part of the beginning of % the content of \@tempa (read from the input) that constitutes a % legal number; any remaining characters (including the terminating % space) will then be discarded by the call of \special@gobble, but % this macro only takes effect AFTER the assignment to the counter % has taken place \afterassignment \special@gobble \@tempcnta=0\@tempa \@nil % We define \@tempb to expand to whatever legal number was input, % with a trailing space. \xdef\@tempb{\the\@tempcnta\space}% % therefore, iff \@tempa contained only digits then its first-level % expansion will be identical to that of \@tempb \ifx\@tempb\@tempa \g@rbagefalse \else % But if the `number' input contained non-digit characters, then % \@tempb will contain something different! \g@rbagetrue \fi % If we didn't get a valid number, then tell the punter; the % \repeat will then return us to the \loop above, and re-prompt for % input. \ifg@rbage \typeout{Your response contained non-digit characters; try again!}% \repeat % Finally, we can set the user-provided LaTeX counter and return \setcounter{#3}{\the\@tempcnta}% } ------------------------------ . . .