I am trying to define a new command in my Latex file like this


But I get the following error after Latex compilation

ERROR: Illegal parameter number in definition of \\newchunk.

--- TeX said ---
<to be read again> 
l.58 ...nk}[1][2]{\section{#2}\label{sec:#1}\quad}

--- HELP ---
This is probably caused by a \newcommand, \renewcommand,
\newenvironment, or \renewenvironment command in which a # is used
incorrectly.  A # character, except as part of the command name \#,
can be used only to indicate an argument parameter, as in #2, which
denotes the second argument. This error is also caused by nesting one
of the above four commands inside another, or by putting a parameter
like #2 in the last argument of a \newenvironment or \renewenvironment

Don't we use #2 to refer to the second argument? Where am I going wrong?

  • Where's the advantage of \newchunk{label}{Title} over the clearer \section{Title}\label{label}? – egreg Nov 6 '18 at 0:14
  • As egreg mention, this is not the greatest idea. Here's another reason: a lot of editors can help with cross references, they do this by looking at the \label commands in the source. Here you're hiding them so chances are your editor will not see these labels and include them in the choose reference interface. – daleif Nov 6 '18 at 7:03

defines a command \newchunk to have a one, optional argument that has a default value of 2. It seems like you're after defining a command that can take an optional argument containing the \label. Here's a suggestion:


  % https://tex.stackexchange.com/q/53068/5764



See section~\ref{sec:second}.



The above \newchunk takes two arguments, the first of which is an optional label string, used in \label. A check is performed to see whether the user supplied anything or not. If something is supplied, it's used as a \label, otherwise no \label is issued. The second argument is mandatory and is used as the \section title.


\newcommand{<command>}[<number of arguments>]{<what the command does>}

The second argument of \newcommand expects the number of arguments that the command takes. So you would want to do


Your Answer

