What is the difference between \myname and \my@name while writing a .cls file?

\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.


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.


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.