18

I understand the purpose of all special characters, $, &, %, _, {, }, ^, ~, and \, but just except for hash (#). When and why should it be used?

20

The character # (with category code 6) has two uses.

It denotes a parameter to a macro both in the parameter text and in the replacement text:

\def\foo#1{-#1-}

and in this context it should be followed by a digit from 1 to 9, with the restriction that the parameters must be numbered consecutively in the parameter text. Note that LaTeX automatically supplies the parameter text, so, for instance,

\newcommand{\foo}[3]{...}

ultimately reduces to

\def\foo#1#2#3{...}

There is a special usage for # in the parameter text: if # appears just before the opening brace, the last argument of the macro being defined will be delimited by the brace. You can see examples in the TeXbook or in TeX by Topic.

The second usage for # is in alignment preambles. The primitive \halign can be followed by to <dimen> or spread <dimen>, then a { should appear. What follows next is a sequence of chunks of the form <u>#<v> separated by & and ending with \cr, called the alignment preamble, whose purpose is to specify the tokens to be added to every subsequent entry in the table. The # represents the actual contents of a cell in the alignment. A typical alignment preamble can be

#\hfil&\hfil#\hfil&\hfil#\hfil&\hfil#\cr

which specifies four columns, with left alignment in the first, center alignment in the next two and right alignment in the fourth column.

In LaTeX, tabular is realized through \halign, the alignment preamble is automatically built from the mandatory argument to the environment.

|improve this answer|||||
17

# is the macro parameter character, #1 means macro parameter one, #2 parameter two etc. If you want to use it as a character itself, use \# or change the catcode.

\newcommand{\mymacro}[5]{% #1 #2 #3 #4 #5 }

will use 5 macro arguments, available as #1 to #5.

However, if there is a \newcommand within \mymacro, the macro parameter has to be doubled:

\documentclass{article}

\newcommand{\mymacro}[1]{%
  \newcommand{\myinnermacro}[1]{%
    Outer: #1

    Inner: ##1
  } 

  \myinnermacro{\textbf{#1}}
}


\begin{document}
\mymacro{\LaTeX\ is beautiful}
\end{document} 

enter image description here

As can be seen from the screenshot, the content of #1 is masked within of the inner macro and used as ##1 then.

The # property holds for \def and \NewDocumentCommand etc. too, of course.

Edit

Here is a bad example how to change the macro parameter character ;-)

\documentclass{article}

\catcode`#=11
\catcode`!=6


\newcommand{\mymacro}[1]{%
  \newcommand{\myinnermacro}[1]{%
    Outer: !1

    Inner: !!1
  } 

  \myinnermacro{\textbf{!1}}
}


\begin{document}
\mymacro{\LaTeX\ is beautiful}
\end{document} 

Use it at your own risk ;-)

|improve this answer|||||
13

# is used in defining commands;

\newcommand\zzz[2]{this #1 and this #2.}

defines a command with two arguments such that

\zzz{a}{b}

expands to

this a and this b.
|improve this answer|||||
  • Stock answer? ;-) – user31729 Mar 11 '15 at 22:42
  • 7
    @SoundsOfSilence there's not an answer posted to any question that couldn't be found in the archive of comp.text.tex over the last 30 years. – David Carlisle Mar 11 '15 at 22:44

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.