12

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

4
  • In Windows 8.1 64bit my output is: .HelloWorld. .texstudio_gS2832. No Commented May 7, 2015 at 8:21
  • 1
    @GiacomoAlessandroni, to compare, you need to replace HelloWorld by the same file name you are compiling.
    – Sigur
    Commented May 7, 2015 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" Commented May 7, 2015 at 8:24
  • @Sigur Now I have the same result and... unfortunately I have not an answer. Commented May 7, 2015 at 8:28

2 Answers 2

12

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}
1
  • @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
    Commented May 7, 2015 at 8:48
8

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*.

1
  • Thanks so much. I was reading answer after answer without getting anywhere, and this does the trick. Commented Nov 3, 2018 at 17:28

You must log in to answer this question.

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