1

I have latex files with file names T-BA-DEC-de-0001_Title1.tex and T-BA-DEC-de-0002_Text2.tex. I would like to switch code depending on these file names. Up to now I tried to solve the problem using \ifthenelseand with the xstring package:

\usepackage{xstring}

\IfBeginWith{\detokenize\expandafter{\c_sys_jobname_str}}{\detokenize{T-BA-DEC-de-0001}}{TRUE}{FALSE}
\IfBeginWith{\detokenize\expandafter{\c_sys_jobname_str}}{\detokenize{T-BA-DEC-de-0002}}{TRUE}{FALSE}

Whatever I tried, the \IfBeginWith always runs into the FALSE condition or creates runtime errors. What am I doing wrong?

  • You seem to be mixing expl3 and xstring, which seems a bit odd (though \jobname is a bit strange, admittedly). Could you show us a complete example: with \ExplSyntaxOn added the above does work for me. – Joseph Wright May 13 at 9:27
2

As you are using expl3 for \c_sys_jobname_str, I'd be minded to go the whole way and simply code up a quick 'starts with' test myself. Something like

\documentclass{article}
\usepackage{expl3}
\ExplSyntaxOn
\cs_new_protected:Npn \WolfiG_test:nnTF #1#2
  { \exp_args:Ne \__WolfiG_test:nn { \tl_to_str:n {#1} } {#2} }
\cs_generate_variant:Nn \WolfiG_test:nnTF { nV }
\cs_new_protected:Npn \__WolfiG_test:nn #1#2
  {
    \cs_set:Npn \__WolfiG_test:w ##1 #1 ##2 \q_stop
      { \tl_if_blank:nTF {##1} }
    \__WolfiG_test:w #2 #1 \q_stop
  }
\cs_new:Npn \__WolfiG_test:w { }
\WolfiG_test:nVTF { T-BA-DEC-de-0001 } \c_sys_jobname_str \T \F
\WolfiG_test:nVTF { T-BA-DEC-de-0002 } \c_sys_jobname_str \T \F

Or if you want to use 'generic string' functions

\documentclass{article}
\usepackage{expl3}
\ExplSyntaxOn
\cs_new_protected:Npn \WolfiG_test:nnTF #1#2
  {
    \str_if_eq:eeTF {#1} { \str_range:nnn {#2} { 1 } { \str_count:n {#1} } }
  }
\cs_generate_variant:Nn \WolfiG_test:nnTF { nV }
\WolfiG_test:nVTF { T-BA-DEC-de-0001 } \c_sys_jobname_str \T \F
\WolfiG_test:nVTF { T-BA-DEC-de-0002 } \c_sys_jobname_str \T \F

where I've used \str_range:nnn to extract a substring from #2 (the job name) that is the same length as #1 (the test string), then compared the two.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.