Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
2  
\newcommand{\mysec}[2][]{\section[#1]{#2}}? –  Henri Menke Feb 16 at 13:31
    
@HenriMenke Thank you for your reply. Please change your comment to an answer so that I can accept it. –  Sisabe Feb 16 at 13:48
2  
@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 at 14:05

3 Answers 3

up vote 9 down vote accepted

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

share|improve this answer

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

share|improve this answer

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.