I suggest to use xparse
and \RenewDocumentCommand
with provides easier ways to define macros with optional arguments
The argument specification somo
says to use a starred (#1
), optional (#2
), mandatory (#3
) and a final optional argument (#4
), if needed.
Redefining the sectioning commands should be done with care since they provide a starred variant which is used for example in \tableofcontents
as a 'heading'
Instead of \@ifnextchar[
it's necessary to use \IfValueTF{#2}{true}{#4}
to ask whether the optional argument #2
was used or not.
\documentclass{article}
\usepackage{xcolor}
\usepackage{xparse}
\let\origsection\section
\RenewDocumentCommand{\section}{somo}{%
\IfBooleanTF{#1}{%
\origsection*{#3}%
\IfValueTF{#4}{%
\textcolor{red}{You used the starred version and the 4th optional argument: }\textcolor{violet}{#4}%
}{%
% \textcolor{blue}{You used the starred version but omitted the 4th one}%
}
}{%
\IfValueTF{#2}{%
\origsection[#2]{#3}
\IfValueTF{#4}{%
\textcolor{red}{You used the optional 2nd and 4th. arguments: }\textcolor{violet}{#4}%
}{%
\textcolor{blue}{You used the optional 2nd argument but omitted the optional 4th one}%
}
}{%
\origsection{#3}
\IfValueTF{#4}{%
\textcolor{red}{You used the optional 4th. argument: }\textcolor{violet}{#4}%
}{%
\textcolor{blue}{You omitted the optional argument}
}
}%
}%
}
\begin{document}
\tableofcontents
\section*{Starred section}[With an optional argument]
\section{A section}
\section[Another section]{Another section with useless long title}
\section[Another section]{Another section with useless long title}[And again with an optional argument]
\section{A section}[With optional arg]
\end{document}