# How can I include a new command in \input

I'm trying to variate the file name by creating a newcommand \lbname in LaTeX. by printing out \lbname, I got lo3.txt in this case, but it wouldn't work in \input command. I get an error "TeX capacity exceeded, sorry [input stack size=5000".

\newcommand{\experr}{\input{examp_error..txt}}
\newcommand{\lbname}{lo\experr\unskip .txt}
\newcommand{\lb}{\input{\lbname}}

• the posted code does not generate any error, you may get an error depending what is in examp_error..txt and how and where you use the command \lb (which seems to be the same as \lbname, it is not clear what is the intention of the last definition). But you have given no indication of that use, so it is impossible to say why you get an error. – David Carlisle Aug 24 '15 at 19:48
• Sorry I posted the wrong code. I was trying to include \lbname in \input – verawarr Aug 24 '15 at 19:53
• Calling \lb would do \input{lo\input{examp_error..txt}\unskip .txt} which I can't see the usefulness of. What's in examp_error..txt? – egreg Aug 24 '15 at 19:57
• It's a text file from SAS code. In this case it's "3", but it varies. – verawarr Aug 24 '15 at 20:45

There are some issues with the snippet in the question:

• \input{} is not expandable, because it also checks, if the file exists. The primitive \@@input could be used instead, but there are better ways, see below.

• \unskip is also not expandable and can therefore not be used as part of a file name. See package trimspaces to remove spaces at the end of a macro.

Both issues can be avoided by using package catchfile. Assuming the file examp_err.txt (the two dots in the questions are a typo?) contains the number 3, then

\CatchFileEdef\experr{examp_error.txt}{\endlinechar=-1 }


will define the macro \experr, which expands to 3. \endlinechar=-1 removes the line ends, which would be converted to a space otherwise. Therefore \unskip or package trimspaces are not needed.

Then, \experr can be safely used as part of a file name:

\newcommand*{\lbname}{lo\experr.txt}


or

\edef\lbname{lo\experr.txt}


The file name \lbname can then be used in \input or whatever you want to do with it.

Full code snippet:

\usepackage{catchfile}

\CatchFileEdef\experr{examp_error.txt}{\endlinechar=-1 }
\edef\lbname{lo\experr.txt}


If examp_error.txt contains 3, then \lbname expands to lo3.txt.

Another solution from TeX primitive level:

\newread\exfile
\openin\exfile=examp_error.txt % the file where "3" or similar is included"
\ifeof\exfile \def\experr{} \errmessage{the file examp_error.txt doesn't exist}

This works in classical TeX and in LaTeX too. Note, that \input is TeX primitive in non-LaTeX formats. So, we use it as a primitive without {braces} in its argument.