6

The motivation is that I sometimes have, e.g.

% !TEX encoding = UTF-8 Unicode
\catcode`¯=\active
\def¯#1#2{\overline{#1#2}}
$¯ab+¯bc=¯ac$

in my code. I thought it would be nice if there is a macro, say \activatedefine, such that

\activatedefine¯#1#2{\overline{#1#2}}

does the job that \catcode and \def do.

I understand that it is not correct to write

\def\activatedefine#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

\def\activatedefine{
    \futurelet\thechar\nowwecanplaywiththechar
}
\def\nowwecanplaywiththechar{
    \expandafter\catcode\expandafter`\thechar=13
    \def
}

This is still not quite right because \thechar does not expand to ¯. So I try to fix it

\def\activatedefine{
    \futurelet\thechar\nowwecanplaywiththechar
}
\def\gobbletwo#1 #2 {}
\def\nowwecanplaywiththechar{
    \edef\activatethechar{
        \noexpand\catcode`\expandafter\gobbletwo\meaning\thechar=\active
    }
    %\message{^^J^^J \string\activatethechar is \meaning\activatethechar}
    \activatethechar
    %\message{^^J The catcode becomes \the\catcode`¯^^J^^J}
    \def
}
\activatedefine¯#1#2{\overline{#1#2}}

This macro does change the catcode of ¯ to 13. But TeX complains Missing control sequence inserted.

What is going wrong with my code?

Edit

Almost forgot MWE

% !TEX TS-program = XeLaTeX
% !TEX encoding = UTF-8 Unicode

\documentclass{article}
\begin{document}

\def\activatedefine{\futurelet\thechar\nowwecanplaywiththechar}
%\def\nowwecanplaywiththechar{
%   \expandafter\catcode\expandafter`\thechar=13
%   \def
%}

\def\gobbletwo#1 #2 {}
\def\nowwecanplaywiththechar{%
    \edef\activatethechar{%
        \noexpand\catcode`\expandafter\gobbletwo\meaning\thechar=\active
    }
    \message{^^J^^J \string\activatethechar is \meaning\activatethechar}
    \activatethechar
    \message{^^J The catcode becomes \the\catcode`¯^^J^^J}
    \def¯
}
\activatedefine¯#1#2{\overline{#1#2}}

\end{document}
  • 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
  • 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
  • @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
6

\documentclass{article}

\def\activatedefine#1{\begingroup\lccode`~=`#1\relax
                      \lowercase{\endgroup\def~}}%

\activatedefine¯#1#2{\overline{#1#2}}

\begin{document}

$¯ab+¯bc=¯ac$
\end{document}

\documentclass{article}

\def\activatedefine#1{\begingroup\lccode`~=`#1\relax
                      \lowercase{\endgroup\catcode`#1\active\def~}}%

\activatedefine|#1#2{\overline{#1#2}}

\begin{document}
$|ab+|bc=|ac$
\end{document}

enter image description here

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

\documentclass{article}

\def\activatedefine#1{\begingroup\lccode`~=`#1\relax
                      \lowercase{\endgroup\catcode`#1\active\def~}}%

\activatedefine¯#1#2{\overline{#1#2}}

\begin{document}
\the\catcode`¯ % ONLY XETEX, NOT PDFTEX!

$¯ab+¯bc=¯ac$
\end{document}
% Local variables:
% TeX-engine: xetex
% End:

enter image description here

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
  • 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
  • @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
  • 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
4

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.

Note that these examples can be used with all TeX engines (except Knuth TeX).

\documentclass{article}

\usepackage{newunicodechar}

\newunicodechar{‾}{\overline}

\newcommand{\twooverline}[2]{\overline{#1#2}}
\newunicodechar{﹌}{\twooverline}

\begin{document}

$‾a$ $﹌ab$

\end{document}

enter image description here

Another example:

\documentclass{article}

\usepackage{newunicodechar}

\makeatletter
\newunicodechar{‾}{\symbol@overline}
\def\symbol@overline#1‾{\overline{#1}} % must go second

\begin{document}

$‾abc‾$

\end{document}

enter image description here

  • 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
  • @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
  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.