3

There are several solutions both integrated in TeX and proposed on this side for checking if a substring can be found in a given string (such as IfSubStr, IfStringInList and others). As for example, \instringTF (as defined here:https://tex.stackexchange.com/a/26873/61517) when used as

\instringTF{graph}{graphs}{We have data}{We do not have data}

gives

We have data

Now, I would like to extend that to several possible substrings, i.e. to a call such as

\instringTF{graph,images}{graphs}{We have data}{We do not have data}

This should still evaluate to

We have data

while

\instringTF{graph,images}{image}{We have data}{We do not have data}

should evaluate to

We do not have data

Are there solutions for that issue, too?

Edit: Peter Grill suggested using a solution from https://tex.stackexchange.com/a/350818/61517. I implemented it, resulting in:

\documentclass[%
reprint,
amsfonts,
floatfix,
amsmath,amssymb,
aps,
pra,
]{revtex4-2}
\usepackage{graphicx}% Include figure files
%\usepackage{dcolumn}% Align table columns on decimal point
%\usepackage{bm}% bold math
\usepackage{todonotes}
%\usepackage{siunitx}
%\usepackage{caption}
\usepackage{letltxmacro}
\usepackage{xstring}
\usepackage[labelformat=simple]{subcaption}
\usepackage{float}
\usepackage{xparse}
\usepackage{substr}
\makeatletter
\renewcommand{\todo}[2][]{\tikzexternaldisable\@todo[#1]{#2}\tikzexternalenable}
\makeatother

\usepackage{hyperref}

\ExplSyntaxOn \cs_new_protected:Nn \grill_str_multicase:nnTF { \seq_set_split:Nnn \l__grill_str_multicase_cases_seq { } { #2 } \tl_clear:N \l__grill_str_multicase_cases_tl \int_step_inline:nnnn { 1 } { 2 } { \seq_count:N \l__grill_str_multicase_cases_seq } { \seq_set_split:Nnx \l__grill_str_multicase_subcases_seq { } { \seq_item:Nn \l__grill_str_multicase_cases_seq { ##1 } } \seq_map_inline:Nn \l__grill_str_multicase_subcases_seq { \tl_put_right:Nx \l__grill_str_multicase_cases_tl { {\exp_not:n{####1}}{\seq_item:Nn \l__grill_str_multicase_cases_seq { ##1 + 1}} } } } \str_case:nVTF { #1 } \l__grill_str_multicase_cases_tl { #3 } { #4 } } \cs_generate_variant:Nn \seq_set_split:Nnn { Nnx } \cs_new_protected:Nn \grill_str_multicase:nn { \grill_str_multicase:nnTF { #1 } { #2 } { } { } } \cs_new_protected:Nn \grill_str_multicase:nnT { \grill_str_multicase:nnTF { #1 } { #2 } { #3 } { } } \cs_new_protected:Nn \grill_str_multicase:nnF { \grill_str_multicase:nnTF { #1 } { #2 } { } { #3 } }

\NewDocumentCommand{\IfStringCaseX}{mmo} { \IfNoValueTF{#3} { \grill_str_multicase:nn { #1 } { #2 } } { \grill_str_multicase:nnF { #1 } { #2 } { #3 } } } \ExplSyntaxOff

\newcommand{\ConditionalText}[1]{% \IfStringCaseX{#1} { {{images}{graphs}} {% We have images/graphs } }[{Error: Unknown parameter ``#1'' to ConditionalText}]% }

\makeatother \begin{document} \ConditionalText{images/subx.tikz}

\ConditionalText{images}

\end{document}

Here, the first line containing

\ConditionalText{images/subx.tikz}

produces Unknown parameter, while the second line

\ConditionalText{images}

produces

We have images/graphs

i.e. the proposed solution does not work for my use case.

arc_lupus
  • 1,781

1 Answers1

4

Map over the list of possible substring; if a match is found, break the mapping by setting the conditional to true.

Then check whether the conditional is true or false.

\documentclass{article}

\ExplSyntaxOn

\NewDocumentCommand{\instringTF}{mmmm} {% #1 = list of possible substrings, #2 = string, #3 = true text, #4 = false text \arclupus_instring:nnnn { #1 } { #2 } { #3 } { #4 } }

\bool_new:N \l_arclupus_instring_bool

\cs_new_protected:Nn \arclupus_instring:nnnn { \bool_set_false:N \l_arclupus_instring_bool \clist_map_inline:nn { #1 } { \str_if_in:nnT { #2 } { ##1 } {% ##1 is a substring, no need to check further \clist_map_break:n { \bool_set_true:N \l_arclupus_instring_bool } } } \bool_if:NTF \l_arclupus_instring_bool { #3 } { #4 } }

\ExplSyntaxOff

\begin{document}

\instringTF{graph}{graphs}{We have data}{We do not have data}

\instringTF{graph,images}{graphs}{We have data}{We do not have data}

\instringTF{graph,images}{image}{We have data}{We do not have data}

\end{document}

enter image description here

egreg
  • 1,121,712