The title already states the whole question. It has been answered by Joseph Wright at http://www.texdev.net/2009/11/17/tex-counts-and-latex-counters/, but I have not seen the according answer here at tex.stackexchange.com yet.
[Tex/LaTex] What are the differences between TeX counts and LaTeX counters
counterstex-core
Related Solutions
As always with (La)TeX: If you know what you are doing you are allowed to do it!
Some local counter changes make perfectly sense, like in the example in the question. However I wouldn't call secnumdepth
a real counter. After all it is never incremented. Here simply a counter register was used to store an integer because there is no other suitable type.
Real LaTeX counters for sectioning commands, lists or other stuff should normally be global, because they express a count which goes over the whole document and is not limited to local groups.
If you are so far that you need a local counter for your code you normally already know about \newdimen
and can define a TeX counter instead of a LaTeX one.
A good example IMHO of a local redefinition of an otherwise global counter is shown in my answer to Cross-referencing in multiple chapters. There I use {\value{chapter}=<value>\relax\thechapter}
(which is kind of a dirty hack) so I can use the current \thechapter
format but with a different chapter number as the current one.
PS: I usually code after the rule "Once global, always global!", except in special cases as explained above.
The short answer
There are actually three levels of obfuscation in that code: Firstly, some clever recursive macros are used to compress the text to just a few lines. Secondly, some kind of substitution and transposition ciphers are implemented by rather straightforward macros like \defR#1#2#3{#3#2#1}
(where R
is an active character). The third level is the use of category codes to avoid intellegible TeX-specific characters like {
, }
, #
and \
.
Some more explanations
I saw this TeX file more that 10 years ago and thought Eh? So I tried to take it apart step by step. On the way I learned a lot about TeX's macro language, and also about category codes. To begin with, I'll only post the very first steps I took. (I still have the files on my computer, and this one is named hae.1
, "hae" being German and meaning "eh?")
\let~\catcode ~`76 ~`A13 ~`F1 ~`j00 ~`P2
\defA#1{~`#113\def}
ALL{
}
A''{w} A;;{} AZZ{LaL} A//#1{#1i} AHH{L}
Azz{en} ASS{th}; A$${ev} A@@{f}
ARR#1#2#3{#3#2#1};
ADD{Rgni} AWW#1{} ATT{ve} A**{stRsam} AGG{Rruo}
Aqq#1.#2.{#1#2#1} AYY#1#2{#2#1#1}
A??{i*Lm} A&\fi{\fi#1} AVV{\bigskipR}WG
AUU#1#2#3#4 #5,#6{\par#1#2#3#5D\ifx:#6\else&U#6\fi}L
AKK#1#2{#1l#2#1}
AXX#1{VLnOSeL#1SLRyadR@oL RrhC?yLRurtK{eL}{ov}gaTLtReRomL;}
ABB#1 #2,#3:{\if.#3.\else B#3:\fiX{#1}U#1 #2,#3:}Ws;
AMM#1{#1di} AJJ{Rdri} AQQ{RsreL} AII#1{o#1d} A!!{Rgie}
Bt'el@ lTLqdrYmu.Q.,Ke;vz vzLqpip.Q.,tz;
;Lql.IrsZ.eap,qn.i. i.eLlMaesLdRcna,;!;h htLqm.MRasZ.ilk,%
s$;z zLqs'.ansZ.Ymi,/sx ;LYegseZRyal,@i;@ TLRlogdLrDsW,@;G
LcYlaDLbJsW,SWXJW ree @rzchLhzsW,;WERcesInW qt.'oL.Rtrul;e
doTsW,Wk;Rri@stW aHAHH{ndZ}pqar.tridgeLinZpe.LtYer.W,:\bye
You see, I replaced F
with {
and P
with }
throughout. Why? ~`F1
gives the character F
category code 1 (since the active character ~
is \let
to \catcode
), and TeX understands such characters as opening braces. In the same way, P
is given category code 2 by ~`P2
, so it acts as a closing brace.
The next thing is to understand ~`76
and ~`j00
. The first makes 7
behave like the macro parameter character #
(category code 6), and the second makes j
behave like the control sequence character \
(category code 0), so I've replaced 7
with #
and j
with \
throughout. This enhances readability quite a bit already. Moreover, I added some white space and line breaks, which helps some more.
The key point now is to understand what A
does. This is an "active character" (category code 13) due to ~`A13
, so it behaves like a control sequence. So what does the definition \defA#1{~`#113\def}
mean? A
takes one argument #1
. Then ~
acts as \catcode
, so A
gives it's argument category code 13, and then it issues an additional \def
.
So how does this fit with the output of \tracingall
you got? The first line
A71->~`7113\def
says the following: To the left of ->
you see that the active character A
takes one argument #1
(recall that 7
acts as #
); to the right of ->
you see the corresponding expansion. Now the second line
71<-L
says that #1
should be substituted with L
, so that the expansion is ~`L13\def
. Now ~
was \let
to \catcode
, so the category code assignment \catcode`L13
is performed next (making L
an active character); then the \def
is executed. This is what you see in the next two lines:
{\catcode}
{\def}
(Unfortunately \tracingall
doesn't say anything specific about the execution of \catcode
and \def
.)
Let's look at the usage A''{w}
. This expands to \catcode`'13\def'{w}
, so '
is made an active character, and it's given a definition, namely that '
should expand to w
(one of the transposition ciphers)! Just one more example: A??{i*Lm}
makes ?
active and gives it the definition i*Lm
, which in turn expands to istRsam m
since *
expands to stRsam
and L
to a space. The final result is istmas m
since R
acts as a transposition cipher, as mentioned in the very beginning. And now we're able to understand the little piece RrhC?y
in the code – it expands to Christmas my
!
A challenge
If you remove all the cipher and catcode business, you just have some recursive macros that represent the text rather efficiently. Can anyone do it with less than 479 characters?
\let~\def~\U#1,#2:{\par#1ing \if.#2.\else\U#2:\fi}~\,{\def\,{and
}}~\;#1~#2 #3,#4:{\if.#4.\else\;#4:\fi\bigskip On the #1#2th day of
Christmas my true love gave to me\U#1#3,#4:}~~#1 {}\;twel~f ve drummers
drumm,eleven~ \ pipers pip,ten~ \ lords a leap,nin~ e ladies danc,eigh~
t maids a milk,seven~ \ swans a swimm,six~ \ geese a lay,fi~f ve gold
rings~,four~ \ calling birds~,th~ird\ ~ ree french hens~,~second\ ~ two
turtle doves~,~first\ ~ \,a partridge in a pear tree.~,:\bye
Best Answer
At the LaTeX level, a counter is created using
This creates a counter initialised at zero which can then be set using
or manipulated using
\stepcounter
and\addtocounter
There are then some methods to get the counter value back out. LaTeX creates a
\the...
function for each counter, which will print the current value. In places where TeX expects a number, there is also the\value
function:LaTeX's counters are set globally. That makes them good for tracking something that covers the entire document, but not as good for localised calculations.
A TeX count is created using
where the name is a name including a backslash. Setting a count is done very simply: there is no set function
Notice the
\relax
here. Without it, TeX will continue to look for the number in the next thing it finds. This can have some odd effects, and is best avoided. Altering the value can then be carried out using\advance
A similar termination is brought about by having a space after the number
The value of a count register can be recovered using
\the
or\number
, and the name itself can be used where TeX expects a number.The big difference is that TeX sets count registers locally. So to do a global assignment you have to do it deliberately
As LaTeX is built on TeX, you might guess that LaTeX's counters are an interface to TeX’s count registers, but it's not immediately obvious how this is done. The way it works is that LaTeX prefixes all of the counter names with
c@
, so that if I didLaTeX would issue an error message: the counter is already defined. The other LaTeX functions then build on this, so that they manipulate the internal counters. This is all done globally and with some error checking. For example, the definition of
\addtocounter
isThis checks the counter exists, and if it does globally advances it.