# Use of \csname in \input produces mysterious results

I am stuck with the following one where I want to input a file with a name in the line of \jobnameQ.tex.

Here is my MWE excerpted from a bigger picture.

\documentclass{article}

\usepackage{lipsum}

\usepackage{filecontents}

\def\csinput{\expandafter\input{\csname\jobname Q.tex\endcsname}}

\begin{document}

\begin{filecontents}{\jobname Q.tex}
\lipsum[1]
\end{filecontents}

\csinput

\end{document}


This does not input any file and does not produce any output.

However, I know that if I define \csinput in the manner

\def\csinput#1{\input{\jobname#1.tex}}


and call it like this, \csinput{Q} it will work.

I would like to know why the first methodology does not work. (And how it can be remedied.)

-

The \csname \jobname Q.tex\endcsname expands to \nameQ.tex, i.e. to the control sequence which is not defined. The undefined control sequences constructed by \csname...\endcsname have \relax meaning. The \expandafter in your code is redundant because { isn't expanable. The \input is a macro in the LaTeX. The \input{something} expands to the primitive \input something. Because the something here has a meaning \relax, the following TeX primitive command is processed:

\input \relax


Because the name of the file in \input primitive command can be delimited by another unexpandable primitive command (for example \relax), we have empty filename here. TeX appends to this empty filename the .tex suffix and performs to read .tex file. This file is present in typical TeX distributions with the contents: \message{File ignored}.

The result: this file is read and the message is printed.

Note I try to explain why the .tex file mentioned above is present in the TeX distributions. TeX stops when it finds an error (in \errorstopmode which is common setting) and waits to the user response. The typical user response is only "press Enter" which continues processing. But there is another type of error: I can't find file .... If user responses by Enter and the .tex doesn't exist then the error is printed again and again because only the real file name must be entered here. This would make a user irritation. The .tex file is read when user hit Enter in this situation and the process continues. This is similar behavior as in "normal" errors.

-
Is there a way out of the situation? That is, is there a way to input the file in the manner I want? And how filecontents handles the scenario of a similar kind? – Masroor Aug 5 '14 at 5:42
@Masroor: Just drop the surrounding \csname...\endcsname. Technically you also don't need the \expandafter, as \def\csinput{\input{\jobname Q.tex}} works fine. – Werner Aug 5 '14 at 6:00
@Masroor Why are you trying to do expansion when it's not required? As wipet has explained, your \csname stuff means TeX is looking for a file name that does not exist. As you've already found, the second version works just fine as TeX expands \jobname as part of constructing the file name. – Joseph Wright Aug 5 '14 at 6:01
@Werner That was the first solution I tried and it did not work with a message like undefined \jobnameQ or some such. Now I realize that solution must have been different from my line of thoughts. I can not locate my mistake in that first thought solution because the file has been changed so many times. (And I feel like deleting my question since I have made an easy thing difficult. But I will leave it in that manner since the answer from wipet points out the important issue of \relax in \csname which I was not aware off). – Masroor Aug 5 '14 at 6:22
Maybe there is another enlightenment: read the terminal messages and log file. The message about reading of .tex file is here. This means that isn't true: "TeX does not input any file". – wipet Aug 5 '14 at 6:31

Suppose \jobname has the value of test. Then \csname\jobname Q.tex\endcsname represents a macro with the name testQ.tex, just as \csname jobname\endcsname represents a macro with the name jobname.

The macro with a name testQ.tex (which would be written \testQ.tex if you could have dots in macro names) doesn't exist, so \input will not get the filename you want to give it.

Basically, you shouldn't use \csname here. You don't need a macro with the same name as the file, you need a macro (well, a macro plus some other stuff) whose value is the filename. Just this:

\def\csinput{\input{\jobname Q.tex}}

-