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.

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.

share|improve this question
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
add comment

3 Answers

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
\documentclass{article}
\providecommand*{\Title}{\jobname}

\begin{document}
Title is \Title
\end{document}
share|improve this answer
add comment

\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:

\begingroup
\catcode`\w=12
\def\extracthnum#1w#2\relax{#2}%
\xdef\hnum{\expandafter\extracthnum\jobname\relax}%
\endgroup
%
% ...
%
\title{Homework \hnum}

That probably deserves a little unpacking.

\begingroup\catcode`\w=12

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.)

\def\extracthnum#1w#2\relax{#2}%

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.

\xdef\hnum{\expandafter\extracthnum\jobname\relax}%

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.

share|improve this answer
add comment

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

<string><number>.tex

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

\input{hw-common}

\begin{document}
\title{Homework \NUMBER}
\author{Georg Raba}
\date{}

\maketitle

Solve the following exercises

...

\end{document}

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

\usepackage{expl3,l3regex}

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

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.

share|improve this answer
    
But can you do it without LaTeX3 packages? –  Zack 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
add comment

Your Answer

 
discard

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.