2

Yet another question about passing parameters when compiling. I thought I had a good (for me) solution, by compiling with different jobnames and do conditional compilation on the basis of the jobname, using etoolbox and \ifdefstring; but it does not work. What's wrong with the following code? It seems that the \ifdefstring always evaluate to false.

%
% Save this file as A.tex
% compile it with 'latex A.tex'
% compile it again with 'latex -jobname B A.tex'
% you end up with A.dvi and B.dvi.
%
\documentclass{article}
\usepackage{etoolbox}

\begin{document}
The current jobname is: \jobname

\ifdefstring{\jobname}{A}{This file should be named A.dvi}%
{This file should be named B.dvi}

\end{document}

EDIT: Actually I did not understand the proposed answer (and I tried it unsuccesfully): I should say I am not a TeXpert, only a LaTeX user. I would like to get an answer in terms of the usage of the package etoolbox, which, as far as I understand, is the implementation bit of such affairs whithin LaTeX3. I wrote the above bit of code using the instructions of etoolbox, which says that \ifdefstring{<command>}{<string>}{<true>}{<false>} executes true or false according to wether the command matches the string. I guessed that the problem was that \jobname is not a command in the sense of LaTeX, and I tried to define \newcommand{\Jobname}{\jobname} to "commandify" it, but it did not work. I think that, since LaTeX users which are not TeXperts browse this forum, it would be nice to have a full LaTeX solution if possible. Thank you all (and sorry for making this required edit so late, I was offline for a couple of days).

Gherardo
  • 317
  • Edited. I tried detokenize as you suggested, it did not work.. – Gherardo Aug 14 '18 at 09:57
  • What's precisely you don't understand? – egreg Aug 14 '18 at 10:05
  • 1
    I do not undesrtand why \ifdefstring{\jobname}{string}{}{} always evaluate to false. I partially understood that there is some distinction between first level and second level expansion, and that maybe jobname is expanded as its macro code, instead of its replacement text. But etoolbox says that it compares only the replacement text. Is the documentation of etoolbox wrong? Maybe this should be the question?

    Is there a way of transforing catcode 12 into catcode 11? I tried the suggested combiantion \edef and \detokenize, unsuccesfully.

    – Gherardo Aug 16 '18 at 10:48
  • @egreg @Gherardo So, what's the answer? How to amend \ifdefstring{\jobname}{abc}{file is abc.pdf}{not abc}? Which string do you \detokenize? Note that the jobname may not be a single character. I've tried using \detokenize on \jobname or abc or both, but all ifdefstrings resulted in false. – Ryo Feb 01 '23 at 04:27

1 Answers1

1

This should work:

\documentclass{article}
\def\jobnameA{A}
\begin{document}
The current jobname is: \jobname

\if\jobnameA\jobname 
  This file should be named A.dvi
\else
  This file should be named B.dvi
\fi
\end{document}
  • 1
    that only works if \jobname is a single character – David Carlisle Aug 08 '18 at 19:38
  • Sure, I thought that he really uses a.tex and B.tex –  Aug 08 '18 at 19:55
  • That seems unlikely, I think that they were just examples:-) – David Carlisle Aug 08 '18 at 20:06
  • Thanks. Yes, they were just examples. But if there are no other solutions I will use single characters :-D But also I still am curious to understand why my code does not work. Apparently \jobname does not expand as I expected inside \ifdefstring. – Gherardo Aug 09 '18 at 05:57
  • @Gherardo the linked question gives a solution. Your definitions are not the same as \def\jobnameA{A} is a catcode 11 (letter} A but \jobname always uses catcode 12 (\edef\jobnameA{\detokenize{A}} probably works (untested) – David Carlisle Aug 09 '18 at 11:00