6

Should not the name \jobname expand to the name of the file without the extension?

I looked at

It says that show results in the meaning of a macro. Does my result imply that \jobname is not a macro?

Code

\documentclass{article}
\begin{document}
\makeatletter
\show\jobname
\makeatother
\end{document}

Ouput:

> \jobname=\jobname.
l.4 \show\jobname

2 Answers2

10

\jobname is not a macro (essentially it's not defined with \def) it is an expandable primitive that expands to the filename.

You can make a macro with the same expansion

\edef\xjobname{\jobname}

*\show\xjobname
> \xjobname=macro:
->texput.

so \xjobname has the same expansion as \jobname but they have different \show behaviour and crucially they are not equal if tested with

\ifx\jobname\xjobname

but two macros with the same expansion are \ifx equal.

David Carlisle
  • 757,742
5

Typically that's the way TeX primitives react. From the TeXbook (Chapter 3: Controlling TeX, p 10):

How can a person distinguish a TeX primitive from a control sequence that has been defined at a higher level? There are two ways: (1) The index to this manual lists all of the control sequences that are discussed, and each primitive is marked with an asterisk. (2) You can display the meaning of a control sequence while running TeX. If you type \show\cs where \cs is any control sequence, TeX will respond with its current meaning. For example, \show\input results in > \input=\input., because \input is primitive. On the other hand, \show\thinspace yields

> \thinspace=macro:
->\kern .16667em .

This means that \thinspace has been defined as an abbreviation for \kern .16667em. By typing \show\kern you can verify that \kern is primitive. The results of \show appear on your terminal and in the log file that you get after running TeX.

Commands that are declared in a robust way have a similarly bizarre output even though they are declared as a "macro". For example, \show\vspace prints

> \vspace=macro:
->\protect \vspace  .

in the terminal.

Some primitives can be expanded and there is also a way around peeking inside the definition of robust commands. For those primitives that cannot be expanded, you'd have to look into TeX The Program - the .web code or its documentation - in order to understand what it's doing.

Using David's example that input the following in the TeX terminal (no filename specified):

\edef\xjobname{\jobname}

*\show\xjobname
> \xjobname=macro:
->texput.

528. Initially job_name = 0; it becomes nonzero as soon as the true name is known. We have job_name = 0 if and only if the log file has not been opened, except of course for a short time just after job name has become nonzero.

534. The open_log_file routine...

[...]
if job_name = 0 then job_name ← "texput";
[...]

Werner
  • 603,163
  • ...this is a minor supplement to David's answer. – Werner Jan 21 '16 at 23:30
  • Pretty hefty supplement I'd say. I almost gave the answer to you for writing a more detailed, time-consuming explanation. Nonetheless, David's also provides useful information. I'd be interested in understanding how to use \jobname in xstring's \IfEq{}{}{}{}, for example, where it needs to be expanded at a very specific time in order to be true, like why \IfEq{\jobname}{<nameoffile>}{TRUE}{FALSE} does not become TRUE. It seems \jobname can be expanded, but only sometimes (when the log is written-if I understand you correctly.) – Jonathan Komar Jan 22 '16 at 07:48
  • @macmadness86: As David says, \jobname has to be expanded. Try with \expandafter\IfEq\expandafter{\jobname}{<nameoffile>}{TRUE}{FALSE}... When the .log is opened... – Werner Jan 22 '16 at 07:51
  • That code is still not expanding it at the right time, but I verified that the strings are equal with \typeout{**** \jobname}. Filename=test.tex and my code was therefore \expandafter\IfEq\expandafter{\jobname}{test}{TRUE}{FALSE} Result: FALSE, typeout shows **** test. This is baffling me a bit, and because \IfEq\expandafter{\jobname}{test}{TRUE} results in TRUEFALSE – Jonathan Komar Jan 22 '16 at 08:12
  • 1
    @macmadness86 beware that \jobname produces catcode 12 tokens, I don't know how \IfEq of xstring acts. To produce on your own catcode 12 "letters" (for comparison), something like \expandafter\expandafter\expandafter\@gobble\expandafter\string\csname FOOBAR\endcsname (assuming \escapechar is standard) is a possibility. –  Jan 22 '16 at 08:44
  • "supplement" translating to "I'm learning from egreg how to try to steal ticks by writing second answers ten times longer than the first posted answer":-) – David Carlisle Jan 22 '16 at 09:47
  • @DavidCarlisle It was a nice gesture by Werner to recognize you, being as you have a mere 302k points. Your answer sticks to the point and I recognize that. I do, generally speaking, like longer, more informative answers because I like to understand the relationships between things-I must admit. His answer did almost steal the gold metal-egreg style :-) Better luck next time, Werner. – Jonathan Komar Jan 22 '16 at 10:28
  • @jfbu Excellent comment about the catcodes. It led me to here: http://tex.stackexchange.com/questions/117892/can-i-convert-a-string-to-catcode-11, which I think is relevant to mention here for future reference. – Jonathan Komar Jan 22 '16 at 11:31