How to define a new command based on \section with its optional argument?

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


Macro \section has several syntax forms:

  • \section*{title}: The star form suppresses the numbering and the entry in the table of contents.
  • \section[toc]{title}: A different entry in the table of contents can be given with the optional argument if the star is not used.

Two LaTeX kernel macros are helpful to parse the arguments of \section:

  • \@ifstar{<star>}{<no star>}: If a star follows, then the first argument is called, otherwise the second. If a star is seen, it is removed from the input.

  • \@dblarg{<macro or macro with arguments>}: The macro \@dblarg looks, whether the next mandatory argument is preceded by an optional argument and calls its argument with two arguments. If there was an optional argument, then this argument is given in square brackets, followed by the mandatory argument. In this case, \@dblarg is more or less a no-op. But if the optional argument is missing, then \@dblarg duplicates the contents of the mandatory argument and passes it in square brackets. Examples:

    \@dblarg{\foo}[aa]{bb} ⇒ \foo[aa]{bb}
    \@dblarg{\foo}{cc} ⇒ \foo[cc]{cc}
    

Then \mysec can be defined the following way:

\documentclass{article}

\makeatletter
\newcommand*{\mysec}{%
  \@ifstar{\@mysec@star}{\@mysec}%
}
\newcommand*{\@mysec}{%
  \@dblarg\@mysec@nostar%
}
\newcommand*{\@mysec@star}[1]{%
  \section*{#1}%
  \begin{tabular}{@{}l@{ }l@{}}
    Star: yes\\
    Title: #1\\
  \end{tabular}%
}
\def\@mysec@nostar[#1]#2{%
  \section[{#1}]{#2}%
  \begin{tabular}{@{}l@{ }l@{}}
    Star: & no\\
    Toc: &#1\\
    Title: &#2\\
  \end{tabular}%
}
\makeatother

\begin{document}
\mysec*{Introduction}
\mysec{My section}
\mysec[Foo foo]{Bla bla bla}
\end{document}

Result


This is an easy job for xparse package:

\documentclass{article}
\usepackage{xparse}
\DeclareDocumentCommand\mysec{om}
 {%
  \IfValueTF{#1}
   {\section[#1]{#2}}
   {\section{#2}}%
 }
\begin{document}
   \tableofcontents
  \section[my section]{Here is normal section}
  Some text
  \section{Another normal section}
  Some text
  \mysec[foo bar]{this is my section}
  Text
  \mysec{this is my second section}
  Text
\end{document}

If you don't want to use any extra package:

\documentclass{article}
\let\mysec\section
\begin{document}
   \tableofcontents
  \section[my section]{Here is normal section}
  Some text
  \section{Another normal section}
  Some text
  \mysec[foo bar]{this is my section}
  Text
  \mysec{this is my second section}
  Text
\end{document}

enter image description here