尝试使用纯 TeX 编写通用对话框提示符

尝试使用纯 TeX 编写通用对话框提示符

只要我将 \answer 固定为某个值,以下操作在 \TeX 中就可以正常工作

我找不到正确的命令来要求控制台输入以获取想要的数字

我希望代码能够使用 \gdef\answer{2} 注释掉并替换为 \askanswer

我想仅使用 TeXbook 内部的命令来修复此问题,无需额外的软件包

我正在使用 TeXnicCenter 和 mikTeX

像 TeXBook p218 中那样在是/否对话框中输入内容会导致类似的错误,我不知道问题出在哪里

为什么编译不会停止并要求输入数字?我明白了!紧急停止

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% An attempt for a general dialog prompt
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% source The TeXBook by Donald E. Knuth p218
%
\global\newcount\choiceno % to count the number of choices
\global\newcount\totalchoices % to count the number of choices
\newif\ifgarbage % to keep track if a valid number has been choosen
                 % \garbagetrue a wrong choice has been made
                 % \gargabefalse a valid choice has been made
\def\space{ }

\def\givefirstArg#1#2{\step{#1}}
\def\step#1{\global\advance\choiceno by1\edef\intermediate{%
\intermediate\space\the\choiceno.\space #1}}
\def\gobble#1{} % used only once to remove the most left space
\def\initialise{\def\intermediate{\gobble}}

\def\givesecondArg#1#2{%
\stepsecond%
\ifgarbage% to check if this is maybe the choice that has been made
   \ifx\answer\intermediatetwo\garbagefalse\immediate\write16{#2} \fi 
\fi
}
\def\stepsecond{\global\advance\choiceno by1 
\edef\intermediatetwo{\the\choiceno}}% 

\def\askanswer{{\endlinechar=-1 \global\read16 to \answer}} 
  % THIS CAUSES ALL THE PROBLEMS
  % AN IMMEDIATE EMERGENCY STOP INSIDE TEXNICCENTER
  % BUT COMPILING INSIDE DOS PROMPT WITH tex filename.tex works!!

\def\beginchoices#1\listchoices#2\endchoices{%
\global\choiceno=0%
\let\poschoice=\givefirstArg%
\initialise%
#2
\edef\menu{\intermediate}
   % \intermediate has the possible choices with 
   % their respective numbers in front of them
\gdef\noofchoices{\the\choiceno}
   % \choiceno has at the end the total number 
   % of choices, stored in counter totalchoices
\xdef\intro{#1 Choose from 1 to\space\noofchoices\space and hit enter}

\global\choiceno=0%
\let\poschoice=\givesecondArg
\garbagetrue%
\loop% this loop is not added yet because the 
% reading in doesn't work!!
\immediate\write16{\intro\menu}
%\gdef\answer{2}
\askanswer% this doesn't work from inside editor, reason unknown, 
          % should work because command listed in TeXBook!!
          % it does work in command prompt with "tex filename.tex"
\global\choiceno=0%
#2%
\ifgarbage\immediate\write16{Invalid entry!!}
% testing and afterwards inside loop repeat
\repeat
%\else
%\immediate\write16{Valid entry!!!}
%\fi
}

% the actual command to test
\beginchoices{Make a choice between the following, 
type the number in front of it}\listchoices%
\poschoice{first}{You have choosen for first}% POSsible choice
\poschoice{second}{You have choosen for second}% POSsible choice
\poschoice{third}{You have choosen for third}\endchoices %POSsible choice

\bye

查阅了以下文章,没有解决问题:

Michael J. Downes 撰写的《继续与 TeX 对话》于 2013 年 1 月重新处理

BaSiX:由 Andrew Marc Greene 用 TeX 编写的解释器

checknum.sty Hamilton Kelly,Brian UKTeX91(1) 1991 年 1 月

在 TeX - LaTeX Stack Exchange 上搜索,很多帖子都是关于 \write16 的,而不是 \read16

TeXBook 作者:Donald E. Knuth p218

答案1

这里是一些用于交互式纯文本例程的代码,用于准备传真标题表。(这最初是在 1994 年编写的,远在 latex2e 被常规使用之前。)我只包含了涉及发出提示和接收响应的部分。

\def\prompt#1#2{\message{#1 }
  \read-1 to #2}

\newcount\hours \newcount\minutes \newcount\thetime
\thetime=\time \hours=\thetime
   \divide\hours by 60 \multiply\hours by 60
   \minutes=\thetime \advance\minutes by -\hours
   \divide\hours by 60
\def\now{\number\hours:\ifnum\minutes<10 0\fi\number\minutes}
\def\today{\ifcase\month\or
  January\or February\or March\or April\or May\or June\or
  July\or August\or September\or October\or November\or December\fi
  \space\number\day, \number\year}

\immediate\write16{  }
\prompt{Date (default=\today): }{\calldate}
\prompt{Time (default=\now): }{\calltime}
\prompt{To whom are you sending the FAX? }{\theirname}
\prompt{Whom do they represent? }{\rep}

相关内容