Change the catcode and then define a (Unicode) character
Change the catcode and then define a (Unicode) character
The motivation is that I sometimes have, e.g.
% !TEX encoding = UTF-8 Unicode
catcode`¯=active
def¯#1#2overline#1#2
$¯ab+¯bc=¯ac$
in my code.
I thought it would be nice if there is a macro, say activatedefine
,
such that
activatedefine
activatedefine¯#1#2overline#1#2
does the job that catcode
and def
do.
catcode
def
I understand that it is not correct to write
defactivatedefine#1
catcode`#1=active
def#1
because the catcode of the #1
in the third line is not changed.
But then I hear that futurelet
can duplicate tokens.
So I came up with
#1
futurelet
defactivatedefine
futureletthecharnowwecanplaywiththechar
defnowwecanplaywiththechar
expandaftercatcodeexpandafter`thechar=13
def
This is still not quite right because thechar
does not expand to ¯
.
So I try to fix it
thechar
¯
defactivatedefine
futureletthecharnowwecanplaywiththechar
defgobbletwo#1 #2
defnowwecanplaywiththechar
edefactivatethechar
noexpandcatcode`expandaftergobbletwomeaningthechar=active
%message^^J^^J stringactivatethechar is meaningactivatethechar
activatethechar
%message^^J The catcode becomes thecatcode`¯^^J^^J
def
activatedefine¯#1#2overline#1#2
This macro does change the catcode of ¯
to 13
.
But TeX complains Missing control sequence inserted
.
¯
13
Missing control sequence inserted
What is going wrong with my code?
Edit
Almost forgot MWE
% !TEX TS-program = XeLaTeX
% !TEX encoding = UTF-8 Unicode
documentclassarticle
begindocument
defactivatedefinefutureletthecharnowwecanplaywiththechar
%defnowwecanplaywiththechar
% expandaftercatcodeexpandafter`thechar=13
% def
%
defgobbletwo#1 #2
defnowwecanplaywiththechar%
edefactivatethechar%
noexpandcatcode`expandaftergobbletwomeaningthechar=active
message^^J^^J stringactivatethechar is meaningactivatethechar
activatethechar
message^^J The catcode becomes thecatcode`¯^^J^^J
def¯
activatedefine¯#1#2overline#1#2
enddocument
futurelet
It's not a good idea to make the back quote active.
– egreg
Sep 15 '18 at 20:51
also what do you mean by Uniocde here? if you are using pdftex you can only define single byte tokens this way not arbitrary unicode characters.
– David Carlisle
Sep 15 '18 at 20:54
@jfbu That
futurelet
will freeze the catcode is unexpected (by me). That answers why TeX complains.– Symbol 1
Sep 15 '18 at 20:56
futurelet
@egreg I did not want to make back quote active. If my code did that it is unintended. I (only) want to make non-ASCII characters active.
– Symbol 1
Sep 15 '18 at 20:58
2 Answers
2
documentclassarticle
defactivatedefine#1begingrouplccode`~=`#1relax
lowercaseendgroupdef~%
activatedefine¯#1#2overline#1#2
begindocument
$¯ab+¯bc=¯ac$
enddocument
documentclassarticle
defactivatedefine#1begingrouplccode`~=`#1relax
lowercaseendgroupcatcode`#1activedef~%
activatedefine|#1#2overline#1#2
begindocument
$|ab+|bc=|ac$
enddocument
I did not pay much attention to the actual macro, which is in math mode so math active would be possibly better.
Besides I don't want to have to handle UTF-8, so I used |
rather for the example.
|
AH! but OP is using XeTeX. So we can do this
documentclassarticle
defactivatedefine#1begingrouplccode`~=`#1relax
lowercaseendgroupcatcode`#1activedef~%
activatedefine¯#1#2overline#1#2
begindocument
thecatcode`¯ % ONLY XETEX, NOT PDFTEX!
$¯ab+¯bc=¯ac$
enddocument
% Local variables:
% TeX-engine: xetex
% End:
Problem with pdflatex is that ¯
is multibyte, and the erased code at top of my answer was redefinig the first byte, breaking LaTeX UTF-8.
¯
Yes sorry don't take any notice of me
– David Carlisle
Sep 15 '18 at 20:58
@jfbu Your trick works in XeLaTeX. Are you suggesting that
lowercase
will reevaluate the catcode of ¯
?– Symbol 1
Sep 15 '18 at 21:06
lowercase
¯
The tilde is active. The
lowercase
modifies the character code, but keeps the category code. So it creates an active token, which we can define. Then we must not forget to actually make this character associated to active category code.– user4686
Sep 15 '18 at 21:08
lowercase
@jfbu Where can I find the exact behavior of
lowercase
, including what you just said? Why on earth can lowercase
contains assignments... This blows my mind.– Symbol 1
Sep 15 '18 at 21:18
lowercase
lowercase
TeX by Topic
3.6.4 Creative use of uppercase and lowercase
. A bit terse... but search this site for egreg+lowercase
for many many examples. See also a bit earlier in TeX by Topic 3.6.2 Uppercase and lowercase commands
.– user4686
Sep 15 '18 at 21:19
3.6.4 Creative use of uppercase and lowercase
egreg+lowercase
3.6.2 Uppercase and lowercase commands
The simplest way is to use newunicodechar
; it just defines parameterless macros, but we can exploit the fact TeX is a macro expansion language to begin with.
newunicodechar
Note that these examples can be used with all TeX engines (except Knuth TeX).
documentclassarticle
usepackagenewunicodechar
newunicodechar‾overline
newcommandtwooverline[2]overline#1#2
newunicodechar﹌twooverline
begindocument
$‾a$ $﹌ab$
enddocument
Another example:
documentclassarticle
usepackagenewunicodechar
makeatletter
newunicodechar‾symbol@overline
defsymbol@overline#1‾overline#1 % must go second
begindocument
$‾abc‾$
enddocument
I see what you mean. I prefer @jfbu's answer because I prefer a local trick. I prefer local trick because I want
def•circle(.2)
in some TikZ picture but def•bullet
outside the picture.– Symbol 1
Sep 15 '18 at 21:24
def•circle(.2)
def•bullet
@Symbol1 Using the same symbol or macro for two different meanings is not what I'd recommend.
– egreg
Sep 15 '18 at 21:32
@Symbol1 Anyway, you can do
newunicodechar•bullet
in the preamble and def•circle(.2)
in the TikZ picture, if you so like (only XeLaTeX or LuaLaTeX).– egreg
Sep 15 '18 at 21:36
newunicodechar•bullet
def•circle(.2)
Yes, I know it is not a good practice. I usually include unicode-math so for this very particular case
•
is already the bullet symbol (of catcode 12). And my strategy here is to [re]define •
in TikZ as local as possible.– Symbol 1
Sep 15 '18 at 23:59
•
•
I want to say that I do have very good reason to use unicode in TikZ. For instance gist.github.com/Symbol1/597b2433dc402b5711af37556df443eb.
– Symbol 1
Sep 16 '18 at 0:10
Thanks for contributing an answer to TeX - LaTeX Stack Exchange!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy
using
futurelet
will freeze the catcode of the next token (the one to which the macro is let). This may or not be relevant to your problems (I admit not having read in detail).– user4686
Sep 15 '18 at 20:45