5

My question is originally the same as use jobname to "pass parameters"?

But that particular question doesn't seem to have a complete answer. (I'm sure that in the minds of experts who deeply know the issue, it is solved.)

Looking at a few other related threads,

How can I check if the filename of a LaTeX document contains a string?

\jobname, character codes and \detokenize

I've been able to largely solve my original question using \IfSubStringInString but not using \ifdefstring. See the example code below.

% run this with pdflatex -jobname=hello thissourcecode.tex
\documentclass{article}
\usepackage{etoolbox}
\usepackage{substr}
\begin{document}
jobname is: ``\jobname''.

% The following gives "true"
\IfSubStringInString{\detokenize{hello}}{\jobname}{substr true}{substr false}

% The following gives "false"
\ifdefstring{\detokenize{hello}}{\jobname}{etoolbox true}{etoolbox false}
\end{document}

The solution with \IfSubStringInString isn't bad at all, except that the package doesn't seem to include a command for exact match. I'd also love to get \ifdefstring to work with \jobname.

1 Answer 1

4

You can do it with etoolbox (and a recent release of LaTeX).

But it's better to do it without it; I provide a generic string equality comparison and one specific for comparison with \jobname.

\documentclass{article}
\usepackage{etoolbox}

\ExplSyntaxOn
\NewExpandableDocumentCommand{\stringsequalTF}{mmmm}
 {
  \str_if_eq:eeTF { #1 } { #2 } { #3 } { #4 }
 }
\NewExpandableDocumentCommand{\stringisjobnameTF}{mmm}
 {
  \str_if_eq:nVTF { #1 } \c_sys_jobname_str { #2 } { #3 }
 }
\ExplSyntaxOff

\setlength{\parindent}{0pt} % just for the example

\begin{document}

jobname is: ``\texttt{\jobname}''

\section{With \texttt{etoolbox}}

\ExpandArgs{ee}\ifstrequal{\jobname}{\detokenize{hello}}{true}{false}

\ExpandArgs{ee}\ifstrequal{\jobname}{\detokenize{ryo}}{true}{false}

\section{Without \texttt{etoolbox}}

\subsection{With \texttt{\string\stringsequalTF}}

\stringsequalTF{\jobname}{hello}{true}{false}

\stringsequalTF{\jobname}{ryo}{true}{false}

\subsection{With \texttt{\string\stringisjobnameTF}}

\stringisjobnameTF{hello}{true}{false}

\stringisjobnameTF{ryo}{true}{false}

\end{document}

enter image description here

3
  • @egrep I knew you have a solution! But can you elaborate on the \ExpandArgs bit? Quick search on Google didn't show an answer that can be understood by a non-expert like me. Anyway, do you mean that this is the future way to compare strings, to always precede the comparison with \ExpandArgs? Will you or somebody publish a package that includes this string comparison? Also, why can't a macro like this automatically detokenize its arguments? so that \ifstrequal{\jobname}{hello}{ . . .}{ . . .} always works?
    – Ryo
    Feb 1 at 18:19
  • @Ryo etoolbox was born as a companion package to biblatex and its functions are tailored for those needs. It's not possible to forecast whatever functions user will need in the future, but LaTeX can be extended, as my answer shows. About \ExpandArgs, read texdoc usrguide.
    – egreg
    Feb 1 at 18:23
  • (Sorry I misspelt your name last time because I use the unix command "egrep" daily :-) I didn't mean "future" that way. The need is now! We right now need a function to compare two strings and branch, depending on the result of the comparison. So, the code you have kindly written above should be packaged and made available, in such a way that the user have only to \usepackage to use the simple macro such as \ifstrequal without writing extra code. That's the "future" I'm seeing.
    – Ryo
    Feb 2 at 3:16

You must log in to answer this question.

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