7

I use the following command to have section headings with subtitles

\newcommand{\Section}[2]{\section[#1]{#1\\\small »#2«}}

But now I only can have numbered sections because the * doesn't work for my own command. How can I have my command working both ways, with and without the asterisks?

3 Answers 3

9

This is the standard way to define *-variants:

\makeatletter
\newcommand{\xyz}{\@ifstar\@xyzstar\@xyz}
\newcommand{\@xyzstar}<what to do when there's a *>
\newcommand{\@xyz}<what to do when there's not a *>
\makeatother

In you case it's probably

\makeatletter
\newcommand{\Section}{\@ifstar\@Sectionstar\@Section}
\newcommand{\@Sectionstar}[2]{\section*{#1\\\small »#2«}}
\newcommand{\@Section}[2]{\section[#1]{#1\\\small »#2«}}
\makeatother
4

The xparse package provides an intuitive means for incorporating stars * in commands. Conditioning on whether you are using a starred/unstarred version is done using \IfBooleanTF:

\documentclass{article}
\usepackage[T1]{fontenc}% http://ctan.org/pkg/fontenc
\usepackage{xparse}% http://ctan.org/pkg/xparse
\NewDocumentCommand{\Section}{s m m}{
  \IfBooleanTF{#1}%
    {\section*{#2\\\small \guillemotright #3\guillemotleft}}% Unnumbered section
    {\section[#2]{#2\\\small \guillemotright #3\guillemotleft}}% Numbered section
}
\begin{document}
\Section{First section}{first title}
\Section*{Second section}{second title}
\end{document}

enter image description here

2
  • Should this solution be prefered to the one egreg presented?
    – Sebastian
    Commented Sep 30, 2011 at 19:10
  • @Sebastian: This might be a user preference. If you're comfortable with @egreg's solution and it is easy for you to modify, then that's that. Otherwise, for me, xparse is easy to read in terms of the code, rather than following a sequence of commands. In my answer, xparse is an additional package that has to be loaded (that comes with additional functionality). In @egreg's answer, manual definition of 2 commands is required (one for the starred * and one for the unstarred version). your choice.
    – Werner
    Commented Sep 30, 2011 at 19:17
3

It might be preferable to control section numbering globally. That is, you use non-asterisk sectioning commands everywhere in your document and then you use a command to say which section levels will be numbered.

Here's how to do it. If you place

\setcounter{secnumdepth}{1}

in your preamble, then only the highest level section will be numbered.

1
  • I don't want to activate or deactivate it globally but individually with * or without
    – Sebastian
    Commented Sep 30, 2011 at 9:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .