Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I have a bunch of documents called wk01.tex wk02.tex and so on up to (eventually) 20. These are weekly handouts for a course I TA. Currently, each document starts with a \def\classweek{5} or whatever. I then use this in the document header. Now what I'd really like to do is have latex take the document name, do some sed-fu to strip out just the number (without its initial 0 if applicable) and then use this number in the definition of \classweek automatically. I've spotted reference to \jobname once or twice, but I'm not sure how to use that.

Is what I want to do possible?

[Are there tags appropriate for this sort of question? I couldn't think of any...]

share|improve this question
\jobname expands to the file base, e.g. wk01 for wk01.tex. But be careful, all non-space tokens in it have catcode 12 "other", which makes comparisons a little tricky. But for numbers this shouldn't be a problem, they have catcode 12 anyway and TeX registers don't look at the catcodes. –  Martin Scharrer Feb 21 '11 at 18:52

4 Answers 4

up vote 13 down vote accepted

I would endeavour to only have one set-up macro to simplify the author interface. In this case


Here is a minimal,

%use this to typeset the header
share|improve this answer
Nice solution! Could you explain why we need the \expandafter and \fname\jobname would be insufficient? –  Christian Lindig Feb 21 '11 at 19:40
You need \expandafter because when TeX is collecting the parameters #1, #2, etc. for \fname it doesn't expand them, so the #1 would become \jobname, instead of the w from wk02. –  Phil Hirschhorn Feb 21 '11 at 20:35
Nice answer! I do pretty much the same thing with numbered quizzes. –  Jan Hlavacek Feb 22 '11 at 5:43
@Yiannis could you explain the use of \expandafter in your answer? I think it would be good if answers on this site helped people learn about TeX and friends rather than just offered code snippets. Thanks! –  Seamus Feb 22 '11 at 8:54
@Seamus good suggestion will do it a bit later when I get a chance. –  Yiannis Lazarides Feb 22 '11 at 9:39

The following depends on the file name being wkxx.tex, where each of those x's is a digit:

\ifnum\first = 0\relax
share|improve this answer
I think the stringstrings package provides a macro to get certain parts of a string. Hopefully it allows to get the last n characters. This way it is more robust. –  Martin Scharrer Feb 21 '11 at 19:01

This is almost exactly what I do with my lecture notes. My code is contained in my answer to the question Changing LaTeX headers via a makefile and is explained in my answer to How to use the exact same file for handout and presentation modes in beamer.

(I'm mainly answering here to get these questions linked together as they are similar enough that someone interested in one will find the answers to the others relevant. In this specific case, the already-given answers are more than adequate, but someone else looking at this question might not have exactly the same set-up but still be able to figure out what to do by looking at these questions.)

share|improve this answer

A version that doesn't require to know the name of the file, as long as it is of the form

<characters><two-digit number>.tex

So job names like wk02.tex or week35.tex would give 2 and 35 respectively:

\cs_new:Npx \classweek
  \int_eval:n { \tl_item:Nn \c_job_name_tl { -2 } \tl_item:Nn \c_job_name_tl { -1 } }

In other words the macro \classweek will be defined as the number formed from the last two characters in the file name.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.