2

Suppose I want to test an \ifSomething conditional that is defined by a package which may or may not be loaded. I know how to do this in plain LaTeX, but I'd like to define an expl3 conditional to do the same job. How would I go about doing this?

The MWE illustrates the case with the bidi package and its \if@RTL conditional.

% !TeX program = xelatex
\documentclass{article}
\usepackage{xparse}
\usepackage{bidi}

\makeatletter
% traditional method
\newcommand{\TestRTL}{%
  \@ifundefined{if@RTL}{NO BIDI}%
  {%
    \if@RTL%
    YES%
    \else
    NO%
    \fi%
  }%
}

\ExplSyntaxOn

% non-working first attempt
\prg_new_conditional:Nnn \stdt_if_rtl: {p,T,F,TF}
{
  \bool_lazy_and:nnTF {\bool_if_exist_p:N \if@RTL}  {\bool_if_p:N \if@RTL}
  { \prg_return_true: }
  { \prg_return_false: }
}

\NewDocumentCommand{\TestRTLAlt}{}{
  \stdt_if_rtl:TF {YES} {NO}
  }

\ExplSyntaxOff
\makeatother

\begin{document}

\TestRTL % \TestRTLAlt

\begin{RTL}
  \TestRTL % \TestRTLAlt
\end{RTL}

\end{document} 
4
  • Currently we don't have an interface, but Frank has raised this ...
    – Joseph Wright
    Nov 13, 2018 at 14:28
  • The "traditional" markup that you show is somewhat fragile, it can not be nested inside \iftrue ... \fi for example. Nov 13, 2018 at 14:34
  • @David Carlisle, I know. It was just a quick illustration for the MWE.
    – Karl Hagen
    Nov 13, 2018 at 14:35
  • 2
    OK, although it points to possible difficulties in making a "clean" l3 interface to this, at some point you have to know that tokens that are \let to a primitive \if are parsed differently. Something should be possible though:-) Nov 13, 2018 at 14:39

1 Answer 1

3

After some experimentation, this appears to do the trick.

% !TeX program = xelatex
\documentclass{article}
\usepackage{xparse}
\usepackage{bidi}

\makeatletter
\ExplSyntaxOn

\prg_new_conditional:Nnn \stdt_if_rtl: {p,T,F,TF}
{
  \bool_lazy_and:nnTF {\bool_if_exist_p:N \@RTLtrue}  {\cs_if_eq_p:NN \if@RTL \iftrue}
  { \prg_return_true: }
  { \prg_return_false: }
}

\NewDocumentCommand{\TestRTLAlt}{}{
  \stdt_if_rtl:TF {YES~IT~IS} {NO~IT~ISN'T}
}

\ExplSyntaxOff
\makeatother

\begin{document}
\TestRTLAlt

\begin{RTL}
  \TestRTLAlt
\end{RTL}
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .