[texhax] question about \if

Heiko Oberdiek heiko.oberdiek at googlemail.com
Fri Nov 12 12:57:11 CET 2010


On Fri, Nov 12, 2010 at 10:30:17PM +1100, Vafa Khalighi wrote:

> To be more specific, this is what I have in mind:
> 
> \newcommand{\setmaindir}[1]{\if#1RTL\pagedir TRT\else \if#1LTR\pagedir
> TLT\fi\fi}
> 
> so that
> 
> \setmaindir{RTL} gives  \pagedir TRT and
> \setmaindir{LTR} gives \pagedir TLT

\makeatletter
\newcommand*{\my at pagedir@RTL}{\pagedir TRT}
\newcommand*{\my at pagedir@LTR}{\pagedir TLT}
\newcommand*{\setmaindir}[1]{%
  \ifcsname my at pagedir@#1\endcsname
    \csname my at pagedir@#1\expandafter\endcsname
  \else
    \PackageError{...}%
      {Invalid argument `#1' for \string\setmaindir}%
      \@ehc
  \fi
}
\makeatother

Without e-TeX replace \ifcsname by
  \expandafter\ifx\csname ...\endcsname\relax
or without the side effect of defining undefined
commands with meaning \relax:
  \RequirePackage{ltxcmds}
  ...
  \newcommand*{\setmaindir}[1]{%
    \ltx at IfUndefined{}{%
      \csname my at pagedir@#1\endcsname
    }{%
      \PackageError{...}{...}\@ehc
    }%
  }%
or without the package
  \newcommand*{\setmaindir}[1]{%
    \begingroup\expandafter\expandafter\expandafter\endgroup
    \expandafter\ifx\csname my at pagedir@#1\endcsname\relax
      \csname my at pagedir@#1\expandafter\endcsname
    \else
      \PackageError...
    \fi
  }

Yours sincerely
  Heiko Oberdiek


More information about the texhax mailing list