Article View: comp.text.tex
Article #11817Re: problems with optional parameters...
From: Heiko Oberdiek
Date: Fri, 27 Feb 1998 00:00
Date: Fri, 27 Feb 1998 00:00
101 lines
3962 bytes
3962 bytes
Leo Cacciari <leo.cacciari@univ-pau.fr> wrote:
>[...]
>\newcommand{\subscriptifnotempty}[2]{{#1\ifx#2\empty
> \relax
> \else
> _{#2}
> \fi}}
>\newcommand{\Set}[2][\empty]{\ensuremath{\subscriptifnotempty{\mathsf{#2}}{#1}}\xspace}
>
>\newcommand{\Mname}[2][\empty]{\ensuremath{\subscriptifnotempty{\mathbf{#2}}{#1}}\xspace}
>
>\newcommand{\INMSet}[2][\empty]{\ensuremath{{\Set{#1}{#2}}^{\text{in}}}\xspace}
>
>\begin{document}
>When I type \verb|\INMSet[\Mname{M}]{S}| I espect to obtain somethyng
>like ${\mathsf{S}}_{\mathbf{M}}^{\text{in}}$ but I get
>\INMSet[\Mname{M}]{S} (ie.\ ${\mathbf{M}}S^{\text{in}}$).
>
>Why?
(a) Main: You define \Set with an optional argument, but you use it
with two non-optional parameters in definition of \INMSet.
(b) In math non-argument-braces {...} create a subformula:
$X_a^b$ is different from ${X_a}^b$!
You should remove the braces in the definitions of
\subscriptifnotempty and \INMSet.
(c) In definition of \INMSet the ^{\text{in}} is not in \ensuremath.
Changes (a), (b) and (c):
\newcommand{\subscriptifnotempty}[2]{#1\ifx#2\empty
\relax
\else
_{#2}
\fi}
\newcommand{\Set}[2][\empty]{\ensuremath{\subscriptifnotempty{\mathsf{#2}}{#1}}\xspace}
\newcommand{\Mname}[2][\empty]{\ensuremath{\subscriptifnotempty{\mathbf{#2}}{#1}}\xspace}
\newcommand{\INMSet}[2][\empty]{\ensuremath{\Set[#1]{#2}^{\text{in}}}\xspace}
(d) You use a dangerous \empty-Test in \subscriptifnotempty:
If #2 is really empty and not the command \empty then the not wanted
\else-part is executed.
Empty-tests:
* In plainTeX the empty command is called \empty (\def\empty{})
* In LaTeX the empty command is called \@empty, but \empty works, too.
(i) simple test:
\newcommand{\commandname}[1]{%
\def\PARAMETER{#1}%
\ifx\PARAMETER\empty
parameter empty
\else
parameter not empty
\fi}
Problem: the place for the command names are wasted.
(ii) test with local command:
\newcommand{\commandname}[1]{%
\begingroup
\def\0{#1}%
\expandafter
\endgroup
\ifx\0\empty
parameter empty
\else
parameter not empty
\fi}
(iii) test with tricky \ifx whithout \empty and temporary command:
\newcommand{\commandname}[1]{%
\ifx\\#1\\%
parameter empty
\else
parameter not empty
\fi}
Yours sincerely
Heiko <oberdiek@ruf.uni-freiburg.de>
PS: A complicated solution with extensive use of optional arguments.
Nesting of arguments causes a problem:
\commandA[\commandB[optB]]
The FIRST closing ] is the end of the optional argument of \commandA
UNLIKE curly braces!
==> {} needed:
\commandA[{\commandB[optB]}]
\newcommand{\optsubscript}[2][]{#2%
\ifx\\#1\\%
\else_{#1}%
\fi}
\newcommand{\protectopt}[1]{%
\ifx\\#1\\%
\else{#1}%
\fi}
\renewcommand{\Set}[2][]{\ensuremath{%
\optsubscript[\protectopt{#1}]{\mathsf{#2}}}\xspace}
\renewcommand{\Mname}[2][]{\ensuremath{%
\optsubscript[\protectopt{#1}]{\mathbf{#2}}}\xspace}
\renewcommand{\INMSet}[2][]{\ensuremath{\Set[\protectopt{#1}]{#2}^{\text{in}}}\xspace}
Technical Details
Message-ID:
<34f5ffa7.422922@news.uni-freiburg.de>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<34F5934A.11E96BF5@univ-pau.fr>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f9e16,e576764e3b867f51 X-Google-Attributes: gidf9e16,public X-Google-Thread: 10ee61,e576764e3b867f51 X-Google-Attributes: gid10ee61,public From: oberdiek@ruf.uni-freiburg.de (Heiko Oberdiek) Subject: Re: problems with optional parameters... Date: 1998/02/27 Message-ID: <34f5ffa7.422922@news.uni-freiburg.de> X-Deja-AN: 329020981 References: <34F5934A.11E96BF5@univ-pau.fr> Organization: Rechenzentrum der Universitaet Freiburg, Germany Newsgroups: comp.text.tex,fr.comp.text.tex