7

I am having difficulty in using xtring to check the file name of the .tex file.

All three of the cases shown below produce the wrong output. Surprisingly in my actual more complicated usage, the first two of these work just fine, so I am really confused as to why I am unable to get this simple example to work. The following needs to be saved as "FileName-A-B.tex", and as far as I am concerned it should have printed out the first part of the 'If statements and not the else part.

I want to check the portions between the dashes in the file name.

\documentclass{article}
\usepackage{xstring}

\begin{document}

The .tex file name is "\jobname" \newline

% Check that the file name begins with "FileName-"
\IfBeginWith{\jobname}{FileName-}{
    Yes, file name does begin with [FileName]
}{
    Error: File name does NOT begin with [FileName]
}

% Check that the file name ends with "B"
\IfEndWith{\jobname}{-B}{
    Yes, file name ends with "B"
}{
    Error: File name does NOT end with [B]:
}

%Now check what that there is an "A" in between the dashes
\StrBetween[1,2]{\jobname}{-}{-}[\mystring]%
\IfStrEq{\mystring}{A}{
    Yes, file has an [A] in the middle
}{
    Error: File name does NOT have an [A] in the middle,
it has [\mystring] instead.
}

\end{document}

2 Answers 2

11

The \jobname command return 12-catcode chars and 10-catcode for space. You must detokenize the arguments before manipulate them with xstring to match the catcode. This can be done with a "*" after the xstring commands (read the manual for further explanations):

\documentclass{article}
\usepackage{xstring}

\begin{document}

The .tex file name is "\jobname" \newline

% Check that the file name begins with "FileName-"
\IfBeginWith*{\jobname}{FileName-}{
    Yes, file name does begin with [FileName]
}{
    Error: File name does NOT begin with [FileName]
}

% Check that the file name ends with "B"
\IfEndWith*{\jobname}{-B}{
    Yes, file name ends with "B"
}{
    Error: File name does NOT end with [B]:
}

%Now check what that there is an "A" in between the dashes
\StrBetween*[1,2]{\jobname}{-}{-}[\mystring]%
\IfStrEq*{\mystring}{A}{
    Yes, file has an [A] in the middle
}{
    Error: File name does NOT have an [A] in the middle,
it has [\mystring] instead.
}
\end{document}
1
  • This works great. Thanks. I am surprised that the first two cases were working for me in my real usage. The only difference was that I was comparing to values stored in \newtoks so that must be it... May 30, 2011 at 6:43
5

The string returned by \jobname uses the catcode other for all characters including letters except for the spaces which are still in catcode space. Most string-compare code compares also the catcodes so the tests will not be true even when the ASCII content is the same.

You could use \@onelevel@sanitize\somestring first to set the catcode of \somestring as they are for \jobname before you compare it.

1
  • Since I am just checking the file names (which I define), I think I am safe using the starred version as per the solution provided by @unbonpetit. May 30, 2011 at 6:45

You must log in to answer this question.

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