24

I know most of the effects of the special characters in LaTeX, like \ is used for most commands, or $ is used for opening or closing the math ambience, but I don't know what the symbol # does, and I have seen it in some lines at commands for the creation of personalized packages... here is an example of it:

\newcommand{\mytitle}[2][]%
  {\newcommand\@mytitle{#2}
   \ifthenelse{\not\equal{#1}{}}
              {\newcommand\@shortmytitle{#1\ \ldots}}
              {\newcommand\@shortmytitle{\@mytitle}}
  }

but I still don't understand what the # symbol does... so if anyone could explain it... also what does the \@ command do?

26

Amongst other things (that most likely falls outside the scope of this question), # is used to reference arguments. In a general setting,

\newcommand{<cmd>}[<n>][<opt>]{<stuff>}

allows you to reference any of the <n> arguments inside <stuff> using #1 (for the first argument, which is optional in this case), #2 (for the second argument), ... #n (for the n-th argument) up to 9. There are ways to extend this, but it is highly discouraged from a user-experience point of view.

The above syntax also holds for \renewcommand and \providecommand (using LaTeX2e), and translates seamlessly to \newenvironment and friends as well. There is also no distinction between the use of arguments as being optional or mandatory.

Here are some examples:

enter image description here

\documentclass{article}
\newcommand{\cmdA}[2]{Arg 1: #1; Arg 2: #2}% \cmdA{#1}{#2}
\newcommand{\cmdB}[2][abc]{Arg 1: #1; Arg 2: #2}% \cmdB[#1]{#2}
\newenvironment{envA}[2]
  {EnvA -- Arg 1: #1; Arg 2: #2}% \begin{envA}{#1}{#2}
  {}% \end{envA}
\newenvironment{envB}[2][xyz]
  {EnvB -- Arg 1: #1; Arg 2: #2}% \begin{envB}[#1]{#2}
  {}% \end{envB}
\begin{document}
\cmdA{123}{456}

\cmdA{}{789}

\cmdB[qwe]{rty}

\cmdB{klm}

\begin{envA}{foo}{bar}
stuff
\end{envA}

\begin{envB}[foo]{baz}
stuff
\end{envB}

\begin{envB}{barz}
stuff
\end{envB}
\end{document}

For the latter question, see What do \makeatletter and \makeatother do? - since @ is special. That is, since you're using @ somewhere inside your macro definitions (it's okay in the replacement text), then you need to be careful. To that end, wrap \makeatletter...\makeatother around it if not used inside a class or style file.


Specific to your example:

\newcommand{\mytitle}[2][]%
  {\newcommand\@mytitle{#2}
   \ifthenelse{\not\equal{#1}{}}
              {\newcommand\@shortmytitle{#1\ \ldots}}
              {\newcommand\@shortmytitle{\@mytitle}}
  }

In the outer part you have

\newcommand{\mytitle}[2][]%
  {%<stuff
  }

This creates a macro \mytitle that takes one optional argument (which is empty by default) and one mandatory argument. As such, it's called using \mytitle[<opt>]{<man>} (where \mytitle{<man>} is equivalent to \mytitle[]{<man>}). In order to gain access to the arguments, you reference them as #1 for <opt> and #2 for <man>. If you have macros with arguments inside other macros, the argument references "double" at every level. For more on this, see What is the meaning of double pound symbol (##1) in an argument?.

The use of @ in macros is not possible in general, since control sequences are only allowed to contain letters (as designated by their category codes), and @ is not a letter. So, in order to create a macro of the form \@mytitle, you need to make sure @ is a letter first. Why would one want to go through the hassle if it requires additional care? Since it requires special care, there very little chance that other users may interfere with it, or even have packages clash with one another over the same macro names. For example, while \titlename might be a very common choice for a title name holder macro, \title@n@me might not be.

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.