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.

I've seen sometimes that people use a double pound sign (##) when defining/using arguments.

What is the difference between the normal argument, #1, and the double sign one, ##1? Are there any restrictions for its use? Can you list the good practices, if any, for this type of arguments.

share|improve this question
add comment

1 Answer

up vote 44 down vote accepted

It allows using arguments in nested macro definitions.

In

\def\a#1{\def\b#1{...}}

the macro \b would not have an argument, since #1 belongs to \a and would be replaced by its argument.

However,

\def\a#1{\def\b##1{...}}

defines \b with an argument. During expansion

  • #1 will be replaced by a parameter
  • ## becomes #

Then \b can use #1 instead of the original ##1.

It follows, that for each level of nesting you need to double the number of # characters:

\def\a#1{\def\b##1{\def\c####1{...}}}

Example:

\documentclass{article}
\def\a#1{\def\b##1{#1 ##1}}
\begin{document}
\a{x} % consequence: \def\b#1{x #1}
\b{y} % prints: x y
\end{document}

In LaTeX syntax this would be:

\newcommand{\a}[1]{%
  \newcommand{\b}[1]{#1 ##1}}

or, as \a and \b are already defined, which you would see if you would try it in the small example,

\renewcommand{\a}[1]{%
  \renewcommand{\b}[1]{#1 ##1}}
share|improve this answer
13  
and in a double-nested definition, the # must be doubled again, to ####, and so on. –  barbara beeton Jan 27 '12 at 14:22
3  
@barbarabeeton ... as I wrote in the answer. I will add a code line example to make that clearer. –  Stefan Kottwitz Jan 27 '12 at 16:15
add comment

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.