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


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:


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,


ultimately reduces to


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


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

# 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:


    Outer: #1

    Inner: ##1


\mymacro{\LaTeX\ is beautiful}

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.


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



    Outer: !1

    Inner: !!1


\mymacro{\LaTeX\ is beautiful}

Use it at your own risk ;-)

|improve this answer|||||

# is used in defining commands;

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

defines a command with two arguments such that


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.