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

enter image description here

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?

share|improve this question

1 Answer 1

up vote 9 down vote accepted

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.

enter image description here

The terminal will stop at \show\foo with

> \foo=macro:
->Änderungen.
l.20 \UTFjobname[\foo]\show\foo
share|improve this answer

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.