If I name a LaTeX file hw1, I want to print Homework 1. How can I do this?

I can print the string hw1 directly using \jobname, but I don't want spaces in my file name or have to type that out each time I make a new file.

  • 2
    Please provide a little more detail on how you use \jobname in your title? For example, why don't you just type Homework 1 rather than referring to \jobname? – Werner Jan 19 '12 at 20:57

Let's make this more general. I assume you have files named


where <string> are arbitrary alphabetic characters and <number> is made of digits. For instance,

hw01.tex ... hw10.tex

for homeworks or

lect01.tex ... lect21.tex

for lectures. The structure of the hw file might be


\title{Homework \NUMBER}
\author{Georg Raba}


Solve the following exercises



where hw-common.tex will contain the \documentclass line and other common settings for the homework files. One of the tasks of hw-common should be to define \NUMBER based on the file name. But you want to reuse the code and so make it independent of the actual <string> in the file name, so you can use the same code for lect-common.tex.

How can we do? The l3regex package in the LaTeX3 bundle comes, as usual, to the rescue.

In hw-common.tex you'll have


% Save a copy of \jobname
\tl_set_eq:NN \NUMBER \c_job_name_tl
\regex_replace_once:nnN { [A-Za-z0]* } { } \NUMBER

In this way, if the file is hw01.tex, \NUMBER will print 1, stripping the leading zero. The regular expression matches all initial alphabetic characters and zeroes and replaces them with nothing.


As of September 2015 the code should become


% Save a copy of \jobname
\tl_set:NV \NUMBER \c_sys_jobname_str
\regex_replace_once:nnN { [A-Za-z0]* } { } \NUMBER
  • But can you do it without LaTeX3 packages? – zwol Apr 12 '13 at 14:40
  • @Zack Doing it with this generality is really hard. One could think to look for digits, instead of letters, since those are the important things, and, upon finding one, stopping the search and extracting the number. With regular expressions the file name structure can be almost arbitrary. – egreg Apr 12 '13 at 15:16

\jobname is like \string: it expands to a sequence of category code 12 characters, even if the characters in question would not normally have catcode 12 (except spaces, which continue to be catcode 10). You can use delimited arguments to extract a piece of the sequence and do something with it; the easiest "something" is to save them in a macro. For instance, suppose you know that your \jobname will always have the form XXXXwYYYY where the YYYY is the piece you want, you could do this:

% ...
\title{Homework \hnum}

That probably deserves a little unpacking.


Begin a group (you pretty much always want to wrap catcode changes in a group) and change the category code of w to 12 so that it will match the w in the expansion of \jobname. Note that this means we cannot use w in macro names until we revert it to its normal catcode. (There are ways to get around this if you really have to, but let's not get into that.)


Define \extracthnum as a macro taking two delimited arguments. Unlike normal macros, this one's first argument will be all the input characters up to but not including the first catcode-12 w (that isn't inside a brace group), and the second argument will be all the characters after the w until the first occurrence of \relax. (We use a control sequence to delimit the second argument because it cannot possibly occur in the expansion of \jobname. It will be consumed and discarded; I used \relax because this is traditional, but it could be any control sequence. It doesn't even need to have a definition.) It expands to its second argument.


This line is processed in an unusual order, because of the \xdef and the \expandafter. \jobname is expanded first, then \extracthnum ... \relax acts upon what \jobname expands to, and only then does the macro definition happen. Since by assumption \jobname has the form XXXXwYYYY, \hnum will be defined as YYYY. The other special effect of \xdef is to make the definition globally, i.e. it will survive the \endgroup that's coming up next.


You can use \def on the command line to specify the value, and use that in your code.

pdflatex "\def\Title{Homework 1}\input{hw1}"

Your hw1 file would use \providecommand to specify \Title if it was not specified on the command line. Now you can use \Title anywhere within the document:

%\def\Title{Homework 1} -- This is defined on the command line

Title is \Title

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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