KTUGFaq

KTUG FAQ

α:
йȣ:
Recent investments will yield a slight profit.
KTSConference/2009Karnes/2006-05&value=TeX���۵�����&value=MacOSXġϱ占쏙옙占쏙옙占쏙옙占쏙옙占 › Conditionals

1 TeX ǹ
1.1 \if
1.2 \ifcat
1.3 \ifx
1.4 Mode ˻(\ifhmode,\ifvmode,\ifmmode)
1.5 Number ˻(\ifnum, \ifodd)
1.6 Dimension ˻(\ifdim)
1.7 Box ˻(\ifvoid, \ifhbox, \ifvbox)
1.8 ˻(\ifeof)
1.9 \iff :)
1.10 Case(\ifcase)
1.11 \newif, \iftrue, \iffalse
2 LaTeX2e ǹ
2.1 LaTeX \newif, \iffalse, \iftrue
2.2 \@if
2.3 \@ifnextchar
2.4 \IfFileExists
2.5 \@ifstar
2.6 \@ifpackageclassloaded, \@ifpackageclasslater, \@ifpackageclasswith
2.7 ifthen Ű
2.7.1 \ifthenelse
2.7.2 \ifthenelse ̴ ˻
2.7.3 \newboolean, \setboolean
3 etoolbox ǹ
3.1 boolean
3.2 ˻
4 Loop
4.1 PlainTeX \loop \repeat
4.2 ifthen Ű \whiledo
4.3 \for-loop
4.4 multido
5 : Ǻ
5.1 TTY Ͽ Է ޾Ƶ̴
5.2 ̿ϴ

[]

1 TeX ǹ


TeX if ǹ ַ ڿ ControlSequences ˻ϱ Ͽ ȴ. .
\if... \fi
\if... \else... \fi
[]

1.1 \if

TeX \if ũε ϴµ, ϳĸ ū Ȯ Ѵ. ׸ ū Ѵ. \if ̿ؼ ũγ Ƽ ̸ װ͵ տ \noexpand ٿ 񱳸 ؾѴ. .
\def\A{abc} \def\B{abc}
\if\A\B
ǹ ̴. ֳϸ, \if ũθ ϴµ, ⼭ \A ϰԵȴ. ׷ \A ϰ \if abc ǰ, ū a, b ̹ ΰ ū Ƿ ̻ ġ ΰ ū Ѵ. \if ab ǹ Ǵµ, 翬 a b ٸǷ ̴.
\def\A{aaa} \def\B{bbb}
\if\A\B
? տ Ѱ ó ϸ \if\A\B \if aa ̹Ƿ ǹ ̴. ٸ ϳ .
\def\pion#1{$\if#1+\pi^+\else\pi^-\fi$}
\if expands the tokens, therefore,
\let\a+
\pion\a
would give the same result as \pion+.

\def\pion#1{$\if#1+\pi^+\else\pi^-\fi$}
\let\a+
\begin{tabular}{lll}
\verb|\pion\a| &  : & \pion\a \\
\verb|\pion+| & : & \pion+
\end{tabular}

[]

1.2 \ifcat

The \if test ignores CategoryCodes; these can be tested by
 \ifcat<token1><token2>
This test is a lot like \if : TEX expands after it until unexpandable tokens remain. For this test ControlSequences are considered to have CategoryCode 16 (ordinarily, category codes are in the range 0–15), which makes them all equal to each other, and different from all character tokens.

[]

1.3 \ifx

\if ϳ. \ifx<token1><token2> token ġϴ ˻Ѵ. ׷ Ȯ ʴ´. (true) Ƿ ϳ Ѵ.
  1. token ƴ . ڵ CategoryCode ų TeX ø̰ų \font, \chardef, \countdef ǵ .
  2. token ̰ \long̳ \outer ° . Ķ͸ . Ȯ¿ .

\let\boxhor=\hbox \ifx\boxhor\hbox %is true
\font\a=cmr10 \font\b=cmr10 \ifx\a\b %is true

\ifx\commandsfile\undefined\else\endinput\fi
\let\commandsfile\null

Ȯ ʴ´ٴ , \if ϰ
\def\pion#1{$\ifx#1+\pi^+\else\pi^-\fi$}
\def\a{+}
\pion\a
, \ifʹ ޸ $\pi^-$ ȴ. , \a + Ȯ ʾǷ \ifx ǹ (false) ǰ, \else\pi^- ̴.

[]

1.4 Mode ˻(\ifhmode,\ifvmode,\ifmmode)

\ifhmode is true if TEX is in HorizontalMode or restricted horizontal mode. • \ifvmode is true if TEX is in VerticalMode or internal vertical mode. • \ifmmode is true if TEX is in MathMode or display math mode.

[]

1.5 Number ˻(\ifnum, \ifodd)

\ifnum<number1><relation><number2>
<relation> $>, =, <$  ϳ̴.(catcode 12)

\ifodd<number>
is true if the <number> is odd.

[]

1.6 Dimension ˻(\ifdim)

\ifdim<dimen>
\ifnum ڰ ƴ ̺ ´.

[]

1.7 Box ˻(\ifvoid, \ifhbox, \ifvbox)

Contents of box registers can be tested with
\ifvoid<8-bit number>
which is true if the register contains no box,

\ifhbox<8-bit number>
which is true if the register contains a horizontal box, and

\ifvbox<8-bit number>
which is true if the register contains a vertical box.

[]

1.8 ˻(\ifeof)

Է EOF ¸ ˻Ѵ. \ifeof<number> · ̸, <number> 0-15̸ false̴. ̿Ͽ ϴ ˻ ִ.
\newread\instream \openin\instream= fname.tex
\ifeof\instream \message{File fname does not exist!}
\else \closein\instream \input fname.tex
\fi

[]

1.9 \iff :)

$a\iff b$
$$a\iff b$$

[]

1.10 Case(\ifcase)

\ifcase<number><case0>
\or ...
\or<casen>
\else<other cases>
\fi

\ifcase ̴.
\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}

[]

1.11 \newif, \iftrue, \iffalse

\newif ο Ѵ.
\newif\iffoo
ϳ TeX ControlSequences Ѵ. , \footrue, \foofalse, \iffoo ϴ ̴.

\def\footrue{\let\iffoo=\iftrue}
\def\foofalse{\let\iffoo=\iffalse}
\foofalse

\iffoo
 <iftrue_commands>
\else
 <iffalse_commands>
\fi

[]

2 LaTeX2e ǹ

LaTeX2e ǹ TeX ʹ ޸ 밳 ڸ Ѵ.
\@ifXXX{condition}{iftrue_execution}{iffalse_execution}
̴. Ư ifthen Ű ̷ Ӱ Ȱ ֵ Ѵ.

Ű \if ߰ ִ 찡 . FancyHdr Ű \iffloatpage ̴. See FancyHdr.

[]

2.1 LaTeX \newif, \iffalse, \iftrue

article.cls titlepage .
\newif\if@titlepage
\@titlepagefalse
\DeclareOption{titlepage}{\@titlepagetrue}
\ProcessOptions

\if@titlepage
....
\else
....
\fi

̿ ٸ \if@ ִ. .
\newif\if@pdfmake
\@pdfmaketrue
ϰ Ǹ, \@pdfmaketrue \@pdfmakefalse ְ ȴ. ķδ
\if@pdfmake
...
\else
...
\fi
TeX ִ.

׷ ̷ TeX ŷο ƴ϶ syntax ų ɼ ʹ Ƽ, LaTeX2e ϰ óϴ ifthen Ű ̿ϴ . Ʒ ifthen Ű κ .

[]

2.2 \@if

LaTeX \newif \@if ǵǾ ִ.
\def\newif#1{%
  \count@\escapechar \escapechar\m@ne
    \let#1\iffalse
    \@if#1\iftrue
    \@if#1\iffalse
  \escapechar\count@}
\def\@if#1#2{%
  \expandafter\def\csname\expandafter\@gobbletwo\string#1%
                    \expandafter\@gobbletwo\string#2\endcsname
                       {\let#1#2}}

[]

2.3 \@ifnextchar

LaTeX ǹ ϳ̴. ־ ɿ شϴ κ ڸ о ˻Ѵ. \@ifnextchar ȣ κ̴.
\renewcommand\sqrt{\@ifnextchar[\@sqrt\sqrtsign}
\def\@sqrt[#1]{\root #1\of}

\sqrt[3] \@ifnextchar[ (true) ǵǷ \@sqrt[3] θ. ׷ \sqrt\@ifnextchar[ (false)̹Ƿ \sqrtsign ȣϰ ȴ.

[]

2.4 \IfFileExists

ڸ . ù° ̸̰, then else شϴ ó ɵ̴. ϸ ι° ڰ, ° ڰ ȴ. ϸ ҷ̵ ϴ \InputIfFileExists ִ. ڸ , ϸ ҷ , ι° ڷ ־ ϰ, ׷ ° ڷ ־ Ѵ.
\newcommand{\printglossary}{%
  \InputIfFileExists{\jobname.gld}{}{%
  \typeout{No file exists! \jobname.gld}}%
}

[]

2.5 \@ifstar

ǥ (\table*, \section* ) óϱ ׽Ʈ.
\def\@ifstar#1{\@ifnextchar *{\@firstoftwo{#1} }}

[]

2.6 \@ifpackageclassloaded, \@ifpackageclasslater, \@ifpackageclasswith

\@ifpackageloaded
To find out if a package has already been loaded.
\@ifpackageloaded{package_name}{then_commands}{else_commands}

\@ifpackagelater
To find out if a package has already been loaded with a version more recent than _version_.
\@ifpackagelater{package_name}{_version_}{then_commands}{else_commands}

\@ifpackagewith
To find out if a package has already been loaded with at least the options _options_.
\@ifpackagewith{package_name}{_options_}{then_commands}{else_commands}

[]

2.7 ifthen Ű

ifthen Ű David Carlisle , ǹ ó ϰ ִ Ѵ.
[]

2.7.1 \ifthenelse

\ifthenelse{<test>}{<then clause>}{<else clause>}
<test> boolean Լ ˻Ͽ, ̸ <then clause>, ̸ <else clause> Ѵ.

calc Ű Բ Ἥ am-pm ٲٴ ۼغ.
<!> calc Ű ؼ TeX .
\usepackage{ifthen,calc} %% preamble

\newcommand{\printtime}{%
  \setcounter{hours}{\time / 60}%
  \setcounter{minutes}{\time - \value{hours} * 60}%
  \ifthenelse{\value{hours}>12}%
   {\setcounter{hours}{\value{hours} - 12}%
    \thehours:\theminutes\,pm\ \today}%
   {\thehours:\theminutes\,am\ \today}
}

[]

2.7.2 \ifthenelse ̴ ˻

\ifthenelse ù° ڿ ˻縦 ؼ ִ.
    • \and
    • \or
    • \not
    • ȣ \( \)
    • number $$<$$ number
    • number $$=$$ number
    • number $$>$$ number
    • \isodd{number}
  1. commands
    • \isundefined{command name}
    • \equal{string}{string}
  2. ̺
    • \lengthtest{dimen $$<$$ dimen}
    • \lengthtest{dimen $$=$$ dimen}
    • \lengthtest{dimen $$>$$ dimen}
  3. boolean ˻
    • \boolean{name}

\equal ˻ϴ <string> token Ȯ ɾ̴. Ȯ (equal)ϸ (true) ȴ. \isodd <number> Ȧ̸ ȴ. ڰ ڰ ƴϰų Ȧ ƴϸ (false)̴. \isundefined{\cmd} \cmd ǵǾ ̴. \boolean{xyz} TeX \if ǵ \ifxyz \iftrue (true) ǵش.
xyz \newboolean Ǵ \provideboolean Ͽ ǵ ִ. ׸, TeX \newif Ͽ ǵ ū ؼ ִ. , \boolean{true} \iftrue Ű ̴. , \boolean{mmode} \ifmmode .

[]

2.7.3 \newboolean, \setboolean

\usepackage{ifthen}  %% preamble

\newboolean{BoolVar}
\setboolean{BoolVar}{true}
\ifthenelse{\boolean{BoolVar}}{1}{2}

̿Ͽ, ռ \@pdfmake ߴ Ŀ غ. , @pdfmake Ҹ true ҴѴ.
\newboolean{@pdfmake}
\setboolean{@pdfmake}{true}
\ifthenelse ǹ .
\ifthenelse{\boolean{@pdfmake}}%
 {.....} % if true
 {.....} % if false

[]

3 etoolbox ǹ

etoolbox e-TeX primitive ̿ϴ پ Ѵ. ˻ ؼ ʿ ִٽ ̹Ƿ ڼ etoolbox . ⼭ boolean õ Ͱ ǹ ξ ҰѴ. ǿ LaTeX ǹ .
[]

3.1 boolean

  • \newbool{name}
  • \providebool{name}
  • \setbool{name}{value} : value true false.
  • \booltrue{name}
  • \boolfalse{name}
  • \ifbool{name}{true}{false}
  • \notbool{name}
[]

3.2 ˻

control sequences
  • \ifdef{controlsequence}{true}{false}, \ifcsdef{name}{true}{false}
  • \ifundef{controlsequence}{true}{false}, \ifcsundef{name}{true}{false}
  • \ifdefequal{controlsequence}{controlsequence}{true}{false}, \ifcsequal{name}{name}{true}{false}
string
  • \ifstrequal{string}{string}{true}{false}
  • \ifstrempty{string}{true}{false}
  • \ifblank{string}{true}{false}
arithmetic
  • \ifnumcomp{integer}{relation}{integer}{true}{false} : integer e-TeX number expression .
  • \ifnumequal{integer}{integer}{true}{false}
  • \ifnumgreater, \ifnumless
  • \ifnum.... ɿ ϴ \ifdim... ɵ .
  • \ifnumodd

[]

4 Loop

[]

4.1 PlainTeX \loop \repeat

\newcount\MyCount
\MyCount=0
\newcount\Summation
\Summation=0
\loop
 \message{\number\MyCount}
 \advance\MyCount by 1
 \advance\Summation by \MyCount
  \hbox{MyCounter Number: \number\MyCount\quad Summatioin: \number\Summation}
\ifnum\MyCount<10\repeat
\bye

[]

4.2 ifthen Ű \whiledo

\whiledo{<test>}{<while clause>}
<test> ǰ˻ Ѵ. true <while clause> ȴ.

1 10 Ͽ غ.
\documentclass{article}
\usepackage{ifthen,calc}
\usepackage{hfont}

\begin{document}

\newcounter{myloopcnt}
\setcounter{myloopcnt}{0}
\newcounter{summation}
\setcounter{summation}{0}

\noindent\textbf{1 10 غ.}

\noindent\whiledo{%
        \value{myloopcnt} < 10}{%
        \stepcounter{myloopcnt}%
        \setcounter{summation}{\thesummation + \themyloopcnt}%
        \themyloopcnt°  : \qquad  հ \thesummation\\}
\end{document}

[]

4.3 \for-loop

\makeatletter
\def\for#1#2#3{\@ifnextchar[{\@for@{#1}{#2}{#3}}{\@for@{#1}{#2}{#3}[1]}}
\long\def\@for@#1#2#3[#4]#5{\setcounter{#1}{#3}\addtocounter{#1}{1}%
        \edef\for@loopend{\arabic{#1}}%
        \setcounter{#1}{#2}%
        \loop
        \ifnum \expandafter\the\csname c@#1\endcsname < \for@loopend\relax
        #5\addtocounter{#1}{#4}\repeat}
\newcounter{repeatcnt}
\makeatother

\for Ἥ غ. ⼭ ifthen Ű ʿϴ.
\documentclass{article}
\usepackage{calc}
\usepackage{hfont}

\makeatletter
\def\for#1#2#3{\@ifnextchar[{\@for@{#1}{#2}{#3}}{\@for@{#1}{#2}{#3}[1]}}
\long\def\@for@#1#2#3[#4]#5{\setcounter{#1}{#3}\addtocounter{#1}{1}%
        \edef\for@loopend{\arabic{#1}}%
        \setcounter{#1}{#2}%
        \loop
        \ifnum \expandafter\the\csname c@#1\endcsname < \for@loopend\relax
        #5\addtocounter{#1}{#4}\repeat}
\newcounter{repeatcnt}
\makeatother

\begin{document}

\newcounter{summation}
\setcounter{summation}{0}

\noindent\textbf{1 10 غ.}

\noindent
\for{repeatcnt}{1}{10}%
        {\setcounter{summation}{\thesummation + \therepeatcnt}%
         \therepeatcnt°  : \qquad  հ \thesummation\\}

\end{document}

[]

4.4 multido

Timothy van Zandt ۼ ũ̴. multido.tex multido.sty ִ.
PSTricks ũδ Ÿ ڵ ҷδ.
\multido{_variables_}{_repetitions_}{_stuff_}
_variables_ δ.
variable = initial value + increment
variable ִ Dimension, Number, Integer, Real̰ \d, \n, \i, \r Ѵ. .
\multido{}{10}{\TeX\ }

\multido{\d=2pt+3pt}{5}{\d, }

\multido{\n=2+3}{10}{\n, }

\multido{\i=2+-3}{10}{\i, }

\multido{\r=2+3.05}{6}{\r, }

  • \multido nesting ϴ.
  • ī͸ 1 Ű \mmultido ɵ ִ.
  • \multidostop multido ݺ ġ Ѵ.

[]

5 : Ǻ

[]

5.1 TTY Ͽ Է ޾Ƶ̴

main.tex õϸ ȭ鿡 "Yes/No" , y Ǵ n ϴ ٸ ִ ҽ ۼغ. ѱ ͹̳ Ƿ ȭ ϴ Ѵ.

\documentclass{article}
\usepackage{hfont}
\usepackage{ifthen}

⿡ Preamble Ѵ. ttyin, ttyout ϰ \Msg ɰ \Ask Ѵ.
\makeatletter
\chardef\ttyin16
\chardef\ttyout16
\def\iden#1{#1}
\def\strip#1#2 \@gobble{\def #1{#2}}
\def\@defpar{\par}
\def\yes{yes}
\def\y{y}
\def\n{n}

\def\Msg{\immediate\write\ttyout}
\def\Ask#1#2{%
    \message{#2}\read\ttyin to #1\ifx#1\@defpar\def#1{}\else
    \iden{\expandafter\strip
       \expandafter#1#1\@gobble\@gobble} \@gobble\fi}
\makeatother

̽ YesOrNo boolean (̰ ifthen Ű ʿϴ.) Ѵ. ⺻ true.
\newboolean{YesOrNo}
\setboolean{YesOrNo}{true}

.
\begin{document}

ȭ鿡 "Please Answer Me, Yes or No(y/n)?"̶ ޽ ְ Է ޾Ƶδ.
\Msg{**************************************************^^J%
     * Please Answer Me, Yes or No(y/n)?}%
\Ask\answer{%
     **************************************************}%

y ԷµǸ YesOrNo true, ׹ 쿡 false
\ifx\y\answer\setboolean{YesOrNo}{true}
\else\setboolean{YesOrNo}{false}\fi

·ƾ. YesOrNo true false ٸ ޽ DVI Ѵ. ifthen Ű \ifthenelse .
\ifthenelse{\boolean{YesOrNo}}%
        {`' ϼ̽ϴ.}%
        {`' ϼ̽ϴ.}

.
\end{document}

[]

5.2 ̿ϴ

࿡ Ѳ ϴ ?  غ.

ۼѴ.
\documentclass{article}
\usepackage{hfont}
\usepackage{ifthen}

\makeatletter
\chardef\ttyin16
\chardef\ttyout16
\def\iden#1{#1}
\def\strip#1#2 \@gobble{\def #1{#2}}
\def\@defpar{\par}
\def\Ask#1#2{%
    \message{#2}\read\ttyin to #1\ifx#1\@defpar\def#1{}\else
    \iden{\expandafter\strip
       \expandafter#1#1\@gobble\@gobble} \@gobble\fi}
\def\Msg{\immediate\write\ttyout}
\def\yes{yes}
\def\y{y}
\def\n{n}
\makeatother

\newboolean{YesOrNo}
\setboolean{YesOrNo}{true}

\def\getYesOrNo{%
        \Msg{**************************************************^^J%
         * Please Answer Me, Yes or No(y/n)?}%
        \Ask\answer{%
         **************************************************}%
}

\newcommand\DetermineYesOrNo{%
%%  \answer ̹ ־ ʴٸ
\ifx\answer\undefined
        \getYesOrNo
\else\fi
%% \answer  YesOrNo Ҹ 
\ifx\y\answer\setboolean{YesOrNo}{true}
        \else\setboolean{YesOrNo}{false}\fi
}

%%% ¹ڿ
\newcommand\YesMessage{`' ϼ̽ϴ.}
\newcommand\NoMessage{`' ϼ̽ϴ.}

\begin{document}
\DetermineYesOrNo
\ifthenelse{\boolean{YesOrNo}}%
        {\YesMessage}%
        {\NoMessage}

\end{document}

࿡ óѴ.(̸ test.tex)
#> latex \def\answer{y}\input{test}

YesOrNo no ϰ
#> latex \def\answer{n}\input{test}

ܼϰ latex test Ѵٸ ռ ȭ (y/n) ȭ鿡  ´. ̰ ġ Ǵ ũƮ ̴.


պκ Victor Eijkhout, TeX By Topic(http://www.eijkhout.net/tbt/), pp.112-117. ַ , οϿϴ.
DeleteMe ? --Karnes My name is approximately pronounced "ike-howt". -- Victor Eijkhout HJhan

DeleteMe \ifXXX Ǵ ڼ ãҽϴ. ;) Ե մϴ. --2004-03-28hermian

DeleteMe ۼֽɿ 帳ϴ. LaTeX ǻ͵ ˾ƾϰ, е, Ʈ ˾ƾϰ, ˸ ... ׷ ᱹ Conditionals ˾ƾ ȿ ۼ ϴ. --Progress
ϴ. ;) ̵ Īޱ ִϴ. ߰ŵ. :) Ǹ ڽϴ. --Karnes
׳ ڶ± . ߰پ ֽ Karnes մϴ. бԴϴ. :) ϰڽϴ. ~ٹ --hermian

Contributors: Karnes


^
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2011-07-05 18:40:17
Processing time 0.1600 sec