# Encoding of non-ASCII characters (Umlauts) in \jobname

I need to parse \jobname to extract some metadata (such as the subject of a letter) that is encoded in the filename of the .tex file. The filenames are not restricted to ASCII, but maybe any UTF8 charcater. In particular, I have lots of files with German Umlauts in their name. However, compiling:

% Änderungen.tex
\documentclass[]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\begin{document}
\jobname
\end{document}

with pdflatex yields:

With lualatex or xelatex the output is fine; however, I would prefer to stay with pdflatex. So how is \jobname actually encoded and how can I transform it?

-

If you only need to print the jobname, then

\scantokens\expandafter{\jobname\noexpand}

is sufficient. The problem is that all characters in the expansion of \jobname have category code 12, so they aren't interpreted in the correct way for printing. With \scantokens we make TeX reread them, but first we need to expand \jobname. The final \noexpand is for avoiding a spurious space at the end.

If instead you want to store the "retokenized" name in a control sequence, then using LaTeX3 is easier:

\documentclass[]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\UTFjobname}{o}
{
\tl_set_rescan:Nno \l_tmpa_tl { } \c_job_name_tl
\IfNoValueTF{#1}
{ \tl_use:N \l_tmpa_tl }
{ \tl_set_eq:NN #1 \l_tmpa_tl }
}
\ExplSyntaxOff

\begin{document}
\scantokens\expandafter{\jobname\noexpand}X

\UTFjobname X

\UTFjobname[\foo]\show\foo

\end{document}

With only \UTFjobname you print the jobname, with \UTFjobname[\foo] you store the jobname (with the correct category codes) in \foo. The X are there just to show that no space is added when printing.

The terminal will stop at \show\foo with

> \foo=macro:
->Änderungen.
l.20 \UTFjobname[\foo]\show\foo
-