5

I have put the following in example.tex:

\catcode`\@=13 

\let@\IND

The problem is when I want to use my package eexxample.sty that contains example.tex, the comand @ won't come out, it will turn out to catcode 12, I show it using \the\catcode`\@. Maybe the \usepackage redefine the command? But I don't know why...

The code is::

When i type this, the @ shows catcode 12, maybe have you meant before that @ shows catcode 12?

\documentclass{scrreprt}

\usepackage{eexxample}

\begin{document}

\the\catcode`\@

$@X_a @X_a^g @X^g X_a X_a^g X^g $ \ $X_b$ \ $\IND X_b \IND X_b^a \IND X_b \IND X^a $ \

$@X_a @X_a^g @X^g X_a X_a^g X^g $ \

\end{document}

but when i type this, the catcode @ show 13, and the command is functioning, so i think TEX redefine this @ command :

\documentclass{scrreprt}

\begin{document}

\input{C:/LocalTexFiles/tex/example}

\the\catcode`\@

$@X_a @X_a^g @X^g X_a X_a^g X^g $ \ $X_b$ \ $\IND X_b \IND X_b^a \IND X_b \IND X^a $ \

$@X_a @X_a^g @X^g X_a X_a^g X^g $ \

\end{document}

I would like to thank first for your help!


When i type this, the @ shows catcode 12, maybe have you meant before that @ shows catcode 12?

\documentclass{scrreprt}
\usepackage{eexxample}
\begin{document}
\the\catcode`\@

$@X_a @X_a^g @X^g X_a X_a^g X^g $ \\
$X_b$ \\
$\IND X_b \IND X_b^a \IND X_b \IND X^a $ \\

$@X_a @X_a^g @X^g X_a X_a^g X^g $ \\

\end{document}

but when i type this, the catcode @ show 13, and the command is functioning, so i think TEX redefine this @ command :

\documentclass{scrreprt}
\begin{document}
\input{C:/LocalTexFiles/tex/example}

\the\catcode`\@

$@X_a @X_a^g @X^g X_a X_a^g X^g $ \\
$X_b$ \\
$\IND X_b \IND X_b^a \IND X_b \IND X^a $ \\

$@X_a @X_a^g @X^g X_a X_a^g X^g $ \\

\end{document}
Joseph Wright
  • 259,911
  • 34
  • 706
  • 1,036
  • Oh perhaps your mean that you are setting the catcode of @ in a package. Do not do that, the catcode of @ is always set to 11 at the start of a package and restored at the end to the value it had at the start. – David Carlisle Jan 12 '15 at 22:37
  • Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. – yo' Jan 12 '15 at 22:45

2 Answers2

11

At the start of every package the current catcode of @ is pushed on to a stack, and the catcode of @ is set to 11 so that internal commands may be used. At the end of the package the catcode of @ is restored to the saved value and the stack is popped. this means that any settings of the catcode of @ in a package will be lost.

Like any other latex behaviour this could be over-ridden but it would break most tex code, are you sure you want to make @ globally active?

The active definition of @ in your package is not lost it is just that @ is not active (it would be very bad to make @ active in the preamble as it would almost certainly cause the commands in the aux file to fail.

Your package could use

\AtBeginDocument{\catcode`\@\active}

so @ is active in the document, but this will almost certainly break many packages not expecting this, safer would be if you defined a command that make @ active and just used that locally within special environments.

David Carlisle
  • 757,742
  • Hey thanks for the reply. I have set the definition of @ in my example.tex, and i put the example.tex in my own package(eexxample.sty) using \input{example} in my package. I think tex have redefine the @ in package. Because i already try my example.tex using \input{C:/LocalTexFilex/tex/example} in my document, and the command @ show what i wanted. I wont to make @ globally active, cause i think i will use the symbol @ later . – stevennessa Jan 12 '15 at 22:51
  • @stevennessa The active definition is still there so if you put \catcode\@=\activeat the point you want to use@it will work, but of all the characters you could choose why use a character like@` that already has so many special uses in latex, as part of command names, as part of tabular syntax, etc. – David Carlisle Jan 12 '15 at 23:06
  • do you have any advice how i can make a command active and inactive in an environments? And also which symbol do i need to use instead of @, because @ is often use in latex. – stevennessa Jan 12 '15 at 23:23
  • @stevennessa active characters are tricky things why do you want an active character rather than a command? – David Carlisle Jan 12 '15 at 23:26
  • Because i will use @ very often, and i think with active characters it´s the simplest and fastest way to bring up the function of active characters that we define before. I will use @ often because i use @ to write all of the subscript Index become at the same high ,when you want try to write X_b and X_b^a the Index b shows the different high, thats why i need a command/active characters to make the index at a same high. – stevennessa Jan 13 '15 at 00:09
  • You can change the math parameters so the index height is the same without having to mark every single instance by hand – David Carlisle Jan 13 '15 at 00:35
  • Can you give me some ideas how to change the parameters? – stevennessa Jan 13 '15 at 00:44
  • 1
    @stevennessa that's a different question but eg http://tex.stackexchange.com/questions/10286/is-it-possible-to-change-the-depth-of-subscripts/10310#10310 (control of sub or super script are similar) – David Carlisle Jan 13 '15 at 00:59
3

I don't know how you define \IND, so I'll just define it to call \mathcal. Since you seem to be using @ as active only inside math, there's another strategy available, that of making @ math active.

In your package write

\newcommand{\IND}{\newcommand{\IND}[1]{\mathcal{#1}} % or whatever

\begingroup
\catcode`@=\active
\global\let @=\IND
\endgroup

\AtBeginDocument{\mathcode`@=\string"8000} % \string is to avoid problems with babel

This will make @ only math active, which means that outside math it will behave normally, but inside math it will assume the same meaning as \IND.

Example

\documentclass{article}

\newcommand{\IND}[1]{\mathcal{#1}} % or whatever

\begingroup
\catcode`@=\active
\global\let @=\IND
\endgroup

\AtBeginDocument{\mathcode`@=\string"8000} % \string is to avoid problems with babel

\begin{document}

$@X_a @X_a^g @X^g X_a X_a^g X^g $

$\IND X_b \IND X_b^a \IND X_b \IND X^a $

\end{document}

enter image description here


If you want to use @ as active character nonetheless, then it's much better to define

\def\whateverATwillstandfor{...}

and later define the active @ to do that action:

\begingroup\lccode`~=`@
\lowercase{\endgroup\AtBeginDocument{\let~\whateverATwillstandfor\catcode`@=\active}}

The \lowercase trick avoids the need of setting @ as active in the package. It will only be set at begin document, and it will receive the intended meaning.

egreg
  • 1,121,712
  • The problem is are that i define @ and \IND at one file .tex, let me call this file a.tex, and i will type some file at b.tex, and the problem is i want to make somehow @ active in a.tex, and input this a.tex in a package, but i think it's useless to make @ active in a.tex , because in package @ will be unactive. I found a way to make @ useable in b.tex, using \AtBeginDocument{\mathcode@=\string"8000}, but my proffesor want to put it somehow at a.tex(but it won't working),the reason is so that he didnt must to type \AtBeginDocument{\mathcode@=\string"8000} at a.tex. Any suggestion? – stevennessa Jan 22 '15 at 22:00
  • actually i also can change @ as active character become an @ or anything new command, the reason i use active characters, because it's easy to only type @ to call the function of @ that i define in my a.tex , because i will be using @ to write a tensor, and @ will use so many times to write in a document. – stevennessa Jan 22 '15 at 22:05