1

How can I use \str_if_in:nnTF as a boolean function i.e without TF arguments so that I could use it inside

\bool_lazy_and:nnTF
      { \str_if_in:nn {abc} {a} }
      { ... }
      {
        true code
      }{
        false code
      }
9
  • 1
    \str_if_in:nnTF cannot be fully expandable, so it cannot have a predicate form.
    – egreg
    May 11, 2022 at 22:53
  • Use the nnTF one, set a temp variable like tmp1 (name for illustration only), then check bool value of tmp1.
    – user202729
    May 11, 2022 at 23:28
  • You may have a look at functional package. There any predicate function is protected. Therefore you have \StrIfIn as well as \StrIfInTF.
    – L.J.R.
    May 12, 2022 at 0:59
  • @L.J.R. as egreg said for such a boolean test you need an expandable test. Making everything protected in functional breaks such use cases. May 12, 2022 at 7:09
  • @UlrikeFischer This is just my opinion: for programming it is a better choice to make all functions protected and only variables can be expandable. Could you provide an example where \StrIfIn breaks?
    – L.J.R.
    May 12, 2022 at 8:10

2 Answers 2

1

You can use the etl package, which defines a fully expandable form of \tl_if_in:nnTF (named \etl_if_in:nnTF). Please note that the functions of etl are slower than the expl3 equivalents, so you should only use this if you have to be expandable.

\documentclass{article}

\usepackage{etl}
\ExplSyntaxOn
\cs_generate_variant:Nn \etl_if_in:nnTF { oo }
\prg_new_conditional:Npnn \antshar_str_if_in:nn #1#2 {p, TF, T, F}
  {
    \etl_if_in:ooTF { \tl_to_str:n {#1} } { \tl_to_str:n {#2} }
      \prg_return_true:
      \prg_return_false:
  }
\NewExpandableDocumentCommand \antshartest { m m }
  {
    \bool_lazy_and:nnTF
      { !\tl_if_empty_p:n {#1} }
      { \antshar_str_if_in_p:nn {#1} {#2} }
      { true }
      { false }
  }
\ExplSyntaxOff

\begin{document}
\antshartest{}{abc}

\antshartest{def{abc}def}{abc}
\end{document}
5
  • The reason I wanted the predicate dorm is to make my code simpler and shorter, avoiding creating extra functions or temporary variables. But this approach doesn't seem to be any better at this. Anyway it might be helpful for someone else, so thank you for the answer.
    – antshar
    May 12, 2022 at 11:32
  • 1
    @antshar well you didn't state your use case :) But in that case I'd stick with user202729's comment.
    – Skillmon
    May 12, 2022 at 11:51
  • @antshar "Avoiding temporary variable to make code simpler" is exactly what latex3 does not do (because not everything are expandable) (for unexpandable things at least) -- it can't be done without "interpreting" another mini-language like how functional does.
    – user202729
    May 22, 2022 at 8:34
  • Isn't it simpler to just use the _p variant of etl_if_in instead of defining a new one?
    – user202729
    May 22, 2022 at 8:36
  • @user202729 not if you want string comparison, for that you need to stringify the input, so you're either using \etl_if_in_p:oo { \tl_to_str:n {#1} } { \tl_to_str:n {#2} } in every usage or define a new conditional which does that for you.
    – Skillmon
    May 22, 2022 at 10:25
1

Here is an alternative solution with functional package:

\documentclass{article}
\usepackage{functional}
\begin{document}

\IgnoreSpacesOn
\boolVarAndTF {\strIfIn {abc} {a}} {\intCompare {1} < {2}} {
  \prgReturn{True}
} {
  \prgReturn{False}
}
\IgnoreSpacesOff

\end{document}

enter image description here

1
  • 1
    Thanks. I saw your comment, but wanted to stick with pure latex3
    – antshar
    May 12, 2022 at 12:18

You must log in to answer this question.

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