12.07.2015 Views

The memoir class - The UK TeX Archive

The memoir class - The UK TeX Archive

The memoir class - The UK TeX Archive

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

E.6. ProgrammingE.6 PROGRAMMINGOne of the commonest programming operations is to possibly do one thing if somethingis true and to possibly do another thing if it is not true. Generally speaking, this is calledan ‘if-then-else’ or conditional statement.\if... 〈test〉 〈true-text〉 [ \else 〈false-text〉 ] \fi<strong>TeX</strong> has several kinds of ‘if-then-else’ statements which have the general form shownabove. <strong>The</strong> statement starts with an \if... and is finished by a matching \fi. As usual,the brackets enclose optional elements, so there need be no \else portion. <strong>The</strong> 〈true-text〉,it it exists, is processed if the 〈test〉 is true otherwise the 〈false-text〉, if both the \elseclause and 〈false-text〉 are present, is processed.<strong>The</strong> simplest kind of \if... is defined by the \newif macro.\newif\if〈name〉\newif creates three new commands, the \ifname and the two declarations, \nametrueand \namefalse, for setting the value of \ifname to true or false respectively. In thiscase the 〈test〉 is embedded in the \if.... For example:\newif\ifpeter...\ifpeterMy name is Peter.\elseCall me Ishmael.\fior a more likely scenario is\newif\ifmine\minetrue % or \minefalse\newcommand{\whose}{%\ifmine It’s mine. \else I don’t know whose it is. \fi}Here are some of the other more commonly used kinds of ifs.\ifdim 〈dimen1〉 〈rel〉 〈dimen2〉\ifnum 〈number1〉 〈rel〉 〈number2〉\ifodd 〈number〉<strong>The</strong> 〈rel〉 in \ifnum and \ifdim is one of the three characters: < (less than), = (equals), or> (greater than). \ifdim results in true if the two lengths are in the stated relationshipotherwise it results in false. Similarly \ifnum is for the comparison of two integers. <strong>The</strong>\ifodd test is true if the integer 〈number〉 is an odd number, otherwise it results in false.Among other things, the La<strong>TeX</strong> <strong>class</strong> code that organizes the page layout checks if thelength values are sensible. <strong>The</strong> following code is a snippet from the layout algorithm. Itchecks that the sum of the margins and the width of the typeblock is the same as the widthof the page after trimming. \@tempdima and \@tempdimb are two ‘scratch’ lengths usedin many calculations.\@tempdimb= -1pt% allow a difference of 1pt\@tempdima=\paperwidth% paperwidth\advance\@tempdima by -\foremargin % minus the foremargin441

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!