The = in assigments is optional. Actually, it's slightly more efficient to use =, but with a very small advantage. Benchmarking \catcode`?13 against \catcode`?=13 yields
No =
1.02e-7 seconds (0.472 ops)
With =
9.25e-8 seconds (0.427 ops)
I used l3benchmark which repeates the code several times in order to give as accurate an estimate as possible. By the way, it would be even more efficient to use \active instead of 13
No =
7.94e-8 seconds (0.378 ops)
With =
6.93e-8 seconds (0.327 ops)
Once you have set the catcode of a character to 13 (active character), it behaves like a control sequence, so you can use it as the token following \def (or any other control sequence defining command) to assign it a meaning.
The feature is exploited by babel to introduce shorthands. For instance, when the current babel language is German, the combination "| denotes a morpheme boundary, which is important in order to break unwanted ligatures, so one can type in Auf"|lage that makes it clear that the word is composed by “auf” and “lage”. This is achieved by giving " catcode 13 and assigning it a suitable definition based on the following token: "- inserts an additional line break point, allowing for hyphenation also past it.
Sounds familiar? Yes, it's a situation very similar to yours, with a big difference: the character " is very seldom used in TeX, but the period is part of TeX's syntax in many places.
Once you do \catcode`.=13, you will no longer be able to specify
\vspace{1.5pt}
Try it. You'd need to say \vspace{1\string.5pt}. Or you might use a comma, instead of a period. Are you keen to do it?
OK, let's assume you are. The idea is to define the active . to look for the next token and decide what to do.
The standard way to do this in LaTeX is to use \@ifnextchar, to which you supply the token to look for and the code to be executed in case of “success” or “failure”.
The given definition prints a period (instead of \char`. I'd do \string.) and then executes
\@ifnextchar'{T}{F}
(here T and F stand for the actual code). If the character following the period in the typescript is ', then T is executed, otherwise F. Since you want to do nothing, in this case, T is actually empty. In case ' doesn't follow, F is executed and the actual code is
\@ifnextchar.{}{ }
If a period follows, do nothing, otherwise a space is inserted. Why this check? I'm not sure, because typing two consecutive periods is not really usual and three periods is incorrect, because \ldots should be used.
Anyway, the code does its job. But, still assuming you're willing to accept several problems like the \vspace{1.5pt} described above, this is not the best programming.
\documentclass{article}
\makeatletter
% define the desired action for the active period
\begingroup\lccode~=.\lowercase{\endgroup
\newcommand{\active@period}{.@ifnextchar'{}{@ifnextchar~{}{ }}}%
}
\AtBeginDocument{%
% assign the meaning of \active@period to the active period
\begingroup\lccode~=. \lowercase{\endgroup\let~}\active@period
% change the catcode
\catcode.=\active } \makeatother % to locally set the category code of . to 12 \newcommand{\Abbr}{\catcode.=12 }% don't forget the space!
\begin{document}
...
\end{document}
This way the setting of the category code is delayed to when the document begins an configuration files have already been read in.
The space after 12 is needed. Try
{\Abbr 1.2.3}
with the original code to see there's a problem. Indeed, you'd get
\catcode`.121.2.3
and assigning 121 as a catcode is illegal. With the space it would be
\catcode`.12 1.2.3
and the space after 12 is ignored per TeX rules.
With a more modern approach:
\documentclass{article}
\ExplSyntaxOn
% to locally set the category code of . to 12
\NewDocumentCommand{\Abbr}{}{\char_set_catcode_other:N .}
% define the desired action for the active period
\cs_new_protected:Nn \youthdoc_active_period:
{
.
\peek_charcode:NF .
{% a period doesn't follow
\peek_charcode:NF '
{% a quote doesn't follow, insert a space
\c_space_tl
}
}
}
% at begin document set the period to be active
\AtBeginDocument
{
\char_set_active_eq:NN . \youthdoc_active_period:
\char_set_catcode_active:N .
}
\ExplSyntaxOff
\begin{document}
A period.With a space.But ...has a space only at the end
`A quote.'
\end{document}
No contortions to have the period with different catcodes in the definitions.
.active is a sure source for head scratching when weird error messages are thrown. Can you please mention what answer you're referring to? – egreg Nov 08 '22 at 15:23=is almost always optional,\let\abc\xyzis\let\abc=\xyz,\catcode\.13is\catcode`.=13` – David Carlisle Nov 08 '22 at 15:25\vspace{1.5pt}in your document. – egreg Nov 08 '22 at 18:42