pdflatex with precompiling does not support .Tex file with Chinese filename

First I am sure, pdflatex can compile Tex file with Chinese file name without problem. For example, the Tex source content is as follows

\documentclass[UTF8,hyperref]{ctexart}
\begin{document}
中文
\end{document}


save it as a file named "中文.tex" and run

pdflatex 中文.tex


everything is OK.

Now, in order to speed up the compilation, we make pre-compilation according to this link. That is to edit a file named "pre.tex" whose content is as follows

\documentclass[UTF8,hyperref]{ctexart}


and run

pdflatex -ini -jobname="pre" "&pdflatex pre.tex\dump"


to generate a .fmt file called "pre.fmt"

now, we use this precompiled .fmt file in out tex source like

%& pre
\begin{document}
中文
\end{document}


if we save it as a file named "en.tex", and pdflatex en.tex works fine. But if we save it as a file named "中文.tex", then pdflatex 中文.tex will show errors like

What is wrong here? why precompile doesn't support Tex file with Chinese file name?

• Do you type the name of the file? Can you try typing pdflatex, and then drag&drop the file on the Terminal window? It looks like your terminal doesn't see the Chinese characters, so what you type is not what it needs to see. I'd try that. Don't forget the space after pdflatex. – Alenanno Dec 15 '15 at 13:39
• @Alenanno drag is not working. – user15964 Dec 15 '15 at 13:42
• @Alenanno I add a link to my file, maybe you would like to test it – user15964 Dec 15 '15 at 13:46
• Well obviously pdflatex did open the file as it found the %& pre file and loaded the format but then wasn't able to continue. You should report this to the tex live mailing list. (But I would avoid such file names. There are not very portable.). – Ulrike Fischer Dec 15 '15 at 13:48
• Did you try running pdftex -format=pre 中文.tex, instead of relying on the %& line? – egreg Dec 15 '15 at 13:50

You can see the cause here

\edef\x{中文.tex}\show\x
\documentclass[UTF8,hyperref]{ctexart}
\edef\x{中文.tex}\show\x
\begin{document}
中文
\end{document}


Compare the two \show

Initially the bytes are all essentially inert and so the filename gets passed to the filesystem and would work, but after the class has been loaded the bytes have been made active and given definitions that enable typesetting of the characters but this means they expand to the wrong thing for the filename.

I don't have all the fonts to fully test this but I think it works if you change your format template to

\documentclass[UTF8,hyperref]{ctexart}

% set high chars active at start of job
% After the commandline has been read, but before Tex starts reading the
% file contents, the tokens in \everyjob are run.
% This loop sets every character between 127 and 255 to be catcode 13
% "active" which is the catcode they normally have after inputenc package
% or the ctexart class code.
\everyjob{{\count0=127
\loop
\global\catcode\count0=13
\ifnum\count0<255
\repeat
}}

% set high chars inactive now for filename reading
% This loop sets every character between 127 and 255 to be catcode 12
% "other" which is the normal catcode for punctuation. this means that
% no special interpretation is done and the filename works as in a normal
% format (where high characters are also not active until inputenc or
% Unlike the previous loop which is delayed in \everyjob
% This loop is executed here to make characters safe before the \dump
{\count0=127
\loop
\global\catcode\count0=12
\ifnum\count0<255

So that the 8bit characters are not active until \everyjob is run, after the filename has been read.
• Well, that's very interesting code. What's the e in \edef? – Alenanno Dec 15 '15 at 13:56
• @Alenanno \edef is expanded def, macros in the replacement text get expanded at definition time, needed here to show that 中文 expands to typesetting commands in the second case – David Carlisle Dec 15 '15 at 15:09