Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question
add comment

2 Answers

up vote 9 down vote accepted

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}
share|improve this answer
    
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... –  Peter Grill May 30 '11 at 6:43
add comment

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.

share|improve this answer
    
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. –  Peter Grill May 30 '11 at 6:45
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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