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

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.

-
\newcommand{\mysec}[2][]{\section[#1]{#2}}? –  Henri Menke Feb 16 '14 at 13:31
@HenriMenke Contrary to \section, your \mysec command will result in incorrect (empty) headers and ToC entries if the optional argument is omitted. –  lockstep Feb 16 '14 at 14:05

Quoting the UK TeX FAQ:


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}

-

Macro \section has several syntax forms:

• \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}

-

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}

-