问一下 \newif 这个命令的用法

428 views
Skip to first unread message

Lingtao Pan

unread,
Nov 15, 2007, 3:49:15 AM11/15/07
to thut...@googlegroups.com
Hi, all:

发现 texbook 这本书还是不容易看懂的。下面这一段我看了好几篇还是没弄清楚 \newif 的用法。

To facilitate \if... constructions, plain TEX has a \newif
macro, such that
after you say '\newif\ifabc' three control sequences will be
defined: \ifabc
(for testing the switch), \abctrue (for making the switch true), and
\abcfalse (for
making it false). The \phantom problem is now solved in Appendix B by writing

\newif\ifhph \newif\ifvph
\def\hphantom{\hphtrue\vphfalse\phant}

and with similar definitions of \vphantom and \phantom. There is no
longer any need
for a \ph macro; again \phant tests \ifhph and \ifvph. Appendix E contains other
examples of conditionals created by \newif. New conditionals are
initially false.

另外,ThuThesis 中是这样使用 \newif 的 (当然我也没看懂)。

\newif\ifthu@bachelor\thu@bachelorfalse
\DeclareOption{bachelor}{\thu@bachelortrue}
\ifthu@bachelor\relax\else


Best regards.

Ruini Xue

unread,
Nov 15, 2007, 6:02:04 AM11/15/07
to thut...@googlegroups.com
On Nov 15, 2007 4:49 PM, Lingtao Pan <free...@gmail.com> wrote:
Hi, all:

发现 texbook 这本书还是不容易看懂的。下面这一段我看了好几篇还是没弄清楚 \newif 的用法。

         To facilitate \if... constructions, plain TEX has a \newif
macro, such that
         after you say '\newif\ifabc' three control sequences will be
defined: \ifabc
(for testing the switch), \abctrue (for making the switch true), and
\abcfalse (for
making it false).

用c++对比可以这样理解:

\newif\ifabc            bool abc;
\abctrue                abc = true;
\abcfalse               abc = false;
\ifabc                    if (abc) {
  abc is true             cout << "abc is true";
\else                     } else {
  abc is false            cout << "abc is false";
\fi                         }
 
也就是说\newif\ifabc看起来定义一个宏,实际上它定义了三个宏:\ifabc, \abctrue, \abcfalse。
\ifabc用来判断abc是否为真,而后两个分别用来设置abc的值为真或假.

The \phantom problem is now solved in Appendix B by writing

         \newif\ifhph \newif\ifvph
         \def\hphantom{\hphtrue\vphfalse\phant}

and with similar definitions of \vphantom and \phantom. There is no
longer any need
for a \ph macro; again \phant tests \ifhph and \ifvph. Appendix E contains other
examples of conditionals created by \newif. New conditionals are
initially false.

另外,ThuThesis 中是这样使用 \newif 的 (当然我也没看懂)。

    \newif\ifthu@bachelor\thu@bachelorfalse

这跟上面一样,定义一个"布尔变量"thu@bachelor,并给它赋初值为false
 
    \DeclareOption{bachelor}{\thu@bachelortrue}

\DeclareOption是LaTeX的命令,表示如果文档类有bachelor选项,那么执行后面的命令,即将thu@bachelor设置为真。
 

      \ifthu@bachelor\relax\else
 
翻译成c++就是
if (thu@bachelor == true) {
     // do nothing. \relax means 'do nothing' in TeX
     // 当然这里的\relax可以忽略,TeX会将\ifXXX和\else之间的东西作为条件判断为真时执行的部分。
} else {
   ....
}


Best

Ruini





Best regards.

Lingtao Pan

unread,
Nov 15, 2007, 9:06:34 AM11/15/07
to thut...@googlegroups.com
在 07-11-15,Ruini Xue<xuer...@gmail.com> 写道:

>
>
> On Nov 15, 2007 4:49 PM, Lingtao Pan <free...@gmail.com> wrote:
> > Hi, all:
> >
> > 发现 texbook 这本书还是不容易看懂的。下面这一段我看了好几篇还是没弄清楚 \newif 的用法。
> >
> > To facilitate \if... constructions, plain TEX has a \newif
> > macro, such that
> > after you say '\newif\ifabc' three control sequences will be
> > defined: \ifabc
> > (for testing the switch), \abctrue (for making the switch true), and
> > \abcfalse (for
> > making it false).
>
> 用c++对比可以这样理解:
>
> \newif\ifabc bool abc;
> \abctrue abc = true;
> \abcfalse abc = false;
> \ifabc if (abc) {
> abc is true cout << "abc is true";
> \else } else {
> abc is false cout << "abc is false";
> \fi }
>
> 也就是说\newif\ifabc看起来定义一个宏,实际上它定义了三个宏:\ifabc, \abctrue,
> \abcfalse。
> \ifabc用来判断abc是否为真,而后两个分别用来设置abc的值为真或假.
>

多谢, 解释地很清楚,我明白了。

我原来主要是没理解这句话:
three control sequences will be defined.

moz

unread,
Jul 15, 2012, 2:47:09 AM7/15/12
to thut...@googlegroups.com
谢谢 4年后还教会了我 ^_^

在 2007年11月15日星期四UTC+8下午4时49分15秒,Lingtao Pan写道:
Reply all
Reply to author
Forward
0 new messages