2

I have a string check macro \IfBeginsWith from xstring that is wrapped in a non-user level macro. I have learned here that the wrong approach would be to use user-level commands in a wrapper that no user sees. I should use either low-level TeX stuff (which is beneficial for the sake of understanding the fundamentals) or LaTeX 3 syntax.

I would like to check whether or not the filename starts with UserManual, DVD, or Interface. If it does, then I should do something (e.g. redefine a macro to true), otherwise I should do something else (e.g. redefine a macro to false, although it is probably the default setting anyway).

Just doing it using the semi user-level way:

Since \jobname exports catcode 12 tokens (which makes sense since it must expand to any supported filesystem characters that are not escaped) and not catcode 11 tokens, it is just easy to convert both sides of the equation with catcode 12 with \detokenize (converts spaces to catcode 10 and characters to 12).

\IfBeginWith{\detokenize\expandafter{\jobname}}{\detokenize\expandafter{DVD}}{TRUE}{FALSE}

Code

\documentclass{article}
\usepackage{xparse}

\def\whatever{false}
\NewDocumentCommand{\setwhatever}{m}{\def\whatever{#1}}

\ExplSyntaxOn
  \tl_new:N \g_jobname_tl
  \tl_gset:Nn \g_jobname_tl {\jobname}
  \IfBeginWith{\g_jobname_tl}{UserManual} %something like \str_case_beginswith:nn
    {\setwhatever{true}}
    {\setwhatever{false}}
  \IfBeginWith{\g_jobname_tl}{DVD} %something like \str_case_beginswith:nn
    {\setwhatever{true}}
    {\setwhatever{false}}
  \IfBeginWith{\g_jobname_tl}{Interface} %something like \str_case_beginswith:nn
    {\setwhatever{true}}
    {\setwhatever{false}}
\ExplSyntaxOff
\begin{document}
\whatever{}
\end{document}
1

There is no “if token list begins with” predefined function. You can build it easily with l3regex:

\documentclass{article}
\usepackage{xparse,l3regex}

\ExplSyntaxOn
\DeclareExpandableDocumentCommand{\whatever}{}
 {
  \tl_use:N \g_macmadness_whatever_tl
 }

\tl_new:N \g_macmadness_whatever_tl

\cs_generate_variant:Nn \regex_match:nnTF { nV }
\regex_match:nVTF { \A (UserManual|DVD|Interface) } \c_sys_jobname_str
 {
  \tl_gset:Nn \g_macmadness_whatever_tl {true}
 }
 {
  \tl_gset:Nn \g_macmadness_whatever_tl {false}
 }
\ExplSyntaxOff

\begin{document}

\whatever{}

\end{document}

Note that \jobname is already available as the string \c_sys_jobname_str, so you don't need to allocate a variable for it.

The token \A anchors the search string at the start; the search is for one among UserManual, DVD or Interface and the token list is set accordingly to whether the match was found.

  • It would seem \regex_match:nnTF is a more recent addition to LaTeX 3 than the version I have because I am getting LaTeX has been asked to use a command \regex_match:nnTF, but this has not been defined yet. – Jonathan Komar Jan 19 '16 at 10:24
  • @macmadness86 You need to update – egreg Jan 19 '16 at 10:40

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.