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'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.

share|improve this question
add comment

2 Answers

up vote 6 down vote accepted

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.

share|improve this answer
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
add comment

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
share|improve this answer
    
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
add comment

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.