[Tex/LaTex] How to define a new command based on \section with its optional argument

macrosoptional argumentssectioning

I've defined a new command called \mysec like the below:
‎‎‎‎‎‎

\documentclass{article}
\newcommand{\mysec}[1]{\section{#1}}
\begin{document}
\mysec[Foo foo]{Bla bla bla}
\end{document}

But in the PDF output, I get:

1 [

Foo foo]Bla bla bla

How to solve this problem?

By the way, I'd prefer to have the solution without using extra packages. Also note that I don't want to redefine the \section command.

Best Answer

Quoting the UK TeX FAQ:

Optional arguments, in macros defined using \newcommand, don’t quite work like the optional argument to \section. The default value of \section’s optional argument is the value of the mandatory argument, but \newcommand requires that you ‘know’ the value of the default beforehand.

The requisite trick is to use a macro in the optional argument[...]

Using your MWE plus the first FAQ solution:

\documentclass{article}
\newcommand\mysec[2][\DefaultOpt]{%
  \def\DefaultOpt{#2}%
  \section[#1]{#2}%
}
\begin{document}
\tableofcontents
\mysec{Foo}
\mysec[Bla]{Bla bla bla}
\end{document}

enter image description here

Related Question