11

I have some code that parses \jobname, but it's not working as expected. I've included a minimal example below where the file name is HelloWorld.tex. When I print the \jobname, it comes out as "HelloWorld". When I compare it with the string "HelloWorld" using, for example, xstring's \IfStrEq, they are not equal. Why?

I'm running on OSX using the MacTeX version of TeX Live.

\documentclass{article}
\usepackage{xstring}

\begin{document}
\noindent
.HelloWorld.\\
.\jobname.\\
\IfStrEq{\jobname}{HelloWorld}{Yes}{No}\\

\end{document}

The output when this is compiled is

.HelloWorld.

.HelloWorld.

No

  • In Windows 8.1 64bit my output is: .HelloWorld. .texstudio_gS2832. No – Giacomo Alessandroni May 7 '15 at 8:21
  • 1
    @GiacomoAlessandroni, to compare, you need to replace HelloWorld by the same file name you are compiling. – Sigur May 7 '15 at 8:23
  • @Sigur, Ok I have see now: "C:\Program Files (x86)\Adobe\Reader 11.0\Reader\AcroRd32.exe" "C:\Users\Utente\AppData\Local\Temp\texstudio_gS2832.pdf" – Giacomo Alessandroni May 7 '15 at 8:24
  • @Sigur Now I have the same result and... unfortunately I have not an answer. – Giacomo Alessandroni May 7 '15 at 8:28
10

The \jobname primitive is a TeX string, so all characters have category code 12 (other) not 11 (letter) (with the exception of any spaces, which are category code 10 (space)). Slightly confusingly, xstring is carrying out a token-based comparison here not a string one! Assuming e-TeX is available you can fix your code easily

\documentclass{article}
\usepackage{xstring}

\begin{document}
\noindent
.HelloWorld.\\
.\jobname.\\
\IfStrEq{\jobname}{\detokenize{HelloWorld}}{Yes}{No}\\

\end{document}

There are alternatives available. For example, with any recent TeX engine other than Knuth's TeX you can do a string comparison directly

\documentclass{article}
\usepackage{pdftexcmds}

\begin{document}
\noindent
.test.\\
.\jobname.\\
\makeatletter
\ifnum\pdf@strcmp{\jobname}{HelloWorld}=0 Yes\else No\fi
\makeatother

\end{document}

Sticking to classical TeX, it is possible to set up a comparison by using \meaning to detokenize the content of a macro. That's available in LaTeX as \@onelevel@sanitize

\documentclass{article}

\begin{document}
\noindent
.test.\\
.\jobname.\\
\makeatletter
\edef\@tempa{\jobname}
\def\@tempb{HelloWorld}
\@onelevel@sanitize\@tempb
\ifx\@tempa\@tempb Yes\else No\fi
\makeatother
\end{document}
  • @Addlai I don't tend to use xstring so missed that one: a self-answer would be good. The fact that \jobname is a string is consistent with other parts of TeX and is quite straight-forward I'd say :-) – Joseph Wright May 7 '15 at 8:48
7

I just realized there is another easy solution. The xstring package has starred versions of its comparison commands that do not take category codes into account. All that is necessary to make the file work is to replace \IfStrEq by \IfStrEq*.

  • Thanks so much. I was reading answer after answer without getting anywhere, and this does the trick. – Martin Argerami Nov 3 '18 at 17:28

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.