Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

What is the difference between \myname and \my@name while writing a .cls file? Are they equivalent?
Please let me know if it is necessary to post a minimal example.
Edit:
Let me ask my question more clearly:
What is the difference between the two following lines?

‎‎\def\myname#1{\gdef\@myname{#1}}‎‎
‎\newcommand*{\myname}[1]{\gdef\my@myname{#1}}‎‎

I am a bit confused with these lines. I know exactly the difference between \myname and \@myname, however I don't know the difference between \myname and \my@name in a .cls file.

share|improve this question
1  
\myname is a user macro but \my@name is an internal macro. I am not sure what you mean, when you ask "are they equivalent"? equivalent in what sense? Also this is already answered on parsilatex and there is no point in repeating questions. –  IRAN Feb 14 '11 at 18:23
2  
I think it's OK to repeat questions across different forums. on the other hand, this question has been asked here before, too. –  Matthew Leingang Feb 14 '11 at 18:34
    
The two lines in your question are the same for end-user. They have the same effect. But the latter seems preferable. \@myname looks like a command defined in LaTeX kernel, while \my@myname is likely about the prefix my. –  Leo Liu Feb 14 '11 at 18:43
1  
@Vafa: There would be no point if Vahid himself had asked the question on parsilatex. Otherwise I don't quite see your point. –  Hendrik Vogt Feb 14 '11 at 21:03

3 Answers 3

up vote 8 down vote accepted

\my@name and \myname are in different 'namespace' of TeX.

The catcode of @ are different in a .tex file and a .cls file. @ is a letter in a .cls or .sty file (catcode 11); but an 'other character' in a .tex file (catcode 12). (see TeXbook, Ch 8)

Therefore, a user of your class file cannot use \my@name directly in .tex file, it is protected; but (s)he can use \myname freely.

Macros with @ like \my@name are used for internal macros. They slould not be accessed by normal users. While macros without @ like \myname are used for interface macros, that should be used by normal users.

An example (it's silly, for demostration only):

% setname.sty
\providecommand\setname@name{}
\newcommand\setname[1]{%
  \renewcommand\setname@name{#1}}
\newcommand\getnameinparens{%
  (\setname@name)}
\endinput

Package users can use \setname to set the name and \getnameinparens to get the (name). However, they cannot access \setname@name, unless \makeatletter is used.

share|improve this answer
    
Please see my edit in my post. –  Vahid Damanafshan Feb 14 '11 at 18:42

The difference is that a user is less likely to redefine and clobber \my@name because @'s in control sequences are usually not allowed within regular TeX files.

This is the purpose of using @; it marks the control sequence as "internal" and not for end-user use. Users should not expect that an @-macro persist from version to version or work the same way.

This namespacing is not automatic; if two packages define \my@name one will overwrite the other. This is why it's a better idea to choose a (hopefully, relatively) unique "namespace" and prefix your internal macros with that plus the @. So if your package is called baz, call your internal macros \baz@foo, etc.

I say "usually" above because a LaTeX use can put \makeatletter...\makeatother in his/her TeX file and use @ as a letter. But users who do so know what they're doing and/or assume the risks of it.

share|improve this answer

To answer our updates question:

What is the difference between the two following lines?

\def\myname#1{\gdef\@myname{#1}}‎‎
‎\newcommand*{\myname}[1]{\gdef\my@myname{#1}}‎‎

See, \def is a TeX primitive, i.e. a macro which isn't defined using other macros but which executes a certain pre-defined, hard-coded function (however the names of the primitives can be redefined to user macros). Here \def sets the definition of \myname to \gdef\@myname{#1}. A primitive is to a macro like an axiom to a proof. Proofs depend on other proofs with depend on other proofs which finaly depend on a few axioms. It's the same with macros, they are defined as other macros (actually as other tokens) which can be defined again as other macros, which then finally end in primitives.

While TeX gives your access to the lower level, LaTeX gives you a neat user-interface. One drawback of \def is that it simply sets the definition of the macro, i.e. it defines or redefines it without checking if it was already defined before. If all or most LaTeX macros were defined using \def, packages could overwrite macros of other packages without warning which would leads pretty fast to a mess. Debugging such a scenario would be very difficult.

Therefore LaTeX gives you \newcommand which checks if the macro was already defined and triggers an error if so. It also allows you to simply give you the number of arguments as number (e.g. [3]), instead of writing e.g. #1#2#3. After all the checks (there are a few more) it uses \def internally to finally define the macro.

So, \newcommand is safer but slower (not that it matters much), while \def gives you the additional benefit of defining a parameter list. You can for example say:

\def\mymacro(#1,#2){...}

to define a macro which awaits (<something>,<something more>) and gives you this two values as #1 and #2. If the patterns doesn't match an error is triggered. The optional arguments [ ] are implemented very similar. More complex syntax like the one used in TikZ would not be possible if all macros would be defined using \newcommand.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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