1

I would like a LaTeX source file to compile in different ways depending on the job name. However, when I run pdflatex mwe with mwe.tex containing

\documentclass{article}
\begin{document}
\ifx{mwe}\jobname
  true
\else
  false.\jobname.
\fi
\def\mwe{mwe}
\ifx\mwe\jobname
  true
\else
  false.\jobname.\mwe.
\fi
\end{document}

the resulting pdf file is "false.mwe. false.mwe.mwe.", while I expected to see "true". What am I doing wrong?

3

1 Answer 1

4

\ifx compares only two tokens. In the first test it compares { and m, and in the second test it compares the macro \mwe and primitive \jobname, which are not exactly the same (even if they expand to the same text).

If you want string comparison, you can use expl3's \str_if_eq:nnTF (using \str_if_eq:eeTF so that it expands macros before comparing):

\documentclass{article}

\ExplSyntaxOn
\cs_new_eq:NN \strcompare \str_if_eq:eeTF
\ExplSyntaxOff

\begin{document}
\strcompare{mwe}{\jobname}
  {true}
  {false.\jobname.}

\def\mwe{mwe}
\strcompare{\mwe}{\jobname}
  {true}
  {false.\jobname.\mwe.}
\end{document}

The document prints true true.

You must log in to answer this question.

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