Chapter12. Expansion
For\noexpandthemostimportantapplicationisprobablyin\edefcommands(butin
writestatementsitcanoftenreplace\string).Considerasanexample
\edef\one{\def\noexpand\two{\the\prevdepth}}
Withoutthe\noexpand,T
E
Xwouldtrytoexpand\two,thusgivingan‘undefinedcontrol
sequence’error.
A(ratherpointless)illustrationofthefactthat\noexpandmakesthefollowingtokenef-
fectivelyintoa\relaxis
\def\a{b}
\noexpand\a
This willnot produceany output,becausetheeffect of the\noexpandistomakethe
controlsequence\atemporarilyequalto\relax.
12.4.2 \noexpandandactivecharacters
Thecombination\noexpandtokenisequivalentto\relax,evenifthetokenisanactive
character.Thus,
\csname\noexpand~\endcsname
willnotbethesameas\char‘\~.Insteaditwillgiveanerrormessage,becauseunexpan-
dablecommands–suchas\relax–arenotallowedtoappearinbetween\csnameand
\endcsname.Thesolutionistouse\stringinstead;seepage137foranexample.
In anothercontext,however,thesequence\noexpandactivecharacterisequivalentto
thecharacter,butinunexpandableform.Thisiswhentheconditionals\ifand\ifcatare
used(foranexplanationofthese,seeChapter13).Compare
\if\noexpand~\relax % is false
wherethecharactercodeofthetildeistested,with
\def\a{ ... } \if\noexpand\a\relax % is true
wheretwocontrolsequencesaretested.
12.5 \relax
Thecontrolsequence\relaxcannotbeexpanded,butwhenitisexecutednothinghap-
pens.
Thisstatementsoundsabitparadoxical,soconsideranexample.Letcounters
\newcount\MyCount
\newcount\MyOtherCount \MyOtherCount=2
begiven.Intheassignment
\MyCount=1\number\MyOtherCount3\relax4
thecommand\numberisexpandable,and\relaxisnot.WhenT
E
Xconstructsthenumber
thatistobeassigneditwillexpandallcommands,eitheruntilanon-digitisfound,oruntil
anunexpandablecommandisencountered.Thusitreadsthe1;itexpandsthesequence
130
VictorEijkhout–T
E
XbyTopic
Delete blank pages in pdf - remove PDF pages in C#.net, ASP.NET, MVC, Ajax, WinForms, WPF
Provides Users with Mature Document Manipulating Function for Deleting PDF Pages
delete page on pdf document; delete page in pdf file
Delete blank pages in pdf - VB.NET PDF Page Delete Library: remove PDF pages in vb.net, ASP.NET, MVC, Ajax, WinForms, WPF
Visual Basic Sample Codes to Delete PDF Document Page in .NET
best pdf editor delete pages; acrobat export pages from pdf
12.5. \relax
\number\MyOtherCount,whichgives2;itreadsthe3;itseesthe\relax,andasthisis
unexpandableithalts.Thenumbertobeassignedisthen123,andthewholecallhasbeen
expandedinto
\MyCount=123\relax4
Sincethe\relaxtokenhasnoeffectwhenitisexecuted,theresultofthislineisthat123
isassignedto\MyCount,andthedigit4isprinted.
Anotherexampleofhow\relaxcanbeusedtoindicatetheendofacommandis
\everypar{\hskip 0cm plus 1fil }
\indent Later that day, ...
Thiswillbemisunderstood:T
E
Xwillsee
\hskip 0cm plus 1fil L
andfil Lisavalid,ifbizarre,wayofwritingfill(seeChapter36).Oneremedyisto
write
\everypar{\hskip 0cm plus 1fil\relax}
12.5.1 \relaxand\csname
Ifa\csname ... \endcsnamecommandformsthenameofapreviouslyundefinedcon-
trolsequence,thatcontrolsequenceismadeequalto\relax,andthewholestatementis
alsoequivalentto\relax(seealsopage116).
However,thisassignmentof\relaxisonlylocal:
{\xdef\test{\expandafter\noexpand\csname xx\endcsname}}
\test
givesanerrormessageforanundefinedcontrolsequence\xx.
ConsiderasanexampletheLAT
E
Xenvironments,whicharedelimitedby
\begin{...} ... \end{...}
Thebeginandendcommandsare(inessence)definedasfollows:
\def\begin#1{\begingroup\csname#1\endcsname}
\def\end#1{\csname end#1\endcsname \endgroup}
Thus,forthelistenvironmentthecommands\listand\endlistaredefined,butany
command can beusedasanenvironmentname,evenifnocorresponding \end...has
beendefined.Forinstance,
\begin{it} ... \end{it}
isequivalentto
\begingroup\it ... \relax\endgroup
Seepage 106 for therationalebehind using \begingroupand \endgroupinstead of
\bgroupand\egroup.
VictorEijkhout–T
E
XbyTopic
131
C# PDF Page Insert Library: insert pages into PDF file in C#.net
such as how to merge PDF document files by C# code, how to rotate PDF document page, how to delete PDF page using Add and Insert Blank Pages to PDF File in
delete pages in pdf reader; delete pages of pdf preview
VB.NET PDF Page Insert Library: insert pages into PDF file in vb.
Able to add and insert one or multiple pages to existing adobe PDF document in VB.NET. Ability to create a blank PDF page with related by using following online
delete pages on pdf; delete pages from pdf reader
Chapter12. Expansion
12.5.2 Preventingexpansionwith\relax
Because\relaxcannotbeexpanded,acontrolsequencecanbepreventedfrombeingex-
panded(forinstanceinan\edefora\write)bymakingittemporarilyequalto\relax:
{\let\somemacro=\relax \write\outfile{\somemacro}}
willwritethestring‘\somemacro’toanoutputfile.Itwouldwritetheexpansionofthema-
cro\somemacro(orgiveanerrormessageifthemacroisundefined)ifthe\letstatement
hadbeenomitted.
12.5.3 T
E
Xinsertsa\relax
T
E
Xitselfinserts\relaxonsomeoccasions.Forinstance,\relaxisinsertedifT
E
Xen-
countersan\or,\else,or\fiwhilestilldeterminingtheextentofthetest.
\ifvoid1\else ... \fi
ischangedinto
\ifvoid1\relax \else ...\fi
internally.
Similarly,ifoneofthetests\if,\ifcatisgivenonlyonecomparand,asin
\if1\else ...
a\relaxtokenisinserted.Thusthistestisequivalentto
\if1\relax\else ...
Anotherplacewhere\relaxisusedisthefollowing.Whileacontrolsequenceisbeingde-
finedinashorthanddefinition–thatis,aregisterdefor\chardefor\mathchardef–
itsmeaningistemporarilymadeequalto\relax.Thismakesitpossibletowrite\chardef\foo=123\foo.
12.5.4 Thevalueofnon-macros;\the
ExpansionisapreciselydefinedactivityinT
E
X.Thefulllistoftokensthatcanbeexpanded
wasgivenabove.Othertokensthanthoseintheabovelistmayhavean‘expansion’inan
informal sense. Forinstanceonemaywishto ‘expand’ the\parindentintoitsvalue,
say20pt.
Convertingthevalueof(amongothers)anintegerparameter,aglueparameter,dimen
parameteroratokenparameterintoastringofcharactertokensisdonebytheexpansion
processor.Thecommand \theisexpanded wheneverexpansionisnot inhibited,andit
takesthevalueofvarioussortsofparameters.Itsresult(inmostcases)isastringoftokens
ofcategory12,exceptthatspaceshavecategorycode10.
Hereisthelistofeverythingthatcanbeprefixedwith\the.
parameterorregister Iftheparameterorregisterisoftypeinteger,glue,dimenor
muglue,itsvalueisgivenasastringofcharactertokens;ifitisoftypetokenlist
(forinstance\everyparor\toks5),theresultisastringoftokens.Boxregisters
areexcludedhere.
codename8-bitnumber Seepage49.
132
VictorEijkhout–T
E
XbyTopic
C# Word - Insert Blank Word Page in C#.NET
Users to Insert (Empty) Word Page or Pages from a to specify where they want to insert (blank) Word document rotate Word document page, how to delete Word page
delete pages from pdf acrobat; delete page from pdf acrobat
C# PowerPoint - Insert Blank PowerPoint Page in C#.NET
to Insert (Empty) PowerPoint Page or Pages from a where they want to insert (blank) PowerPoint document PowerPoint document page, how to delete PowerPoint page
delete blank pages in pdf; cut pages from pdf
12.6. Examples
specialregister Theintegerregisters\prevgraf,\deadcycles,\insertpenalties
\inputlineno,\badness,\parshape,\spacefactor(onlyinhorizontalmode),
or\prevdepth(onlyinvertical mode).Thedimensionregisters\pagetotal,
\pagegoal,\pagestretch,\pagefilstretch,\pagefillstretch,\pagefilllstretch,
\pageshrink,or\pagedepth.
Fontproperties: \fontdimenparameternumberfont,\skewcharfont,\hyphencharfont.
Lastquantities: \lastpenalty,\lastkern,\lastskip.
definedcharacter Anycontrolsequencedefinedby\chardefor\mathchardef;the
resultisthedecimalvalue.
Insomecases\thecangiveacontrolsequencetokenorlistofsuchtokens.
font Theresultisthecontrolsequencethatstandsforthefont.
tokenvariable Tokenlistregistersandtokenparameterscanbeprefixedwith\the;
theresultistheircontents.
Letusconsideranexampleoftheuseof\the.Ifinafilethatistobe\inputthecategory
codeofacharacter,saytheatsign,ischanged,onecouldwrite
\edef\restorecat{\catcode‘@=\the\catcode‘@}
andcall\restorecatattheendofthefile.Ifthecategorycodewas11,\restorecatis
definedequivalentto
\catcode‘@=11
Seepage137formoreelaboratemacrosforsavingandrestoringcatcodes.
12.6 Examples
12.6.1 Expandingafter
Themostobvioususeof\expandafteristoreachoveracontrolsequence:
\def\stepcounter
#1{\expandafter\advance\csname
#1:counter\endcsname 1\relax}
\stepcounter{foo}
Herethe\expandafterletsthe\csnamecommandformthecontrolsequence\foo:counter;
after\expandafterisfinishedthestatementhasreducedto
\advance\foo:counter 1\relax
Itispossibletoreachovertokensotherthancontrolsequences:in
\uppercase\expandafter{\romannumeral \year}
itexpands\romannumeralontheothersideoftheopeningbrace.
Youcanexpandaftertwocontrolsequences:
\def\globalstepcounter
#1{\expandafter\global\expandafter\advance
\csname #1:counter\endcsname 1\relax}
Ifyouthinkof\expandafterasreversingtheevaluationorderoftwocontrolsequences,
youcanreversethreeby
VictorEijkhout–T
E
XbyTopic
133
C# Create PDF Library SDK to convert PDF from other file formats
String outputFile = Program.RootPath + "\\" output.pdf"; // Create a new PDF Document object with 2 blank pages PDFDocument doc = PDFDocument.Create(2
delete page in pdf preview; add and remove pages from pdf file online
VB.NET PDF File & Page Process Library SDK for vb.net, ASP.NET
is unnecessary, you may want to delete this page is the programmatic representation of a PDF document instance may consist of newly created blank pages or image
delete pages from pdf acrobat reader; delete page pdf online
Chapter12. Expansion
\expandafter\expandafter\expandafter\a\expandafter\b\c
whichreachesacrossthethreecontrolsequences
\expandafter
\a
\b
toexpand\cfirst.
Thereisevenanunexpectedusefor\expandafterinconditionals;with
\def\bold#1{{\bf #1}}
thesequence
\ifnum1>0 \bold \fi {word}
willnotgiveaboldface‘word’,but
\ifnum1>0 \expandafter\bold \fi {word}
will.The\expandafterletsT
E
Xseethe\fiandremoveitbeforeittacklesthemacro
\bold(seealsopage147).
12.6.2 Defininginsidean\edef
ThereisoneT
E
Xcommandthatisexecutedinsteadofexpandedthatisworthpointingout
explicitly:theprimitivecommand\def(andallotherdefcommands)isnotexpanded.
Thusthecall
\edef\next{\def\thing{text}}
willgivean‘undefinedcontrolsequence’for\thing,eventhoughafter\defexpansionis
ordinarilyinhibited(seepage126).After
\edef\next{\def\noexpand\thing{text}}
the‘meaning’of\nextwillbe
macro: \def \thing {text}
Thedefinition
\edef\next{\def\noexpand\thing{text}\thing}
willagaingivean‘undefinedcontrolsequence’for\thing(thistimeonitssecondoccur-
rence),asitwillonlybedefinedwhen\nextiscalled,notwhen\nextisdefined.
12.6.3 Expansionand\write
Theargumenttokenlistof\writeistreatedinmuchthesamewayasthereplacementtext
ofan \edef; thatis,expandablecontrol sequencesandactivecharactersarecompletely
expanded.Unexpandablecontrolsequencesaretreatedby\writeasiftheyareprefixed
by\string.
Becauseoftheexpansionperformedby\write,somecarehastobetakenwhenoutputting
controlsequenceswith\write.Evenmorecomplicationsarisefromthefactthattheex-
pansionoftheargumentof\writeisonlyperformedwhenitisshippedout.Herefollows
aworked-outexample.
Suppose\somecsisamacro,andyouwanttowritethestring
134
VictorEijkhout–T
E
XbyTopic
VB.NET Create PDF Library SDK to convert PDF from other file
Dim outputFile As String = Program.RootPath + "\\" output.pdf" ' Create a new PDF Document object with 2 blank pages Dim doc As PDFDocument = PDFDocument
cut pages out of pdf online; delete pages from pdf in reader
How to C#: Cleanup Images
returned. Delete Blank Pages. Set property BlankPageDelete to true , blank pages in the document will be deleted. Remove Edges or Borders.
delete pages of pdf online; cut pages from pdf online
12.6. Examples
\def\othercs{theexpansionof\somecs}
toafile.
Thefirstattemptis
\write\myfile{\def\othercs{\somecs}}
Thisgivesanerror‘undefinedcontrolsequence’for\othercs,becausethe\writewill
trytoexpandthattoken.Notethatthe\somecsisalsoexpanded,sothatpartisright.
Thenextattemptis
\write\myfile{\def\noexpand\othercs{\somecs}}
Thisisalmostright,butnotquite.Thestatementwrittenis
\def\othercs{expansionof\somecs}
whichlooksright.
However,writes–andtheexpansionoftheirargument–arenotexecutedonthespot,but
saveduntilthepartofthepageonwhichtheyoccurisshippedout(seeChapter30).So,in
themeantime,thevalueof\somecsmayhavechanged.Inotherwords,thevaluewritten
maynotbethevalueatthetimethe\writecommandwasgiven.Somehow,therefore,the
currentexpansionmustbeinsertedinthewritecommand.
Thefollowingisanattemptatrepair:
\edef\act{\write\myfile{\def\noexpand\othercs{\somecs}}}
\act
Nowthewritecommandwillbe
\write\myfile{\def\othercs{valueof \somecs}}
The\noexpandpreventedthe\edeffromexpandingthe\othercs,butafterthedefini-
tionithasdisappeared,sothatexecutionofthewritewillagaingiveanundefinedcontrol
sequence.Thefinalsolutionis
\edef\act{\write\myfile
{\def \noexpand\noexpand \noexpand\othercs{\somecs}}}
\act
Inthiscasethewritecommandcausedbytheexpansionof\actwillbe
\write\myfile{\def\noexpand\othercs{currentvalueof\somecs}
andthestringactuallywrittenis
\def\othercs{currentvalueof\somecs}
Thismechanismisthebasisforcross-referencingmacrosinseveralmacropackages.
12.6.4 Controlledexpansioninsidean\edef
Sometimesyoumayneedan\edeftoevaluatecurrentconditions,butyouwanttoexpand
somethinginthereplacementtextonlytoacertainlevel.Supposethat
\def\a{\b} \def\b{c} \def\d{\e} \def\e{f}
isgiven,andyouwanttodefine\gas\aexpandedonestep,followedby\dfullyexpanded.
Thefollowingworks:
VictorEijkhout–T
E
XbyTopic
135
VB.NET PDF: Get Started with PDF Library
Auto Fill-in Field Data. Field: Insert, Delete, Update Field. RootPath + "\\" output.pdf" ' Create a new PDF Document object with 2 blank pages Dim doc
add or remove pages from pdf; cut pages out of pdf file
Chapter12. Expansion
\edef\g{\expandafter\noexpand\a \d}
Explanation:the\expandafterreachesoverthe\noexpandtoexpand\aonestep,after
whichthesequence\noexpand\bisleft.
Thistrickcomesinhandywhenyouneedtoconstructacontrolsequencewith\csname
insidean\edef.Thefollowingsequenceinsidean\edef
\expandafter\noexpand\csname name\endcsname
willexpandexactlyto\name,butnotfurther.Asanexample,suppose
\def\condition{true}
hasbeengiven,then
\edef\setmycondition{\expandafter\noexpand
\csname mytest\condition\endcsname}
willlet\setmyconditionexpandto\mytesttrue.
12.6.5 Multiplepreventionofexpansion
Aswaspointedoutabove,prefixingacommandwith\noexpandpreventsitsexpansion
incommandssuchas\edefand\write.However,ifasequenceoftokenspassesthrough
morethanoneexpandingcommandstrongermeasuresareneeded.
Thefollowingtrickcanbeused:inordertoprotectacommandagainstexpansionitcanbe
prefixedwith\protect.Duringthestagesofprocessingwhereexpansionisnotdesired
thedefinitionof\protectis
\def\protect{\noexpand\protect\noexpand}
Lateron,whenthecommandisactuallyneeded,\protectisdefinedas
\def\protect{}
Whydoesthiswork?Theexpansionof
\protect\somecs
isatfirst
\noexpand\protect\noexpand\somecs
Insidean\edefthissequenceisexpandedfurther,andthesubsequentexpansionis
\protect\somecs
Thatis,theexpansionisequaltotheoriginalsequence.
12.6.6 Moreexampleswith\relax
Above,afirstexamplewasgiveninwhich\relaxservedtopreventT
E
Xfromscanning
toofar.Herearesomemoreexamples,using\relaxtoboundnumbers.
After
\countdef\pageno=0 \pageno=1
\def\Par{\par\penalty200}
thesequence
136
VictorEijkhout–T
E
XbyTopic
12.6. Examples
\Par\number\pageno
ismisunderstoodas
\par\penalty2001
Inthiscaseitissufficienttodefine
\def\Par{\par\penalty200 }
asanoptionalspaceisallowedtofollowanumber.
Sometimes,however,suchasimpleescapeisnotpossible.Considerthedefinition
\def\ifequal#1#2{\ifnum#1=#2 1\else 0\fi}
Thequestioniswhetherthespaceafter#2isnecessary,superfluous,orsimplywrong.Calls
suchas\ifequal{27}{28}thatcomparetwonumbers(denotations)willcorrectlygive1
or0,andthespaceisnecessarytopreventmisinterpretation.
However,\ifequal\somecounter\othercounterwillgive1ifthecountersareequal;
inthiscasethespacecouldhavebeendispensedwith.Thesolutionthatworksinbothcases
is
\def\ifequal#1#2{\ifnum#1=#2\relax 1\else 0\fi}
Notethat\relaxisnotexpanded,so
\edef\foo{1\ifequal\counta\countb}
willdefine\fooaseither1\relax1or10.
12.6.7 Example:categorycodesavingandrestoring
In many applicationsit is necessaryto changethecategory codeofacertaincharacter
duringtheexecutionofsomepieceofcode.Ifthewriterofthatcodeisalsothewriterof
thesurroundingcode,s/hecansimplychangethecategorycodebackandforth.However,
ifthesurroundingcodeisbyanotherauthor,thevalueofthecategorycodewillhavetobe
storedandrestored.
Thusonewouldliketowrite
\storecat@
... some code ...
\restorecat@
ormaybe
\storecat\%
forcharactersthatarepossiblyacommentcharacter(orignoredorinvalid).Thebasicidea
istodefine
\def\storecat#1{%
\expandafter\edef\csname restorecat#1\endcsname
{\catcode‘#1=\the\catcode‘#1}}
sothat,forinstance,\storecat$willdefinethesinglecontrolsequence‘\restorecat$’
(onecontrolsequence)as
\catcode‘$=3
VictorEijkhout–T
E
XbyTopic
137
Chapter12. Expansion
Themacro\restorecatcanthenbeimplementedas
\def\restorecat#1{%
\csname restorecat#1\endcsname}
Unfortunately,thingsarenotsosimple.
Theproblemsoccurwithactivecharacters,becausetheseareexpandedinsidethe\csname ... \endcsname
pairs.Onemightbetemptedtowrite\noexpand#1everywhere,butthisiswrong.Aswas
explainedabove,thisisessentiallyequalto\relax,whichisunexpandable,andwillthe-
reforeleadtoanerrormessagewhenitappearsbetween\csnameand\endcsname.The
propersolutionisthentouse\string#1.Forthecasewheretheargumentwasgivenasa
controlsymbol(forexample\%),theescapecharacterhastobeswitchedoffforawhile.
Herearethecompletemacros.The\storecatmacrogivesitsargumentadefaultcategory
codeof12.
\newcount\tempcounta % just a temporary
\def\csarg#1#2{\expandafter#1\csname#2\endcsname}
\def\storecat#1%
{\tempcounta\escapechar \escapechar=-1
\csarg\edef{restorecat\string#1}%
{\catcode‘\string#1=
\the\catcode\expandafter‘\string#1}%
\catcode\expandafter‘\string#1=12\relax
\escapechar\tempcounta}
\def\restorecat#1%
{\tempcounta\escapechar \escapechar=-1
\csname restorecat\string#1\endcsname
\escapechar\tempcounta}
12.6.8 Combining\aftergroupandboxes
Attimes,onewantstoconstructaboxandimmediatelyafterit hasbeenconstructedto
dosomethingwithit.The\aftergroupcommandcanbeusedtoputboththecommands
creatingthebox,andtheoneshandlingit,inonemacro.
Asanexample,hereisamacro\textvcenterwhichdefinesavariantofthe\vcenter
box(seepage205)thatcanbeusedoutsidemathmode.
\def\textvcenter
{\hbox \bgroup$\everyvbox{\everyvbox{}%
\aftergroup$\aftergroup\egroup}\vcenter}
Theideaisthatthemacroinserts\hbox {$,andthatthematching$}getsinsertedbythe
\aftergroupcommands.Inordertogetthe\aftergroupcommandsinsidethebox,an
\everyvboxcommandisused.
Thismacrocanevenbeusedwithaboxspecification(seepage58),forexample
\textvcenter spread 8pt{\hbox{a}\vfil\hbox{b}}
andbecauseitisreallyjustan\hbox,itcanalsobeusedina\setboxassignment.
138
VictorEijkhout–T
E
XbyTopic
12.6. Examples
12.6.9 Moreexpansion
Thereisaparticularcharmtomacrosthatworkpurelybyexpansion.Seethearticlesby
[11],[16],and[32].
VictorEijkhout–T
E
XbyTopic
139
Documents you may be interested
Documents you may be interested