8

I'd like to create a new subsection command with the same calling structure like the original one, but I have no idea on how to handle the case when the optional argument is omitted. Basically I want to have newsubsection[Short Title]{Long Title} to call subsection[#1]{#2} and newsubsection{Title} to call subsection{#1}.

I've read the thread about a similar problem posted here around two years ago, but it seems to have been much more complicated (more than one opt-argument) and I'm too much of a TeX-newbie to transfer the solutions to my problem.

Also, I already tried lots of weird combinations like \newcommand{\subsect}[2][#1]{\par\nolinenumbers\subsection[#1]{#2}\linenumbers} (failed attempt to use the syntax from the \wbalTwo-example here), but none of them were resulting in anything remotely working.

Big-Blue
  • 1,000

2 Answers2

6

xparse provides an easy interface and conditioning in terms of arguments. The following might be what you're after:

enter image description here

\documentclass{article}
\usepackage{lipsum,lineno,xparse}% http://ctan.org/pkg/{lipsum,lineno,xparse}
\NewDocumentCommand{\sect}{s o m}{%
  \par\nolinenumbers%
  \IfBooleanTF{#1}
    {\section*{#3}}% \sect*
    {\IfNoValueTF{#2}
      {\section{#3}}% \sect{...}
      {\section[#2]{#3}}% \sect[..]{...}
    }%
  \linenumbers%
}
\linenumbers
\begin{document} 
\section{First section}\lipsum[8]
\sect{Second section}\lipsum[8]
\end{document}

xparse provides \IfBooleanTF to condition on the presence of the * (or s macro argument) and \IfNoValueTF to condition on the presence of an optional argument (or o macro argument).

Although I've demonstrated it with \section, it's easy to modify to work with \subsection or the like.

Werner
  • 603,163
  • 1
    Thanks for this fast, detailed answer including the already ready-made example, which works just as I intended. – Big-Blue Jan 20 '13 at 18:30
  • @Big-Blue, I'm trying to use this to declare glossaries with optional inputs, which means that I have to call the macro from the preamble. Doing so gives the missing \begin{document} error. Is there a way to make it work? – Kajsa Aug 18 '17 at 12:29
  • @Kajsa: You can't call anything that sets content within the preamble. Such things have to occur after \begin{document}. – Werner Aug 18 '17 at 20:50
  • @Werner, Works with \newcommand but then I can't make the optional default be one of the mandatory arguments. – Kajsa Aug 20 '17 at 16:50
  • @Kajsa: I don't understand your problem. Perhaps you can post a complete minimal example that replicates the behaviour on PasteBin. – Werner Aug 21 '17 at 09:54
  • @Werner, Thank you for suggesting that. The problem was me using the wrong brackets and being unable to see it. – Kajsa Aug 24 '17 at 11:47
6

LaTeX has a helper macro \@dblarg which is used in sectioning and caption and similar commands for exactly this use: making an optional argument have a default value of the main argument.

\makeatletter
\def\newsubsection{\@dblarg\@newsubsection}
\def\@newsubsection[#1]#2{%
\typeout{opt arg: #1}%
\typeout{main arg: #2}%
}

\newsubsection{aaa}

\newsubsection[xxx]{yyy}

\stop

Produced a terminal output of

opt arg: aaa
main arg: aaa
opt arg: xxx
main arg: yyy
David Carlisle
  • 757,742
  • How would you trigger something after \@newsubsection using \@dblarg using your setup? That seems to be what the OP is after. – Werner Jan 20 '13 at 19:54
  • @Werner Not sure what you mean \@newsubsection could have more than two arguments if you need to pick up more. Or of course it can end with a macro that takes arguments? But the example given in the question doesn't seem to need that? – David Carlisle Jan 20 '13 at 19:59
  • @David Carlisle Thanks for providing an alternate method. Unfortunately I am not able to transfer your code to my problem, here is what I've tried. Any further advice is appreciated. – Big-Blue Jan 21 '13 at 13:00